-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathindex.test.js
More file actions
121 lines (104 loc) · 4.06 KB
/
index.test.js
File metadata and controls
121 lines (104 loc) · 4.06 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
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
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React from 'react'
import {screen, act} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {ChakraProvider} from '@chakra-ui/react'
import theme from '../../theme'
import {renderWithRouter} from '../../utils/test-utils'
import OfflineBoundary, {UnwrappedOfflineBoundary} from '../../components/offline-boundary/index'
// Custom render function that combines Router and Chakra contexts
const renderWithRouterAndChakra = (component) => {
const ComponentWithChakra = () => <ChakraProvider value={theme}>{component}</ChakraProvider>
return renderWithRouter(<ComponentWithChakra />)
}
class ChunkLoadError extends Error {
constructor(...params) {
// Pass remaining arguments (including vendor specific ones) to parent constructor
super(...params)
this.name = 'ChunkLoadError'
}
}
describe('The OfflineBoundary', () => {
beforeEach(() => {
// React's logging is noisey even when an Error Boundary catches. Silence
// the distracting logs during tests, since they are expected in any event.
jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
console.error.mockRestore()
})
test('should render its children', () => {
renderWithRouterAndChakra(
<OfflineBoundary isOnline={true}>
<div id="child">child</div>
</OfflineBoundary>
)
expect(screen.getByText(/child/i)).toBeInTheDocument()
})
test('should render the error splash when a child throws a chunk load error', () => {
const ThrowingComponent = () => {
throw new ChunkLoadError()
}
renderWithRouterAndChakra(
<OfflineBoundary isOnline={true}>
<div>
<ThrowingComponent />
<div id="child">child</div>
</div>
</OfflineBoundary>
)
expect(screen.getByRole('img', {hidden: true})).toBeInTheDocument()
expect(
screen.getByRole('heading', {name: /you are currently offline/i})
).toBeInTheDocument()
expect(screen.queryByText(/child/i)).not.toBeInTheDocument()
})
test('should re-throw errors that are not chunk load errors', () => {
const ThrowingComponent = () => {
throw new Error('Anything else')
}
expect(() => {
renderWithRouterAndChakra(
<OfflineBoundary isOnline={true}>
<div>
<ThrowingComponent />
<div id="child">child</div>
</div>
</OfflineBoundary>
)
}).toThrow()
})
test('should call clearError when retry button is clicked', async () => {
const user = userEvent.setup()
const ThrowingComponent = () => {
throw new ChunkLoadError()
}
const clearErrorSpy = jest.spyOn(UnwrappedOfflineBoundary.prototype, 'clearError')
renderWithRouterAndChakra(
<OfflineBoundary isOnline={true}>
<ThrowingComponent />
</OfflineBoundary>
)
expect(screen.getByRole('img', {hidden: true})).toBeInTheDocument()
expect(
screen.getByRole('heading', {name: /you are currently offline/i})
).toBeInTheDocument()
const retryButton = screen.getByRole('button', {name: /retry connection/i})
await act(async () => {
await user.click(retryButton)
})
expect(clearErrorSpy).toHaveBeenCalled()
clearErrorSpy.mockRestore()
})
test('should derive state from a chunk load error', () => {
const derived = UnwrappedOfflineBoundary.getDerivedStateFromError(
new ChunkLoadError('test')
)
expect(derived).toEqual({chunkLoadError: true})
})
})