-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimers.js
48 lines (43 loc) · 1.14 KB
/
timers.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
/// <reference types="cypress" />
const React = require('react')
const { useQuery, queryCache } = require('react-query')
const {mount}= require('cypress-react-unit-test')
beforeEach(() => queryCache.clear())
it('renders data after real 300ms', () => {
function Example() {
const {data} = useQuery({
queryKey: 'some-key',
queryFn: () => {
return new Promise((r) => setTimeout(() => {
return r('data')
}, 300))
},
})
return data || 'no data yet'
}
mount(<Example />)
cy.contains('no data yet')
// and the it shows "data"
cy.contains(/^data$/)
})
it('renders data after mock clock delay', () => {
// notice 3000ms delay
function Example() {
const {data} = useQuery({
queryKey: 'some-key',
queryFn: () => {
return new Promise((r) => setTimeout(() => {
return r('data')
}, 3000))
},
})
return data || 'no data yet'
}
cy.clock()
mount(<Example />)
cy.contains('no data yet')
// advance clock by 3 seconds with a single command
cy.tick(3010)
// and the it shows "data" right away
cy.contains(/^data$/, {timeout: 100})
})