Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions test/client/utils/__snapshots__/socket.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.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 mockServerInstance = SockJSClient.mock.instances[0];

// this simulates recieving a message from the server and passing it
// along to the callback of onMessage
mockServerInstance.onMessage.mock.calls[0][0](
JSON.stringify({
type: 'example',
data: 'hello world',
})
);

expect(SockJSClient.mock.calls[0]).toMatchSnapshot();
expect(mockServerInstance.onOpen.mock.calls).toMatchSnapshot();
expect(mockServerInstance.onClose.mock.calls).toMatchSnapshot();
expect(mockServerInstance.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 mockServerInstance =
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
mockServerInstance.onMessage.mock.calls[0][0](
JSON.stringify({
type: 'example',
data: 'hello world',
})
);

expect(
global.__webpack_dev_server_client__.mock.calls[0]
).toMatchSnapshot();
expect(mockServerInstance.onOpen.mock.calls).toMatchSnapshot();
expect(mockServerInstance.onClose.mock.calls).toMatchSnapshot();
expect(mockServerInstance.onMessage.mock.calls).toMatchSnapshot();
expect(mockHandler.mock.calls).toMatchSnapshot();
});
});