forked from bahmutov/cypress-data-session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvalidate.cy.js
76 lines (66 loc) · 2.13 KB
/
invalidate.cy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// @ts-check
import '../../src'
describe('Data C', () => {
beforeEach(() => {
Cypress.clearDataSession('C')
})
it('exists under an alias', function () {
// force invalidation by putting some random data
Cypress.setDataSession('C', 42)
cy.dataSession(
'C',
() => 'c',
// notice that the validate always fails
(x) => x === 'd',
// provide the third argument that is called
// if the validation fails (in this case, it is always called)
cy.stub().as('invalidated'),
)
// new value is computed and set
cy.get('@C').should('equal', 'c')
// our initial wrong value was passed to onInvalidate argument
cy.get('@invalidated').should('be.calledOnceWith', 42)
})
it('calls expected functions (valid)', function () {
Cypress.setDataSession('C', 42)
const setup = cy.stub().as('setup').returns('c')
const validate = cy.stub().as('validate').returns(true)
const invalidated = cy.stub().as('invalidated')
cy.dataSession({
name: 'C',
setup,
validate,
onInvalidated: invalidated,
})
.should('equal', 42)
.then(() => {
expect(validate).to.be.calledOnceWithExactly(42)
expect(setup).to.not.be.called
expect(invalidated).to.not.be.called
})
})
it('calls expected functions (invalidated)', function () {
Cypress.setDataSession('C', 42)
const setup = cy.stub().as('setup').returns('c')
const validate = cy.stub().as('validate').returns(false)
const invalidated = cy.stub().as('invalidated')
cy.dataSession({
name: 'C',
setup,
validate,
onInvalidated: invalidated,
})
// changed the data to whatever "setup" yields
.should('equal', 'c')
.then(() => {
expect(validate).to.be.calledOnceWithExactly(42)
expect(setup).to.be.calledOnce
expect(invalidated).to.be.calledOnceWithExactly(42)
// check the order of calls
expect(validate, 'validate -> invalidated').to.be.calledBefore(
invalidated,
)
expect(invalidated, 'invalidated -> setup').to.be.calledBefore(setup)
})
})
})