-
Notifications
You must be signed in to change notification settings - Fork 428
/
gizmo.spec.js
61 lines (44 loc) · 1.69 KB
/
gizmo.spec.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
49
50
51
52
53
54
55
56
57
58
59
60
61
import gizmo from './gizmo';
jest.useFakeTimers();
describe('gizmo', () => {
let config;
let stopMock;
let consoleErrorSpy;
let windowErrorSpy = jest.fn();
beforeEach(() => {
stopMock = jest.fn();
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
window.onerror = windowErrorSpy;
config = {
logger: console,
stop: stopMock,
window,
};
});
it('should call console.error with right args', () => {
const mogwais = gizmo()(config);
mogwais();
console.error('first error');
expect(consoleErrorSpy).toHaveBeenCalledTimes(1);
expect(consoleErrorSpy).toHaveBeenCalledWith('first error');
});
it('should call window.error with right args', () => {
const mogwais = gizmo()(config);
mogwais();
window.onerror('first error', 'https://foo.bar', 10);
expect(windowErrorSpy).toHaveBeenCalledTimes(1);
expect(windowErrorSpy).toHaveBeenCalledWith('first error', 'https://foo.bar', 10);
});
it('should call stop when maxErrors is reached', () => {
const mogwais = gizmo({ maxErrors: 2 })(config);
mogwais();
console.error('first error');
console.error('second', 'error');
expect(consoleErrorSpy).toHaveBeenCalledTimes(2);
expect(consoleErrorSpy).toHaveBeenNthCalledWith(1, 'first error');
expect(consoleErrorSpy).toHaveBeenNthCalledWith(2, 'second', 'error');
expect(stopMock).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 4);
});
});