/
wait-for-object-property-spec.js
124 lines (105 loc) · 3.07 KB
/
wait-for-object-property-spec.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/// <reference types="cypress" />
// several unit tests showing how Cypress wraps an object
// and can wait for a property to:
// - change its value
// - be added to the object
// - be deleted from the object
context('waiting for a property of an object', () => {
it('detects an existing property', () => {
// assert given object has a property with a given value
expect({ foo: 42 }).to.have.property('foo', 42)
// alternative - use cy.wrap + "should" syntax
// @see https://on.cypress.io/wrap
cy.wrap({ foo: 42 }).should('have.property', 'foo', 42)
})
it('waits for changed property value', () => {
const o = { foo: 20 }
// changes property "foo" after delay
setTimeout(() => {
o.foo = 42
}, 100)
// "expect" syntax does NOT work
// because it is not retried!
// expect(o).to.have.property('foo', 42)
// wrapping an object and using "should" syntax retries
// the assertion until the "o.foo = 42" runs and the assertion passes
cy.wrap(o).should('have.property', 'foo', 42)
})
it('waits for added property value', () => {
const o = {}
setTimeout(() => {
o.foo = 42
}, 100)
// wrapping an object and using "should" syntax retries
// the assertion until the "o.foo = 42" runs and the assertion passes
cy.wrap(o).should('have.property', 'foo', 42)
// if we are not interested in the value we could use assertion
// .should('have.property', 'foo')
})
it('waits for added property using .its', () => {
const o = {}
setTimeout(() => {
o.foo = 42
}, 100)
// @see https://on.cypress.io/its
cy.wrap(o)
.its('foo')
.should('exist')
})
it('waits for property to be deleted', () => {
const o = {
foo: 42,
}
setTimeout(() => {
delete o.foo
}, 100)
// @see https://on.cypress.io/its
// we cannot use cy.wrap(o).its('foo').should...
// because the property exists from the start and becomes
// the subject of the command chain right away
// so we must wrap the object and use assertion on the object
cy.wrap(o).should('not.have.property', 'foo')
})
it('waits for added property value using .its', () => {
const o = {}
setTimeout(() => {
o.foo = 42
}, 100)
// @see https://on.cypress.io/its
cy.wrap(o)
.its('foo')
.should('eq', 42)
})
it('waits for added nested property value', () => {
const o = {}
setTimeout(() => {
o.foo = {
bar: {
baz: 42,
},
}
}, 100)
// cy.its is awesome for waiting for a nested property that
// passes assertion after it
cy.wrap(o)
.its('foo.bar.baz')
.should('equal', 42)
})
it('waits for deleted nested property', () => {
const o = {
foo: {
bar: {
baz: 42,
},
},
}
setTimeout(() => {
delete o.foo.bar.baz
}, 100)
// traverse the object to {baz: 42} and retry assertion
// until the subject {baz: 42} does NOT have property "baz"
cy.wrap(o)
.its('foo.bar')
.should('not.have.property', 'baz')
})
})