forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading-indicator-spec.js
More file actions
81 lines (68 loc) · 2.22 KB
/
Copy pathloading-indicator-spec.js
File metadata and controls
81 lines (68 loc) · 2.22 KB
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
import React from 'react'
import { mount, unmount } from '@cypress/react'
import LoadingIndicator from './LoadingIndicator'
// compare these tests to Jest + Enzyme tests in
// https://github.com/bruceharris/react-unit-testing-example/blob/main/src/components/LoadingIndicator.test.js
describe('LoadingIndicator', () => {
describe('when isLoading is false', () => {
it('should render children', () => {
mount(
<LoadingIndicator isLoading={false}>
<div>ahoy!</div>
</LoadingIndicator>,
)
cy.contains('div', 'ahoy!')
})
})
describe('when isLoading is true', () => {
describe('given 200ms have not yet elapsed', () => {
it('should render nothing', () => {
mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>,
)
cy.contains('loading...').should('be.visible')
cy.contains('ahoy!').should('not.exist')
})
})
describe('given 2000ms have elapsed', () => {
it('should render loading indicator (waits 2 seconds)', () => {
mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>,
)
// this test runs for 2 seconds, since it just waits for the
// loading indicator to show up after "setTimeout"
cy.contains('loading...', { timeout: 2100 }).should('be.visible')
})
it('should render loading indicator (mocks clock)', () => {
cy.clock()
mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>,
)
// force 2 seconds to pass instantly
// and component's setTimeout to fire
cy.tick(2010)
cy.contains('loading...', { timeout: 100 }).should('be.visible')
})
})
})
describe('on unmount', () => {
it('should clear timeout', () => {
cy.clock()
cy.window().then((win) => cy.spy(win, 'clearTimeout').as('clearTimeout'))
mount(
<LoadingIndicator isLoading={true}>
<div>ahoy!</div>
</LoadingIndicator>,
)
cy.tick(2010)
unmount()
cy.get('@clearTimeout').should('have.been.calledOnce')
})
})
})