-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
9a09420
commit aa31d87
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
test/client/utils/__snapshots__/socket-helper.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |