-
Notifications
You must be signed in to change notification settings - Fork 428
/
alert.spec.js
94 lines (69 loc) · 2.61 KB
/
alert.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
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
import alert from './alert';
describe('alert', () => {
const chanceBoolMock = jest.fn();
const chanceSentenceMock = jest.fn();
let config;
let consoleMock;
let chanceMock;
beforeEach(() => {
consoleMock = { warn: jest.fn() };
chanceMock = {
bool: chanceBoolMock,
sentence: chanceSentenceMock,
};
config = {
logger: consoleMock,
randomizer: chanceMock,
window,
};
});
it('should call logger warn when window.alert is call', () => {
const mogwais = alert()(config);
mogwais();
window.alert('new alert');
expect(consoleMock.warn).toHaveBeenCalledTimes(1);
expect(consoleMock.warn).toHaveBeenCalledWith('mogwai ', 'alert ', 'new alert', 'alert');
});
it('should call logger warn when window.confirm is call', () => {
const mogwais = alert()(config);
mogwais();
window.confirm('new confirm');
expect(consoleMock.warn).toHaveBeenCalledTimes(1);
expect(consoleMock.warn).toHaveBeenCalledWith('mogwai ', 'alert ', 'new confirm', 'confirm');
});
it('should call logger warn when window.prompt is call', () => {
const mogwais = alert()(config);
mogwais();
window.prompt('new prompt');
expect(consoleMock.warn).toHaveBeenCalledTimes(1);
expect(consoleMock.warn).toHaveBeenCalledWith('mogwai ', 'alert ', 'new prompt', 'prompt');
});
it('should call randomize bool when window.confirm is call', () => {
const mogwais = alert()(config);
mogwais();
window.confirm('new confirm');
expect(chanceBoolMock).toHaveBeenCalledTimes(1);
});
it('should call randomize sentence when window.prompt is call', () => {
const mogwais = alert()(config);
mogwais();
window.prompt('new prompt');
expect(chanceSentenceMock).toHaveBeenCalledTimes(1);
});
it('should cleanup the window prop when cleanUp function is call', () => {
jest.spyOn(window, 'alert').mockImplementation();
const mogwais = alert()(config);
mogwais();
window.alert('new alert');
expect(consoleMock.warn).toHaveBeenCalledTimes(1);
mogwais.cleanUp();
window.alert('new alert');
expect(consoleMock.warn).toHaveBeenCalledTimes(1);
});
it('should not override the window object when logger is not defined ', () => {
const mogwais = alert()(config);
mogwais();
window.alert('new alert');
expect(chanceSentenceMock).toHaveBeenCalledTimes(0);
});
});