Skip to content

Commit

Permalink
test(client): socket helper tests (#2095)
Browse files Browse the repository at this point in the history
* test(client): socket helper tests

* test(client): rename socket test, rename mock variable

* test(client): renamed socket helper test filename
  • Loading branch information
knagaitsev authored and hiroppy committed Jul 4, 2019
1 parent 9a09420 commit aa31d87
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/client/utils/__snapshots__/socket-helper.test.js.snap
Original file line number Diff line number Diff line change
@@ -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",
],
]
`;
71 changes: 71 additions & 0 deletions test/client/utils/socket-helper.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit aa31d87

Please sign in to comment.