|
| 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