-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
reloadApp.test.js
102 lines (85 loc) · 2.62 KB
/
reloadApp.test.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
95
96
97
98
99
100
101
102
'use strict';
/* global self */
describe('reloadApp', () => {
let reloadApp;
let log;
let locationValue;
beforeEach(() => {
locationValue = self.location;
self.postMessage = jest.fn();
self.location.reload = jest.fn();
jest.mock('loglevel');
// eslint-disable-next-line global-require
log = require('loglevel');
log.getLogger.mockImplementation(() => {
return {
info: jest.fn(),
setDefaultLevel: jest.fn(),
};
});
// eslint-disable-next-line global-require
reloadApp = require('../../../client-src/default/utils/reloadApp').default;
});
afterEach(() => {
Object.assign(self, locationValue);
jest.resetAllMocks();
jest.resetModules();
});
test('should do nothing when isUnloading is true or hotReload is false', () => {
// eslint-disable-next-line no-undefined
expect(reloadApp({}, { isUnloading: false })).toEqual(undefined);
expect(log.getLogger.mock.results[0].value.info).not.toBeCalled();
expect(reloadApp({ hotReload: false }, { isUnloading: false })).toEqual(
// eslint-disable-next-line no-undefined
undefined
);
expect(log.getLogger.mock.results[0].value.info).not.toBeCalled();
});
test('should run hot', () => {
jest.mock('webpack/hot/emitter');
// eslint-disable-next-line global-require
const emitter = require('webpack/hot/emitter');
emitter.emit = jest.fn();
reloadApp(
{ hot: true, hotReload: true },
{ isUnloading: false, currentHash: 'hash' }
);
expect(
log.getLogger.mock.results[0].value.info.mock.calls[0][0]
).toMatchSnapshot();
expect(emitter.emit.mock.calls[0][0]).toMatchSnapshot();
expect(self.postMessage.mock.calls[0]).toMatchSnapshot();
});
test('should run liveReload when protocol is about:', (done) => {
Object.defineProperty(self, 'location', {
value: {
...self.location,
protocol: 'about:',
},
});
reloadApp(
{ hot: false, hotReload: true, liveReload: true },
{ isUnloading: false }
);
setTimeout(() => {
expect(
log.getLogger.mock.results[0].value.info.mock.calls[0][0]
).toMatchSnapshot();
expect(self.location.reload).toBeCalled();
done();
});
});
test('should run liveReload when protocol is http:', (done) => {
reloadApp(
{ hot: false, hotReload: true, liveReload: true },
{ isUnloading: false }
);
setTimeout(() => {
expect(
log.getLogger.mock.results[0].value.info.mock.calls[0][0]
).toMatchSnapshot();
expect(self.location.reload).toBeCalled();
done();
});
});
});