Skip to content

Commit 79325e5

Browse files
hiroppyevilebottnawi
authored andcommitted
test(client): add tests for default/index.js (#1957)
1 parent d346a53 commit 79325e5

File tree

4 files changed

+320
-4
lines changed

4 files changed

+320
-4
lines changed

client-src/default/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (typeof window !== 'undefined') {
3434
options.hotReload = qs.indexOf('hotreload=false') === -1;
3535
}
3636

37-
const onSocketMsg = {
37+
const onSocketMessage = {
3838
hot() {
3939
options.hot = true;
4040
log.info('[WDS] Hot Module Replacement enabled.');
@@ -141,4 +141,4 @@ const onSocketMsg = {
141141
},
142142
};
143143

144-
socket(socketUrl, onSocketMsg);
144+
socket(socketUrl, onSocketMessage);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`index should run onSocketMessage.close 1`] = `"[WDS] Disconnected!"`;
4+
5+
exports[`index should run onSocketMessage.close 2`] = `"Close"`;
6+
7+
exports[`index should run onSocketMessage.error 1`] = `"error!!"`;
8+
9+
exports[`index should run onSocketMessage.errors 1`] = `"[WDS] Errors while compiling. Reload prevented."`;
10+
11+
exports[`index should run onSocketMessage.errors 2`] = `"Errors"`;
12+
13+
exports[`index should run onSocketMessage.errors 3`] = `
14+
Array [
15+
Array [
16+
"error1",
17+
],
18+
Array [
19+
"error2",
20+
],
21+
Array [
22+
"error3",
23+
],
24+
]
25+
`;
26+
27+
exports[`index should run onSocketMessage.hot 1`] = `"[WDS] Hot Module Replacement enabled."`;
28+
29+
exports[`index should run onSocketMessage.invalid 1`] = `"[WDS] App updated. Recompiling..."`;
30+
31+
exports[`index should run onSocketMessage.invalid 2`] = `"Invalid"`;
32+
33+
exports[`index should run onSocketMessage.liveReload 1`] = `"[WDS] Live Reloading enabled."`;
34+
35+
exports[`index should run onSocketMessage.ok 1`] = `"Ok"`;
36+
37+
exports[`index should run onSocketMessage.ok 2`] = `
38+
Object {
39+
"hot": false,
40+
"hotReload": true,
41+
"initial": false,
42+
"liveReload": false,
43+
"useErrorOverlay": false,
44+
"useProgress": false,
45+
"useWarningOverlay": false,
46+
}
47+
`;
48+
49+
exports[`index should run onSocketMessage.progress and onSocketMessage['progress-update'] 1`] = `"Progress"`;
50+
51+
exports[`index should run onSocketMessage.progress and onSocketMessage['progress-update'] 2`] = `"[WDS] 12% - mock-msg."`;
52+
53+
exports[`index should run onSocketMessage.warnings 1`] = `"[WDS] Warnings while compiling."`;
54+
55+
exports[`index should run onSocketMessage.warnings 2`] = `"Warnings"`;
56+
57+
exports[`index should run onSocketMessage.warnings 3`] = `
58+
Array [
59+
Array [
60+
"warn1",
61+
],
62+
Array [
63+
"warn2",
64+
],
65+
Array [
66+
"warn3",
67+
],
68+
]
69+
`;
70+
71+
exports[`index should run onSocketMessage['content-changed'] 1`] = `"[WDS] Content base changed. Reloading..."`;
72+
73+
exports[`index should run onSocketMessage['still-ok'] 1`] = `"[WDS] Nothing changed."`;
74+
75+
exports[`index should run onSocketMessage['still-ok'] 2`] = `"StillOk"`;
76+
77+
exports[`index should set arguments into socket function 1`] = `
78+
Array [
79+
"mock-url",
80+
Object {
81+
"close": [Function],
82+
"content-changed": [Function],
83+
"error": [Function],
84+
"errors": [Function],
85+
"hash": [Function],
86+
"hot": [Function],
87+
"invalid": [Function],
88+
"liveReload": [Function],
89+
"log-level": [Function],
90+
"ok": [Function],
91+
"overlay": [Function],
92+
"progress": [Function],
93+
"progress-update": [Function],
94+
"still-ok": [Function],
95+
"warnings": [Function],
96+
},
97+
]
98+
`;

test/client/default/index.test.js

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
global-require,
5+
no-undefined
6+
*/
7+
/* global self */
8+
9+
describe('index', () => {
10+
let log;
11+
let socket;
12+
let overlay;
13+
let reloadApp;
14+
let sendMessage;
15+
let onSocketMessage;
16+
const locationValue = self.location;
17+
const resourceQueryValue = global.__resourceQuery;
18+
19+
beforeEach(() => {
20+
global.__resourceQuery = 'foo';
21+
self.location.reload = jest.fn();
22+
23+
// log
24+
jest.setMock('../../../client-src/default/utils/log.js', {
25+
log: {
26+
info: jest.fn(),
27+
warn: jest.fn(),
28+
error: jest.fn(),
29+
},
30+
});
31+
log = require('../../../client-src/default/utils/log');
32+
33+
// socket
34+
jest.setMock('../../../client-src/default/socket.js', jest.fn());
35+
socket = require('../../../client-src/default/socket');
36+
37+
// overlay
38+
jest.setMock('../../../client-src/default/overlay.js', {
39+
clear: jest.fn(),
40+
showMessage: jest.fn(),
41+
});
42+
overlay = require('../../../client-src/default/overlay');
43+
44+
// reloadApp
45+
jest.setMock('../../../client-src/default/utils/reloadApp.js', jest.fn());
46+
reloadApp = require('../../../client-src/default/utils/reloadApp');
47+
48+
// sendMessage
49+
jest.setMock('../../../client-src/default/utils/sendMessage.js', jest.fn());
50+
sendMessage = require('../../../client-src/default/utils/sendMessage');
51+
52+
// createSocketUrl
53+
jest.setMock(
54+
'../../../client-src/default/utils/createSocketUrl.js',
55+
() => 'mock-url'
56+
);
57+
58+
require('../../../client-src/default');
59+
onSocketMessage = socket.mock.calls[0][1];
60+
});
61+
62+
afterEach(() => {
63+
global.__resourceQuery = resourceQueryValue;
64+
Object.assign(self, locationValue);
65+
jest.resetAllMocks();
66+
jest.resetModules();
67+
});
68+
69+
test('should set arguments into socket function', () => {
70+
expect(socket.mock.calls[0]).toMatchSnapshot();
71+
});
72+
73+
test('should run onSocketMessage.hot', () => {
74+
onSocketMessage.hot();
75+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
76+
});
77+
78+
test('should run onSocketMessage.liveReload', () => {
79+
onSocketMessage.liveReload();
80+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
81+
});
82+
83+
test('should run onSocketMessage.invalid', () => {
84+
onSocketMessage.invalid();
85+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
86+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
87+
expect(overlay.clear).not.toBeCalled();
88+
89+
// change flags
90+
onSocketMessage.overlay(true);
91+
onSocketMessage.invalid();
92+
expect(overlay.clear).toBeCalled();
93+
});
94+
95+
test("should run onSocketMessage['still-ok']", () => {
96+
onSocketMessage['still-ok']();
97+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
98+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
99+
expect(overlay.clear).not.toBeCalled();
100+
101+
// change flags
102+
onSocketMessage.overlay(true);
103+
onSocketMessage['still-ok']();
104+
expect(overlay.clear).toBeCalled();
105+
});
106+
107+
// TODO: need to mock require.context
108+
test.skip("should run onSocketMessage['log-level']", () => {
109+
onSocketMessage['log-level']();
110+
});
111+
112+
test("should run onSocketMessage.progress and onSocketMessage['progress-update']", () => {
113+
onSocketMessage.progress(false);
114+
onSocketMessage['progress-update']({
115+
msg: 'mock-msg',
116+
percent: '12',
117+
});
118+
expect(log.log.info).not.toBeCalled();
119+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
120+
121+
onSocketMessage.progress(true);
122+
onSocketMessage['progress-update']({
123+
msg: 'mock-msg',
124+
percent: '12',
125+
});
126+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
127+
});
128+
129+
test('should run onSocketMessage.overlay with an argument is Object', () => {
130+
onSocketMessage.overlay({
131+
warnings: true,
132+
errors: true,
133+
});
134+
135+
// check if flags have changed
136+
onSocketMessage.invalid();
137+
expect(overlay.clear).toBeCalled();
138+
});
139+
140+
test('should run onSocketMessage.ok', () => {
141+
{
142+
const res = onSocketMessage.ok();
143+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
144+
expect(res).toEqual(false);
145+
}
146+
147+
// change flags
148+
{
149+
onSocketMessage.errors([]);
150+
onSocketMessage.hash('mock-hash');
151+
152+
const res = onSocketMessage.ok();
153+
expect(reloadApp).toBeCalled();
154+
expect(reloadApp.mock.calls[0][0]).toMatchSnapshot();
155+
expect(res).toEqual(undefined);
156+
}
157+
});
158+
159+
test("should run onSocketMessage['content-changed']", () => {
160+
onSocketMessage['content-changed']();
161+
162+
expect(log.log.info.mock.calls[0][0]).toMatchSnapshot();
163+
expect(self.location.reload).toBeCalled();
164+
});
165+
166+
test('should run onSocketMessage.warnings', () => {
167+
{
168+
const res = onSocketMessage.warnings([
169+
'warn1',
170+
'\u001B[4mwarn2\u001B[0m',
171+
'warn3',
172+
]);
173+
expect(res).toEqual(false);
174+
175+
expect(log.log.warn.mock.calls[0][0]).toMatchSnapshot();
176+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
177+
expect(log.log.warn.mock.calls.splice(1)).toMatchSnapshot();
178+
}
179+
180+
// change flags
181+
{
182+
onSocketMessage.overlay({
183+
warnings: true,
184+
});
185+
const res = onSocketMessage.warnings([]);
186+
expect(res).toEqual(undefined);
187+
188+
expect(overlay.showMessage).toBeCalled();
189+
expect(reloadApp).toBeCalled();
190+
}
191+
});
192+
193+
test('should run onSocketMessage.errors', () => {
194+
onSocketMessage.errors(['error1', '\u001B[4error2\u001B[0m', 'error3']);
195+
196+
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
197+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
198+
expect(log.log.error.mock.calls.splice(1)).toMatchSnapshot();
199+
200+
// change flags
201+
onSocketMessage.overlay({
202+
errors: true,
203+
});
204+
onSocketMessage.errors([]);
205+
expect(overlay.showMessage).toBeCalled();
206+
});
207+
208+
test('should run onSocketMessage.error', () => {
209+
onSocketMessage.error('error!!');
210+
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
211+
});
212+
213+
test('should run onSocketMessage.close', () => {
214+
onSocketMessage.close();
215+
expect(log.log.error.mock.calls[0][0]).toMatchSnapshot();
216+
expect(sendMessage.mock.calls[0][0]).toMatchSnapshot();
217+
});
218+
});

test/client/default/utils/reloadApp.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('reloadApp', () => {
88
let locationValue;
99

1010
beforeEach(() => {
11-
locationValue = self;
11+
locationValue = self.location;
1212
self.postMessage = jest.fn();
1313
self.location.reload = jest.fn();
1414

@@ -27,7 +27,7 @@ describe('reloadApp', () => {
2727
});
2828

2929
afterEach(() => {
30-
Object.assign(self.location, locationValue);
30+
Object.assign(self, locationValue);
3131
jest.resetAllMocks();
3232
jest.resetModules();
3333
});

0 commit comments

Comments
 (0)