tonder-web-sdk 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/.env-example +1 -0
  2. package/.htaccess +1 -0
  3. package/README.md +204 -0
  4. package/cypress/e2e/1-getting-started/todo.cy.js +143 -0
  5. package/cypress/e2e/2-advanced-examples/actions.cy.js +299 -0
  6. package/cypress/e2e/2-advanced-examples/aliasing.cy.js +39 -0
  7. package/cypress/e2e/2-advanced-examples/assertions.cy.js +176 -0
  8. package/cypress/e2e/2-advanced-examples/connectors.cy.js +98 -0
  9. package/cypress/e2e/2-advanced-examples/cookies.cy.js +118 -0
  10. package/cypress/e2e/2-advanced-examples/cypress_api.cy.js +185 -0
  11. package/cypress/e2e/2-advanced-examples/files.cy.js +85 -0
  12. package/cypress/e2e/2-advanced-examples/location.cy.js +32 -0
  13. package/cypress/e2e/2-advanced-examples/misc.cy.js +104 -0
  14. package/cypress/e2e/2-advanced-examples/navigation.cy.js +56 -0
  15. package/cypress/e2e/2-advanced-examples/network_requests.cy.js +163 -0
  16. package/cypress/e2e/2-advanced-examples/querying.cy.js +114 -0
  17. package/cypress/e2e/2-advanced-examples/spies_stubs_clocks.cy.js +201 -0
  18. package/cypress/e2e/2-advanced-examples/storage.cy.js +110 -0
  19. package/cypress/e2e/2-advanced-examples/traversal.cy.js +121 -0
  20. package/cypress/e2e/2-advanced-examples/utilities.cy.js +108 -0
  21. package/cypress/e2e/2-advanced-examples/viewport.cy.js +58 -0
  22. package/cypress/e2e/2-advanced-examples/waiting.cy.js +30 -0
  23. package/cypress/e2e/2-advanced-examples/window.cy.js +22 -0
  24. package/cypress/fixtures/example.json +5 -0
  25. package/cypress/support/commands.js +25 -0
  26. package/cypress/support/e2e.js +20 -0
  27. package/cypress.config.js +9 -0
  28. package/index.html +178 -0
  29. package/index.js.example +50 -0
  30. package/package.json +29 -0
  31. package/samples/react/README.md +70 -0
  32. package/samples/react/build/asset-manifest.json +16 -0
  33. package/samples/react/build/favicon.ico +0 -0
  34. package/samples/react/build/index.html +1 -0
  35. package/samples/react/build/logo192.png +0 -0
  36. package/samples/react/build/logo512.png +0 -0
  37. package/samples/react/build/manifest.json +25 -0
  38. package/samples/react/build/robots.txt +3 -0
  39. package/samples/react/build/static/css/main.073c9b0a.css +2 -0
  40. package/samples/react/build/static/css/main.073c9b0a.css.map +1 -0
  41. package/samples/react/build/static/js/787.b83ed06f.chunk.js +2 -0
  42. package/samples/react/build/static/js/787.b83ed06f.chunk.js.map +1 -0
  43. package/samples/react/build/static/js/main.0a848807.js +3 -0
  44. package/samples/react/build/static/js/main.0a848807.js.LICENSE.txt +39 -0
  45. package/samples/react/build/static/js/main.0a848807.js.map +1 -0
  46. package/samples/react/build/static/media/sdk-icons.b491623214b2af4cccdb.png +0 -0
  47. package/samples/react/package-lock.json +28973 -0
  48. package/samples/react/package.json +44 -0
  49. package/samples/react/public/favicon.ico +0 -0
  50. package/samples/react/public/index.html +43 -0
  51. package/samples/react/public/logo192.png +0 -0
  52. package/samples/react/public/logo512.png +0 -0
  53. package/samples/react/public/manifest.json +25 -0
  54. package/samples/react/public/robots.txt +3 -0
  55. package/samples/react/src/App.css +38 -0
  56. package/samples/react/src/App.js +22 -0
  57. package/samples/react/src/App.test.js +8 -0
  58. package/samples/react/src/assets/img/sdk-icons.png +0 -0
  59. package/samples/react/src/components/Cart.js +29 -0
  60. package/samples/react/src/components/ProductCard.js +27 -0
  61. package/samples/react/src/context/CartContext.js +116 -0
  62. package/samples/react/src/index.css +13 -0
  63. package/samples/react/src/index.js +17 -0
  64. package/samples/react/src/logo.svg +1 -0
  65. package/samples/react/src/reportWebVitals.js +13 -0
  66. package/samples/react/src/screens/Checkout.js +82 -0
  67. package/samples/react/src/screens/Store.js +21 -0
  68. package/samples/react/src/setupTests.js +5 -0
  69. package/samples/react/src/storeProducts.js +30 -0
  70. package/src/classes/3dsHandler.js +203 -0
  71. package/src/classes/checkout.js +125 -0
  72. package/src/classes/inlineCheckout.js +349 -0
  73. package/src/data/api.js +109 -0
  74. package/src/helpers/skyflow.js +139 -0
  75. package/src/helpers/styles.js +61 -0
  76. package/src/helpers/template.js +112 -0
  77. package/src/helpers/utils.js +68 -0
  78. package/src/index-dev.js +129 -0
  79. package/src/index.html +58 -0
  80. package/src/index.js +7 -0
  81. package/success.html +22 -0
  82. package/v1/bundle.min.js +18 -0
  83. package/webpack.config.js +66 -0
@@ -0,0 +1,104 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Misc', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/misc')
6
+ })
7
+
8
+ it('.end() - end the command chain', () => {
9
+ // https://on.cypress.io/end
10
+
11
+ // cy.end is useful when you want to end a chain of commands
12
+ // and force Cypress to re-query from the root element
13
+ cy.get('.misc-table').within(() => {
14
+ // ends the current chain and yields null
15
+ cy.contains('Cheryl').click().end()
16
+
17
+ // queries the entire table again
18
+ cy.contains('Charles').click()
19
+ })
20
+ })
21
+
22
+ it('cy.exec() - execute a system command', () => {
23
+ // execute a system command.
24
+ // so you can take actions necessary for
25
+ // your test outside the scope of Cypress.
26
+ // https://on.cypress.io/exec
27
+
28
+ // we can use Cypress.platform string to
29
+ // select appropriate command
30
+ // https://on.cypress/io/platform
31
+ cy.log(`Platform ${Cypress.platform} architecture ${Cypress.arch}`)
32
+
33
+ // on CircleCI Windows build machines we have a failure to run bash shell
34
+ // https://github.com/cypress-io/cypress/issues/5169
35
+ // so skip some of the tests by passing flag "--env circle=true"
36
+ const isCircleOnWindows = Cypress.platform === 'win32' && Cypress.env('circle')
37
+
38
+ if (isCircleOnWindows) {
39
+ cy.log('Skipping test on CircleCI')
40
+
41
+ return
42
+ }
43
+
44
+ // cy.exec problem on Shippable CI
45
+ // https://github.com/cypress-io/cypress/issues/6718
46
+ const isShippable = Cypress.platform === 'linux' && Cypress.env('shippable')
47
+
48
+ if (isShippable) {
49
+ cy.log('Skipping test on ShippableCI')
50
+
51
+ return
52
+ }
53
+
54
+ cy.exec('echo Jane Lane')
55
+ .its('stdout').should('contain', 'Jane Lane')
56
+
57
+ if (Cypress.platform === 'win32') {
58
+ cy.exec(`print ${Cypress.config('configFile')}`)
59
+ .its('stderr').should('be.empty')
60
+ } else {
61
+ cy.exec(`cat ${Cypress.config('configFile')}`)
62
+ .its('stderr').should('be.empty')
63
+
64
+ cy.exec('pwd')
65
+ .its('code').should('eq', 0)
66
+ }
67
+ })
68
+
69
+ it('cy.focused() - get the DOM element that has focus', () => {
70
+ // https://on.cypress.io/focused
71
+ cy.get('.misc-form').find('#name').click()
72
+ cy.focused().should('have.id', 'name')
73
+
74
+ cy.get('.misc-form').find('#description').click()
75
+ cy.focused().should('have.id', 'description')
76
+ })
77
+
78
+ context('Cypress.Screenshot', function () {
79
+ it('cy.screenshot() - take a screenshot', () => {
80
+ // https://on.cypress.io/screenshot
81
+ cy.screenshot('my-image')
82
+ })
83
+
84
+ it('Cypress.Screenshot.defaults() - change default config of screenshots', function () {
85
+ Cypress.Screenshot.defaults({
86
+ blackout: ['.foo'],
87
+ capture: 'viewport',
88
+ clip: { x: 0, y: 0, width: 200, height: 200 },
89
+ scale: false,
90
+ disableTimersAndAnimations: true,
91
+ screenshotOnRunFailure: true,
92
+ onBeforeScreenshot () { },
93
+ onAfterScreenshot () { },
94
+ })
95
+ })
96
+ })
97
+
98
+ it('cy.wrap() - wrap an object', () => {
99
+ // https://on.cypress.io/wrap
100
+ cy.wrap({ foo: 'bar' })
101
+ .should('have.property', 'foo')
102
+ .and('include', 'bar')
103
+ })
104
+ })
@@ -0,0 +1,56 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Navigation', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io')
6
+ cy.get('.navbar-nav').contains('Commands').click()
7
+ cy.get('.dropdown-menu').contains('Navigation').click()
8
+ })
9
+
10
+ it('cy.go() - go back or forward in the browser\'s history', () => {
11
+ // https://on.cypress.io/go
12
+
13
+ cy.location('pathname').should('include', 'navigation')
14
+
15
+ cy.go('back')
16
+ cy.location('pathname').should('not.include', 'navigation')
17
+
18
+ cy.go('forward')
19
+ cy.location('pathname').should('include', 'navigation')
20
+
21
+ // clicking back
22
+ cy.go(-1)
23
+ cy.location('pathname').should('not.include', 'navigation')
24
+
25
+ // clicking forward
26
+ cy.go(1)
27
+ cy.location('pathname').should('include', 'navigation')
28
+ })
29
+
30
+ it('cy.reload() - reload the page', () => {
31
+ // https://on.cypress.io/reload
32
+ cy.reload()
33
+
34
+ // reload the page without using the cache
35
+ cy.reload(true)
36
+ })
37
+
38
+ it('cy.visit() - visit a remote url', () => {
39
+ // https://on.cypress.io/visit
40
+
41
+ // Visit any sub-domain of your current domain
42
+
43
+ // Pass options to the visit
44
+ cy.visit('https://example.cypress.io/commands/navigation', {
45
+ timeout: 50000, // increase total time for the visit to resolve
46
+ onBeforeLoad (contentWindow) {
47
+ // contentWindow is the remote page's window object
48
+ expect(typeof contentWindow === 'object').to.be.true
49
+ },
50
+ onLoad (contentWindow) {
51
+ // contentWindow is the remote page's window object
52
+ expect(typeof contentWindow === 'object').to.be.true
53
+ },
54
+ })
55
+ })
56
+ })
@@ -0,0 +1,163 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Network Requests', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/network-requests')
6
+ })
7
+
8
+ // Manage HTTP requests in your app
9
+
10
+ it('cy.request() - make an XHR request', () => {
11
+ // https://on.cypress.io/request
12
+ cy.request('https://jsonplaceholder.cypress.io/comments')
13
+ .should((response) => {
14
+ expect(response.status).to.eq(200)
15
+ // the server sometimes gets an extra comment posted from another machine
16
+ // which gets returned as 1 extra object
17
+ expect(response.body).to.have.property('length').and.be.oneOf([500, 501])
18
+ expect(response).to.have.property('headers')
19
+ expect(response).to.have.property('duration')
20
+ })
21
+ })
22
+
23
+ it('cy.request() - verify response using BDD syntax', () => {
24
+ cy.request('https://jsonplaceholder.cypress.io/comments')
25
+ .then((response) => {
26
+ // https://on.cypress.io/assertions
27
+ expect(response).property('status').to.equal(200)
28
+ expect(response).property('body').to.have.property('length').and.be.oneOf([500, 501])
29
+ expect(response).to.include.keys('headers', 'duration')
30
+ })
31
+ })
32
+
33
+ it('cy.request() with query parameters', () => {
34
+ // will execute request
35
+ // https://jsonplaceholder.cypress.io/comments?postId=1&id=3
36
+ cy.request({
37
+ url: 'https://jsonplaceholder.cypress.io/comments',
38
+ qs: {
39
+ postId: 1,
40
+ id: 3,
41
+ },
42
+ })
43
+ .its('body')
44
+ .should('be.an', 'array')
45
+ .and('have.length', 1)
46
+ .its('0') // yields first element of the array
47
+ .should('contain', {
48
+ postId: 1,
49
+ id: 3,
50
+ })
51
+ })
52
+
53
+ it('cy.request() - pass result to the second request', () => {
54
+ // first, let's find out the userId of the first user we have
55
+ cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
56
+ .its('body') // yields the response object
57
+ .its('0') // yields the first element of the returned list
58
+ // the above two commands its('body').its('0')
59
+ // can be written as its('body.0')
60
+ // if you do not care about TypeScript checks
61
+ .then((user) => {
62
+ expect(user).property('id').to.be.a('number')
63
+ // make a new post on behalf of the user
64
+ cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
65
+ userId: user.id,
66
+ title: 'Cypress Test Runner',
67
+ body: 'Fast, easy and reliable testing for anything that runs in a browser.',
68
+ })
69
+ })
70
+ // note that the value here is the returned value of the 2nd request
71
+ // which is the new post object
72
+ .then((response) => {
73
+ expect(response).property('status').to.equal(201) // new entity created
74
+ expect(response).property('body').to.contain({
75
+ title: 'Cypress Test Runner',
76
+ })
77
+
78
+ // we don't know the exact post id - only that it will be > 100
79
+ // since JSONPlaceholder has built-in 100 posts
80
+ expect(response.body).property('id').to.be.a('number')
81
+ .and.to.be.gt(100)
82
+
83
+ // we don't know the user id here - since it was in above closure
84
+ // so in this test just confirm that the property is there
85
+ expect(response.body).property('userId').to.be.a('number')
86
+ })
87
+ })
88
+
89
+ it('cy.request() - save response in the shared test context', () => {
90
+ // https://on.cypress.io/variables-and-aliases
91
+ cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
92
+ .its('body').its('0') // yields the first element of the returned list
93
+ .as('user') // saves the object in the test context
94
+ .then(function () {
95
+ // NOTE 👀
96
+ // By the time this callback runs the "as('user')" command
97
+ // has saved the user object in the test context.
98
+ // To access the test context we need to use
99
+ // the "function () { ... }" callback form,
100
+ // otherwise "this" points at a wrong or undefined object!
101
+ cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
102
+ userId: this.user.id,
103
+ title: 'Cypress Test Runner',
104
+ body: 'Fast, easy and reliable testing for anything that runs in a browser.',
105
+ })
106
+ .its('body').as('post') // save the new post from the response
107
+ })
108
+ .then(function () {
109
+ // When this callback runs, both "cy.request" API commands have finished
110
+ // and the test context has "user" and "post" objects set.
111
+ // Let's verify them.
112
+ expect(this.post, 'post has the right user id').property('userId').to.equal(this.user.id)
113
+ })
114
+ })
115
+
116
+ it('cy.intercept() - route responses to matching requests', () => {
117
+ // https://on.cypress.io/intercept
118
+
119
+ let message = 'whoa, this comment does not exist'
120
+
121
+ // Listen to GET to comments/1
122
+ cy.intercept('GET', '**/comments/*').as('getComment')
123
+
124
+ // we have code that gets a comment when
125
+ // the button is clicked in scripts.js
126
+ cy.get('.network-btn').click()
127
+
128
+ // https://on.cypress.io/wait
129
+ cy.wait('@getComment').its('response.statusCode').should('be.oneOf', [200, 304])
130
+
131
+ // Listen to POST to comments
132
+ cy.intercept('POST', '**/comments').as('postComment')
133
+
134
+ // we have code that posts a comment when
135
+ // the button is clicked in scripts.js
136
+ cy.get('.network-post').click()
137
+ cy.wait('@postComment').should(({ request, response }) => {
138
+ expect(request.body).to.include('email')
139
+ expect(request.headers).to.have.property('content-type')
140
+ expect(response && response.body).to.have.property('name', 'Using POST in cy.intercept()')
141
+ })
142
+
143
+ // Stub a response to PUT comments/ ****
144
+ cy.intercept({
145
+ method: 'PUT',
146
+ url: '**/comments/*',
147
+ }, {
148
+ statusCode: 404,
149
+ body: { error: message },
150
+ headers: { 'access-control-allow-origin': '*' },
151
+ delayMs: 500,
152
+ }).as('putComment')
153
+
154
+ // we have code that puts a comment when
155
+ // the button is clicked in scripts.js
156
+ cy.get('.network-put').click()
157
+
158
+ cy.wait('@putComment')
159
+
160
+ // our 404 statusCode logic in scripts.js executed
161
+ cy.get('.network-put-comment').should('contain', message)
162
+ })
163
+ })
@@ -0,0 +1,114 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Querying', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/querying')
6
+ })
7
+
8
+ // The most commonly used query is 'cy.get()', you can
9
+ // think of this like the '$' in jQuery
10
+
11
+ it('cy.get() - query DOM elements', () => {
12
+ // https://on.cypress.io/get
13
+
14
+ cy.get('#query-btn').should('contain', 'Button')
15
+
16
+ cy.get('.query-btn').should('contain', 'Button')
17
+
18
+ cy.get('#querying .well>button:first').should('contain', 'Button')
19
+ // ↲
20
+ // Use CSS selectors just like jQuery
21
+
22
+ cy.get('[data-test-id="test-example"]').should('have.class', 'example')
23
+
24
+ // 'cy.get()' yields jQuery object, you can get its attribute
25
+ // by invoking `.attr()` method
26
+ cy.get('[data-test-id="test-example"]')
27
+ .invoke('attr', 'data-test-id')
28
+ .should('equal', 'test-example')
29
+
30
+ // or you can get element's CSS property
31
+ cy.get('[data-test-id="test-example"]')
32
+ .invoke('css', 'position')
33
+ .should('equal', 'static')
34
+
35
+ // or use assertions directly during 'cy.get()'
36
+ // https://on.cypress.io/assertions
37
+ cy.get('[data-test-id="test-example"]')
38
+ .should('have.attr', 'data-test-id', 'test-example')
39
+ .and('have.css', 'position', 'static')
40
+ })
41
+
42
+ it('cy.contains() - query DOM elements with matching content', () => {
43
+ // https://on.cypress.io/contains
44
+ cy.get('.query-list')
45
+ .contains('bananas')
46
+ .should('have.class', 'third')
47
+
48
+ // we can pass a regexp to `.contains()`
49
+ cy.get('.query-list')
50
+ .contains(/^b\w+/)
51
+ .should('have.class', 'third')
52
+
53
+ cy.get('.query-list')
54
+ .contains('apples')
55
+ .should('have.class', 'first')
56
+
57
+ // passing a selector to contains will
58
+ // yield the selector containing the text
59
+ cy.get('#querying')
60
+ .contains('ul', 'oranges')
61
+ .should('have.class', 'query-list')
62
+
63
+ cy.get('.query-button')
64
+ .contains('Save Form')
65
+ .should('have.class', 'btn')
66
+ })
67
+
68
+ it('.within() - query DOM elements within a specific element', () => {
69
+ // https://on.cypress.io/within
70
+ cy.get('.query-form').within(() => {
71
+ cy.get('input:first').should('have.attr', 'placeholder', 'Email')
72
+ cy.get('input:last').should('have.attr', 'placeholder', 'Password')
73
+ })
74
+ })
75
+
76
+ it('cy.root() - query the root DOM element', () => {
77
+ // https://on.cypress.io/root
78
+
79
+ // By default, root is the document
80
+ cy.root().should('match', 'html')
81
+
82
+ cy.get('.query-ul').within(() => {
83
+ // In this within, the root is now the ul DOM element
84
+ cy.root().should('have.class', 'query-ul')
85
+ })
86
+ })
87
+
88
+ it('best practices - selecting elements', () => {
89
+ // https://on.cypress.io/best-practices#Selecting-Elements
90
+ cy.get('[data-cy=best-practices-selecting-elements]').within(() => {
91
+ // Worst - too generic, no context
92
+ cy.get('button').click()
93
+
94
+ // Bad. Coupled to styling. Highly subject to change.
95
+ cy.get('.btn.btn-large').click()
96
+
97
+ // Average. Coupled to the `name` attribute which has HTML semantics.
98
+ cy.get('[name=submission]').click()
99
+
100
+ // Better. But still coupled to styling or JS event listeners.
101
+ cy.get('#main').click()
102
+
103
+ // Slightly better. Uses an ID but also ensures the element
104
+ // has an ARIA role attribute
105
+ cy.get('#main[role=button]').click()
106
+
107
+ // Much better. But still coupled to text content that may change.
108
+ cy.contains('Submit').click()
109
+
110
+ // Best. Insulated from all changes.
111
+ cy.get('[data-cy=submit]').click()
112
+ })
113
+ })
114
+ })
@@ -0,0 +1,201 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Spies, Stubs, and Clock', () => {
4
+ it('cy.spy() - wrap a method in a spy', () => {
5
+ // https://on.cypress.io/spy
6
+ cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
7
+
8
+ const obj = {
9
+ foo () {},
10
+ }
11
+
12
+ const spy = cy.spy(obj, 'foo').as('anyArgs')
13
+
14
+ obj.foo()
15
+
16
+ expect(spy).to.be.called
17
+ })
18
+
19
+ it('cy.spy() retries until assertions pass', () => {
20
+ cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
21
+
22
+ const obj = {
23
+ /**
24
+ * Prints the argument passed
25
+ * @param x {any}
26
+ */
27
+ foo (x) {
28
+ console.log('obj.foo called with', x)
29
+ },
30
+ }
31
+
32
+ cy.spy(obj, 'foo').as('foo')
33
+
34
+ setTimeout(() => {
35
+ obj.foo('first')
36
+ }, 500)
37
+
38
+ setTimeout(() => {
39
+ obj.foo('second')
40
+ }, 2500)
41
+
42
+ cy.get('@foo').should('have.been.calledTwice')
43
+ })
44
+
45
+ it('cy.stub() - create a stub and/or replace a function with stub', () => {
46
+ // https://on.cypress.io/stub
47
+ cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
48
+
49
+ const obj = {
50
+ /**
51
+ * prints both arguments to the console
52
+ * @param a {string}
53
+ * @param b {string}
54
+ */
55
+ foo (a, b) {
56
+ console.log('a', a, 'b', b)
57
+ },
58
+ }
59
+
60
+ const stub = cy.stub(obj, 'foo').as('foo')
61
+
62
+ obj.foo('foo', 'bar')
63
+
64
+ expect(stub).to.be.called
65
+ })
66
+
67
+ it('cy.clock() - control time in the browser', () => {
68
+ // https://on.cypress.io/clock
69
+
70
+ // create the date in UTC so its always the same
71
+ // no matter what local timezone the browser is running in
72
+ const now = new Date(Date.UTC(2017, 2, 14)).getTime()
73
+
74
+ cy.clock(now)
75
+ cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
76
+ cy.get('#clock-div').click()
77
+ .should('have.text', '1489449600')
78
+ })
79
+
80
+ it('cy.tick() - move time in the browser', () => {
81
+ // https://on.cypress.io/tick
82
+
83
+ // create the date in UTC so its always the same
84
+ // no matter what local timezone the browser is running in
85
+ const now = new Date(Date.UTC(2017, 2, 14)).getTime()
86
+
87
+ cy.clock(now)
88
+ cy.visit('https://example.cypress.io/commands/spies-stubs-clocks')
89
+ cy.get('#tick-div').click()
90
+ .should('have.text', '1489449600')
91
+
92
+ cy.tick(10000) // 10 seconds passed
93
+ cy.get('#tick-div').click()
94
+ .should('have.text', '1489449610')
95
+ })
96
+
97
+ it('cy.stub() matches depending on arguments', () => {
98
+ // see all possible matchers at
99
+ // https://sinonjs.org/releases/latest/matchers/
100
+ const greeter = {
101
+ /**
102
+ * Greets a person
103
+ * @param {string} name
104
+ */
105
+ greet (name) {
106
+ return `Hello, ${name}!`
107
+ },
108
+ }
109
+
110
+ cy.stub(greeter, 'greet')
111
+ .callThrough() // if you want non-matched calls to call the real method
112
+ .withArgs(Cypress.sinon.match.string).returns('Hi')
113
+ .withArgs(Cypress.sinon.match.number).throws(new Error('Invalid name'))
114
+
115
+ expect(greeter.greet('World')).to.equal('Hi')
116
+ expect(() => greeter.greet(42)).to.throw('Invalid name')
117
+ expect(greeter.greet).to.have.been.calledTwice
118
+
119
+ // non-matched calls goes the actual method
120
+ expect(greeter.greet()).to.equal('Hello, undefined!')
121
+ })
122
+
123
+ it('matches call arguments using Sinon matchers', () => {
124
+ // see all possible matchers at
125
+ // https://sinonjs.org/releases/latest/matchers/
126
+ const calculator = {
127
+ /**
128
+ * returns the sum of two arguments
129
+ * @param a {number}
130
+ * @param b {number}
131
+ */
132
+ add (a, b) {
133
+ return a + b
134
+ },
135
+ }
136
+
137
+ const spy = cy.spy(calculator, 'add').as('add')
138
+
139
+ expect(calculator.add(2, 3)).to.equal(5)
140
+
141
+ // if we want to assert the exact values used during the call
142
+ expect(spy).to.be.calledWith(2, 3)
143
+
144
+ // let's confirm "add" method was called with two numbers
145
+ expect(spy).to.be.calledWith(Cypress.sinon.match.number, Cypress.sinon.match.number)
146
+
147
+ // alternatively, provide the value to match
148
+ expect(spy).to.be.calledWith(Cypress.sinon.match(2), Cypress.sinon.match(3))
149
+
150
+ // match any value
151
+ expect(spy).to.be.calledWith(Cypress.sinon.match.any, 3)
152
+
153
+ // match any value from a list
154
+ expect(spy).to.be.calledWith(Cypress.sinon.match.in([1, 2, 3]), 3)
155
+
156
+ /**
157
+ * Returns true if the given number is even
158
+ * @param {number} x
159
+ */
160
+ const isEven = (x) => x % 2 === 0
161
+
162
+ // expect the value to pass a custom predicate function
163
+ // the second argument to "sinon.match(predicate, message)" is
164
+ // shown if the predicate does not pass and assertion fails
165
+ expect(spy).to.be.calledWith(Cypress.sinon.match(isEven, 'isEven'), 3)
166
+
167
+ /**
168
+ * Returns a function that checks if a given number is larger than the limit
169
+ * @param {number} limit
170
+ * @returns {(x: number) => boolean}
171
+ */
172
+ const isGreaterThan = (limit) => (x) => x > limit
173
+
174
+ /**
175
+ * Returns a function that checks if a given number is less than the limit
176
+ * @param {number} limit
177
+ * @returns {(x: number) => boolean}
178
+ */
179
+ const isLessThan = (limit) => (x) => x < limit
180
+
181
+ // you can combine several matchers using "and", "or"
182
+ expect(spy).to.be.calledWith(
183
+ Cypress.sinon.match.number,
184
+ Cypress.sinon.match(isGreaterThan(2), '> 2').and(Cypress.sinon.match(isLessThan(4), '< 4')),
185
+ )
186
+
187
+ expect(spy).to.be.calledWith(
188
+ Cypress.sinon.match.number,
189
+ Cypress.sinon.match(isGreaterThan(200), '> 200').or(Cypress.sinon.match(3)),
190
+ )
191
+
192
+ // matchers can be used from BDD assertions
193
+ cy.get('@add').should('have.been.calledWith',
194
+ Cypress.sinon.match.number, Cypress.sinon.match(3))
195
+
196
+ // you can alias matchers for shorter test code
197
+ const { match: M } = Cypress.sinon
198
+
199
+ cy.get('@add').should('have.been.calledWith', M.number, M(3))
200
+ })
201
+ })