Skip to content

Commit f984e53

Browse files
knagaitsevhiroppy
authored andcommitted
test(client): added clientMode option test (#2078)
1 parent 764b24e commit f984e53

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

test/ports-map.js

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const portsList = {
3939
WebsocketClient: 1,
4040
WebsocketServer: 1,
4141
ClientMode: 1,
42+
'clientMode-option': 1,
4243
};
4344

4445
let startPort = 8079;

test/server/clientMode-option.test.js

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict';
2+
3+
const config = require('../fixtures/simple-config/webpack.config');
4+
const port = require('../ports-map')['clientMode-option'];
5+
6+
describe('clientMode option', () => {
7+
let mockedTestServer;
8+
let testServer;
9+
let getSocketClientPath;
10+
11+
const clientModes = [
12+
{
13+
title: 'as a string ("sockjs")',
14+
clientMode: 'sockjs',
15+
shouldThrow: false,
16+
},
17+
{
18+
title: 'as a path ("sockjs")',
19+
clientMode: require.resolve('../../client-src/clients/SockJSClient'),
20+
shouldThrow: false,
21+
},
22+
{
23+
title: 'as a nonexistent path',
24+
clientMode: '/bad/path/to/implementation',
25+
shouldThrow: true,
26+
},
27+
];
28+
29+
describe('is passed to getSocketClientPath correctly', () => {
30+
beforeEach(() => {
31+
jest.mock('../../lib/utils/getSocketClientPath');
32+
// eslint-disable-next-line global-require
33+
getSocketClientPath = require('../../lib/utils/getSocketClientPath');
34+
});
35+
36+
afterEach((done) => {
37+
jest.resetAllMocks();
38+
jest.resetModules();
39+
40+
mockedTestServer.close(done);
41+
});
42+
43+
clientModes.forEach((data) => {
44+
it(data.title, (done) => {
45+
// eslint-disable-next-line global-require
46+
mockedTestServer = require('../helpers/test-server');
47+
mockedTestServer.start(
48+
config,
49+
{
50+
inline: true,
51+
clientMode: data.clientMode,
52+
port,
53+
},
54+
() => {
55+
expect(getSocketClientPath.mock.calls.length).toEqual(1);
56+
expect(getSocketClientPath.mock.calls[0].length).toEqual(1);
57+
expect(getSocketClientPath.mock.calls[0][0].clientMode).toEqual(
58+
data.clientMode
59+
);
60+
done();
61+
}
62+
);
63+
});
64+
});
65+
});
66+
67+
describe('passed to server', () => {
68+
beforeAll(() => {
69+
jest.unmock('../../lib/utils/getSocketClientPath');
70+
// eslint-disable-next-line global-require
71+
testServer = require('../helpers/test-server');
72+
});
73+
74+
afterEach((done) => {
75+
testServer.close(done);
76+
});
77+
78+
clientModes.forEach((data) => {
79+
it(`${data.title} ${
80+
data.shouldThrow ? 'should throw' : 'should not throw'
81+
}`, (done) => {
82+
const res = () => {
83+
testServer.start(
84+
config,
85+
{
86+
inline: true,
87+
clientMode: data.clientMode,
88+
port,
89+
},
90+
done
91+
);
92+
};
93+
if (data.shouldThrow) {
94+
expect(res).toThrow(/clientMode must be a string/);
95+
done();
96+
} else {
97+
expect(res).not.toThrow();
98+
}
99+
});
100+
});
101+
});
102+
});

0 commit comments

Comments
 (0)