-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy patherrorHandling.test.ts
150 lines (132 loc) · 4.55 KB
/
errorHandling.test.ts
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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 { BrowserWindow, dialog, WebContents } from 'electron';
import { AllowedFrontendChannels } from '../../../shared/ipc-channels';
import { SendErrorInformationArgs } from '../../../shared/shared-types';
import { loadInputAndOutputFromFilePath } from '../../input/importFromFile';
import {
createVoidListenerCallbackWithErrorHandling,
getMessageBoxContentForErrorsWrapper,
getMessageBoxForErrors,
} from '../errorHandling';
jest.mock('electron', () => ({
dialog: {
showMessageBox: jest.fn(() => {
return Promise.resolve({
response: 0,
});
}),
},
app: { exit: jest.fn(), getName: jest.fn(), getVersion: jest.fn() },
}));
jest.mock('../../input/importFromFile', () => ({
loadInputAndOutputFromFilePath: jest.fn(),
}));
jest.mock('../../main/listeners', () => ({
getOpenFileListener: jest.fn(() => jest.fn()),
}));
describe('error handling', () => {
describe('createListenerCallbackWithErrorHandling', () => {
it('returns a wrapper that calls the input function with the same parameters', async () => {
const mainWindow = {
webContents: { send: jest.fn() },
} as unknown as BrowserWindow;
const testFunction = jest.fn();
const testArgs = {
arg1: '1',
arg2: true,
};
await createVoidListenerCallbackWithErrorHandling(
mainWindow,
testFunction,
)(testArgs);
expect(testFunction).toHaveBeenCalledTimes(1);
expect(testFunction).toHaveBeenCalledWith(
expect.objectContaining(testArgs),
);
});
it('shows errors from the input function in a messageBox', async () => {
const mainWindow = {
webContents: { send: jest.fn() },
} as unknown as BrowserWindow;
function testFunction(): void {
throw new Error('TEST_ERROR');
}
await createVoidListenerCallbackWithErrorHandling(
mainWindow,
testFunction,
)();
expect(dialog.showMessageBox).toHaveBeenCalledWith(
expect.objectContaining({
type: 'error',
message: 'Error in app backend: TEST_ERROR',
buttons: ['Reload File', 'Quit'],
}),
);
});
});
describe('getMessageBoxContentForErrors', () => {
it('for backend errors', () => {
const testError = new Error('TEST_ERROR');
const messageBoxContentForBackendErrors =
getMessageBoxContentForErrorsWrapper(
true,
testError.stack,
)(testError.message);
expect(messageBoxContentForBackendErrors.detail).toContain(
'Error: TEST_ERROR',
);
expect(messageBoxContentForBackendErrors.message).toBe(
'Error in app backend: TEST_ERROR',
);
});
it('for frontend errors', () => {
const testError = new Error('TEST_ERROR');
const messageBoxContentForBackendErrors =
getMessageBoxContentForErrorsWrapper(
false,
testError.stack,
)(testError.message);
expect(messageBoxContentForBackendErrors.detail).toContain(
'Error: TEST_ERROR',
);
expect(messageBoxContentForBackendErrors.message).toBe(
'Error in app frontend: TEST_ERROR',
);
});
});
describe('getMessageBoxForErrors', () => {
it('returns a messageBox', async () => {
const sendErrorInformationArgs: SendErrorInformationArgs = {
error: { message: 'errorMessage', name: 'Error' },
errorInfo: { componentStack: 'componentStack' },
};
const mockCallback = jest.fn();
const mainWindow = {
webContents: { send: mockCallback as unknown } as WebContents,
} as unknown as BrowserWindow;
await getMessageBoxForErrors(
sendErrorInformationArgs.error.message,
sendErrorInformationArgs.errorInfo.componentStack,
mainWindow,
false,
);
expect(dialog.showMessageBox).toHaveBeenCalledWith(
expect.objectContaining({
type: 'error',
message: 'Error in app frontend: errorMessage',
detail: 'Stack trace: componentStack',
buttons: ['Reload File', 'Quit'],
}),
);
expect(mockCallback.mock.calls).toHaveLength(1);
expect(mockCallback.mock.calls[0][0]).toContain(
AllowedFrontendChannels.RestoreFrontend,
);
expect(loadInputAndOutputFromFilePath).toHaveBeenCalled();
});
});
});