Skip to content

Commit aa31d87

Browse files
knagaitsevhiroppy
authored andcommitted
test(client): socket helper tests (#2095)
* test(client): socket helper tests * test(client): rename socket test, rename mock variable * test(client): renamed socket helper test filename
1 parent 9a09420 commit aa31d87

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 1`] = `
4+
Array [
5+
"my.url",
6+
]
7+
`;
8+
9+
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 2`] = `
10+
Array [
11+
Array [
12+
[Function],
13+
],
14+
]
15+
`;
16+
17+
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 3`] = `
18+
Array [
19+
Array [
20+
[Function],
21+
],
22+
]
23+
`;
24+
25+
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 4`] = `
26+
Array [
27+
Array [
28+
[Function],
29+
],
30+
]
31+
`;
32+
33+
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 5`] = `
34+
Array [
35+
Array [
36+
"hello world",
37+
],
38+
]
39+
`;
40+
41+
exports[`socket should use __webpack_dev_server_client__ when set 1`] = `
42+
Array [
43+
"my.url",
44+
]
45+
`;
46+
47+
exports[`socket should use __webpack_dev_server_client__ when set 2`] = `
48+
Array [
49+
Array [
50+
[Function],
51+
],
52+
]
53+
`;
54+
55+
exports[`socket should use __webpack_dev_server_client__ when set 3`] = `
56+
Array [
57+
Array [
58+
[Function],
59+
],
60+
]
61+
`;
62+
63+
exports[`socket should use __webpack_dev_server_client__ when set 4`] = `
64+
Array [
65+
Array [
66+
[Function],
67+
],
68+
]
69+
`;
70+
71+
exports[`socket should use __webpack_dev_server_client__ when set 5`] = `
72+
Array [
73+
Array [
74+
"hello world",
75+
],
76+
]
77+
`;
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'use strict';
2+
3+
describe('socket', () => {
4+
afterEach(() => {
5+
jest.resetAllMocks();
6+
jest.resetModules();
7+
});
8+
9+
it('should default to SockJSClient when no __webpack_dev_server_client__ set', () => {
10+
jest.mock('../../../client/clients/SockJSClient');
11+
// eslint-disable-next-line global-require
12+
const socket = require('../../../client/socket');
13+
// eslint-disable-next-line global-require
14+
const SockJSClient = require('../../../client/clients/SockJSClient');
15+
16+
const mockHandler = jest.fn();
17+
socket('my.url', {
18+
example: mockHandler,
19+
});
20+
21+
const mockClientInstance = SockJSClient.mock.instances[0];
22+
23+
// this simulates recieving a message from the server and passing it
24+
// along to the callback of onMessage
25+
mockClientInstance.onMessage.mock.calls[0][0](
26+
JSON.stringify({
27+
type: 'example',
28+
data: 'hello world',
29+
})
30+
);
31+
32+
expect(SockJSClient.mock.calls[0]).toMatchSnapshot();
33+
expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot();
34+
expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot();
35+
expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot();
36+
expect(mockHandler.mock.calls).toMatchSnapshot();
37+
});
38+
39+
it('should use __webpack_dev_server_client__ when set', () => {
40+
jest.mock('../../../client/clients/SockJSClient');
41+
// eslint-disable-next-line global-require
42+
const socket = require('../../../client/socket');
43+
// eslint-disable-next-line global-require
44+
global.__webpack_dev_server_client__ = require('../../../client/clients/SockJSClient');
45+
46+
const mockHandler = jest.fn();
47+
socket('my.url', {
48+
example: mockHandler,
49+
});
50+
51+
const mockClientInstance =
52+
global.__webpack_dev_server_client__.mock.instances[0];
53+
54+
// this simulates recieving a message from the server and passing it
55+
// along to the callback of onMessage
56+
mockClientInstance.onMessage.mock.calls[0][0](
57+
JSON.stringify({
58+
type: 'example',
59+
data: 'hello world',
60+
})
61+
);
62+
63+
expect(
64+
global.__webpack_dev_server_client__.mock.calls[0]
65+
).toMatchSnapshot();
66+
expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot();
67+
expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot();
68+
expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot();
69+
expect(mockHandler.mock.calls).toMatchSnapshot();
70+
});
71+
});

0 commit comments

Comments
 (0)