Add `close` event to MetroClient

Reviewed By: rafeca

Differential Revision: D7083989

fbshipit-source-id: 7a4d4913d32e58a1efc6210dd7118b03b6ad6b2b
This commit is contained in:
Christoph Nakazawa 2018-02-26 07:00:10 -08:00 committed by Facebook Github Bot
parent 063a6867b1
commit f179352292
2 changed files with 12 additions and 1 deletions

View File

@ -35,6 +35,9 @@ class MetroClient extends EventEmitter {
this._ws.onerror = error => {
this.emit('connection-error', error);
};
this._ws.onclose = () => {
this.emit('close');
};
this._ws.onmessage = message => {
const data = JSON.parse(message.data);
switch (data.type) {

View File

@ -17,7 +17,12 @@ global.WebSocket = jest.fn(() => {
mockSocket = {
onerror: jest.fn(),
onmessage: jest.fn(),
close: jest.fn(),
onclose: jest.fn(),
close: jest.fn(() => {
if (mockSocket) {
mockSocket.onclose();
}
}),
mockEmit: (type, data) => {
if (mockSocket) {
if (type === 'error') {
@ -41,10 +46,12 @@ test('connects to a WebSocket and listens to messages', () => {
};
const mockErrorCallback = jest.fn(data => expect(data).toEqual(mockError));
const mockUpdateStartCallback = jest.fn();
const mockCloseCallback = jest.fn();
expect(mockSocket).toBeNull();
client.on('connection-error', mockErrorCallback);
client.on('update-start', mockUpdateStartCallback);
client.on('close', mockCloseCallback);
client.enable();
if (!mockSocket) {
throw new Error('mockSocket was not set when opening the connection.');
@ -64,4 +71,5 @@ test('connects to a WebSocket and listens to messages', () => {
expect(mockSocket.close).not.toBeCalled();
client.disable();
expect(mockSocket.close).toBeCalled();
expect(mockCloseCallback).toBeCalled();
});