From f984e539705f783cd1411b6fadd4e859ce1c724a Mon Sep 17 00:00:00 2001 From: Loonride Date: Fri, 28 Jun 2019 16:53:16 -0500 Subject: [PATCH] test(client): added clientMode option test (#2078) --- test/ports-map.js | 1 + test/server/clientMode-option.test.js | 102 ++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 test/server/clientMode-option.test.js diff --git a/test/ports-map.js b/test/ports-map.js index 5031f898be..6c56f8100c 100644 --- a/test/ports-map.js +++ b/test/ports-map.js @@ -39,6 +39,7 @@ const portsList = { WebsocketClient: 1, WebsocketServer: 1, ClientMode: 1, + 'clientMode-option': 1, }; let startPort = 8079; diff --git a/test/server/clientMode-option.test.js b/test/server/clientMode-option.test.js new file mode 100644 index 0000000000..05f51e5201 --- /dev/null +++ b/test/server/clientMode-option.test.js @@ -0,0 +1,102 @@ +'use strict'; + +const config = require('../fixtures/simple-config/webpack.config'); +const port = require('../ports-map')['clientMode-option']; + +describe('clientMode option', () => { + let mockedTestServer; + let testServer; + let getSocketClientPath; + + const clientModes = [ + { + title: 'as a string ("sockjs")', + clientMode: 'sockjs', + shouldThrow: false, + }, + { + title: 'as a path ("sockjs")', + clientMode: require.resolve('../../client-src/clients/SockJSClient'), + shouldThrow: false, + }, + { + title: 'as a nonexistent path', + clientMode: '/bad/path/to/implementation', + shouldThrow: true, + }, + ]; + + describe('is passed to getSocketClientPath correctly', () => { + beforeEach(() => { + jest.mock('../../lib/utils/getSocketClientPath'); + // eslint-disable-next-line global-require + getSocketClientPath = require('../../lib/utils/getSocketClientPath'); + }); + + afterEach((done) => { + jest.resetAllMocks(); + jest.resetModules(); + + mockedTestServer.close(done); + }); + + clientModes.forEach((data) => { + it(data.title, (done) => { + // eslint-disable-next-line global-require + mockedTestServer = require('../helpers/test-server'); + mockedTestServer.start( + config, + { + inline: true, + clientMode: data.clientMode, + port, + }, + () => { + expect(getSocketClientPath.mock.calls.length).toEqual(1); + expect(getSocketClientPath.mock.calls[0].length).toEqual(1); + expect(getSocketClientPath.mock.calls[0][0].clientMode).toEqual( + data.clientMode + ); + done(); + } + ); + }); + }); + }); + + describe('passed to server', () => { + beforeAll(() => { + jest.unmock('../../lib/utils/getSocketClientPath'); + // eslint-disable-next-line global-require + testServer = require('../helpers/test-server'); + }); + + afterEach((done) => { + testServer.close(done); + }); + + clientModes.forEach((data) => { + it(`${data.title} ${ + data.shouldThrow ? 'should throw' : 'should not throw' + }`, (done) => { + const res = () => { + testServer.start( + config, + { + inline: true, + clientMode: data.clientMode, + port, + }, + done + ); + }; + if (data.shouldThrow) { + expect(res).toThrow(/clientMode must be a string/); + done(); + } else { + expect(res).not.toThrow(); + } + }); + }); + }); +});