From aa31d87a0d46ae18162afa5d755e3a27304e6c63 Mon Sep 17 00:00:00 2001 From: Loonride Date: Thu, 4 Jul 2019 13:13:11 -0500 Subject: [PATCH] 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 --- .../__snapshots__/socket-helper.test.js.snap | 77 +++++++++++++++++++ test/client/utils/socket-helper.test.js | 71 +++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 test/client/utils/__snapshots__/socket-helper.test.js.snap create mode 100644 test/client/utils/socket-helper.test.js diff --git a/test/client/utils/__snapshots__/socket-helper.test.js.snap b/test/client/utils/__snapshots__/socket-helper.test.js.snap new file mode 100644 index 0000000000..e971ef199f --- /dev/null +++ b/test/client/utils/__snapshots__/socket-helper.test.js.snap @@ -0,0 +1,77 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 1`] = ` +Array [ + "my.url", +] +`; + +exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 2`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 3`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 4`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 5`] = ` +Array [ + Array [ + "hello world", + ], +] +`; + +exports[`socket should use __webpack_dev_server_client__ when set 1`] = ` +Array [ + "my.url", +] +`; + +exports[`socket should use __webpack_dev_server_client__ when set 2`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should use __webpack_dev_server_client__ when set 3`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should use __webpack_dev_server_client__ when set 4`] = ` +Array [ + Array [ + [Function], + ], +] +`; + +exports[`socket should use __webpack_dev_server_client__ when set 5`] = ` +Array [ + Array [ + "hello world", + ], +] +`; diff --git a/test/client/utils/socket-helper.test.js b/test/client/utils/socket-helper.test.js new file mode 100644 index 0000000000..49da4a3dd5 --- /dev/null +++ b/test/client/utils/socket-helper.test.js @@ -0,0 +1,71 @@ +'use strict'; + +describe('socket', () => { + afterEach(() => { + jest.resetAllMocks(); + jest.resetModules(); + }); + + it('should default to SockJSClient when no __webpack_dev_server_client__ set', () => { + jest.mock('../../../client/clients/SockJSClient'); + // eslint-disable-next-line global-require + const socket = require('../../../client/socket'); + // eslint-disable-next-line global-require + const SockJSClient = require('../../../client/clients/SockJSClient'); + + const mockHandler = jest.fn(); + socket('my.url', { + example: mockHandler, + }); + + const mockClientInstance = SockJSClient.mock.instances[0]; + + // this simulates recieving a message from the server and passing it + // along to the callback of onMessage + mockClientInstance.onMessage.mock.calls[0][0]( + JSON.stringify({ + type: 'example', + data: 'hello world', + }) + ); + + expect(SockJSClient.mock.calls[0]).toMatchSnapshot(); + expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot(); + expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot(); + expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot(); + expect(mockHandler.mock.calls).toMatchSnapshot(); + }); + + it('should use __webpack_dev_server_client__ when set', () => { + jest.mock('../../../client/clients/SockJSClient'); + // eslint-disable-next-line global-require + const socket = require('../../../client/socket'); + // eslint-disable-next-line global-require + global.__webpack_dev_server_client__ = require('../../../client/clients/SockJSClient'); + + const mockHandler = jest.fn(); + socket('my.url', { + example: mockHandler, + }); + + const mockClientInstance = + global.__webpack_dev_server_client__.mock.instances[0]; + + // this simulates recieving a message from the server and passing it + // along to the callback of onMessage + mockClientInstance.onMessage.mock.calls[0][0]( + JSON.stringify({ + type: 'example', + data: 'hello world', + }) + ); + + expect( + global.__webpack_dev_server_client__.mock.calls[0] + ).toMatchSnapshot(); + expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot(); + expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot(); + expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot(); + expect(mockHandler.mock.calls).toMatchSnapshot(); + }); +});