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