-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathErrorBoundary.test.tsx
62 lines (50 loc) · 1.98 KB
/
ErrorBoundary.test.tsx
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
// SPDX-FileCopyrightText: Meta Platforms, Inc. and its affiliates
// SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
// SPDX-FileCopyrightText: Nico Carl <nicocarl@protonmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import { screen } from '@testing-library/react';
import { AllowedFrontendChannels } from '../../../../shared/ipc-channels';
import { initialResourceState } from '../../../state/reducers/resource-reducer';
import { initialViewState } from '../../../state/reducers/view-reducer';
import { renderComponent } from '../../../test-helpers/render';
import { ErrorBoundary } from '../ErrorBoundary';
describe('ErrorBoundary', () => {
function TestComponent(props: { throws: boolean }): JSX.Element {
if (props.throws) {
throw new Error();
}
return <div>{'Test'}</div>;
}
it('renders its children', () => {
renderComponent(
<ErrorBoundary>
<TestComponent throws={false} />
</ErrorBoundary>,
);
expect(window.electronAPI.openFile).toHaveBeenCalledTimes(0);
expect(window.electronAPI.on).toHaveBeenCalledTimes(1);
screen.getByText('Test');
});
it('renders fallback and restores state', () => {
// we expect warnings that we do not want to see
jest.spyOn(console, 'error').mockImplementation();
const { store } = renderComponent(
<ErrorBoundary>
<TestComponent throws={true} />
</ErrorBoundary>,
);
const expectedNumberOfCalls = 3;
expect(window.electronAPI.sendErrorInformation).toHaveBeenCalledTimes(
expectedNumberOfCalls,
);
expect(window.electronAPI.on).toHaveBeenCalledTimes(1);
expect(window.electronAPI.on).toHaveBeenCalledWith(
AllowedFrontendChannels.RestoreFrontend,
expect.anything(),
);
expect(store.getState().resourceState).toMatchObject(initialResourceState);
expect(store.getState().viewState).toMatchObject(initialViewState);
expect(screen.queryByText('Test')).not.toBeInTheDocument();
});
});