fix tests, add golden command (#4486)

Co-authored-by: Jeremy Letto <jeremy.letto@datasite.com>
This commit is contained in:
Jeremy Letto 2021-11-26 15:55:12 -06:00 committed by GitHub
parent 6ce346af4a
commit 6dc9f004ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 481 additions and 272 deletions

View file

@ -6,6 +6,9 @@ module.exports = {
"@typescript-eslint"
],
"ignorePatterns": ["node_modules/*", "build/*", "public/pb/*"],
"env": {
"jest": true
},
"rules": {
"array-bracket-spacing": ["error", "never"],
"arrow-spacing": ["error", {"before": true, "after": true}],

File diff suppressed because it is too large Load diff

View file

@ -33,10 +33,12 @@
"postinstall:default": "./copy_shared_files.sh",
"start": "cross-env ESLINT_NO_DEV_ERRORS=true react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"test": "cross-env CI=true react-scripts test",
"test:watch": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint \"./**/*.{ts,tsx}\"",
"lint:fix": "eslint \"./**/*.{ts,tsx}\" --fix"
"lint:fix": "eslint \"./**/*.{ts,tsx}\" --fix",
"golden": "npm run lint && npm run test"
},
"eslintConfig": {
"extends": "react-app"

View file

@ -8,11 +8,15 @@ describe('RoomCommands', () => {
let sendRoomCommandSpy;
beforeEach(() => {
sendRoomCommandSpy = spyOn(webClient.protobuf, 'sendRoomCommand');
sendRoomCommandSpy = jest.spyOn(webClient.protobuf, 'sendRoomCommand').mockImplementation(() => {});
webClient.protobuf.controller.RoomCommand = { create: args => args };
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('roomSay', () => {
beforeEach(() => {
webClient.protobuf.controller.Command_RoomSay = { create: args => args };
@ -50,18 +54,18 @@ describe('RoomCommands', () => {
expect(webClient.protobuf.sendRoomCommand).toHaveBeenCalledWith(
roomId,
{ '.Command_LeaveRoom.ext': {} },
jasmine.any(Function)
expect.any(Function)
);
});
it('should call RoomPersistence.leaveRoom if RespOk', () => {
const RespOk = 'ok';
webClient.protobuf.controller.Response = { ResponseCode: { RespOk } };
sendRoomCommandSpy.and.callFake((_, __, callback) => {
sendRoomCommandSpy.mockImplementation((_, __, callback) => {
callback({ responseCode: RespOk })
});
spyOn(RoomPersistence, 'leaveRoom');
jest.spyOn(RoomPersistence, 'leaveRoom').mockImplementation(() => {});
RoomCommands.leaveRoom(roomId);

View file

@ -1,34 +1,37 @@
import { StatusEnum } from 'types';
import { StatusEnum, WebSocketConnectReason } from 'types';
import { SessionCommands } from './SessionCommands';
import { RoomPersistence, SessionPersistence } from '../persistence';
import webClient from '../WebClient';
import { WebSocketConnectReason } from '../services/WebSocketService';
import { AccountActivationParams, ServerRegisterParams } from '../../store';
describe('SessionCommands', () => {
const roomId = 1;
let sendModeratorCommandSpy;
let sendSessionCommandSpy;
let MockSessionCommands;
beforeEach(() => {
spyOn(SessionCommands, 'updateStatus').and.callThrough();
spyOn(webClient, 'updateStatus');
spyOn(console, 'error');
jest.spyOn(SessionCommands, 'updateStatus');
jest.spyOn(webClient, 'updateStatus').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
sendModeratorCommandSpy = spyOn(webClient.protobuf, 'sendModeratorCommand');
sendSessionCommandSpy = spyOn(webClient.protobuf, 'sendSessionCommand');
sendModeratorCommandSpy = jest.spyOn(webClient.protobuf, 'sendModeratorCommand').mockImplementation(() => {});
sendSessionCommandSpy = jest.spyOn(webClient.protobuf, 'sendSessionCommand').mockImplementation(() => {});
webClient.protobuf.controller.ModeratorCommand = { create: args => args };
webClient.protobuf.controller.SessionCommand = { create: args => args };
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('connect', () => {
let options;
beforeEach(() => {
spyOn(webClient, 'connect');
jest.spyOn(webClient, 'connect').mockImplementation(() => {});
options = {
host: 'host',
port: 'port',
@ -69,13 +72,10 @@ describe('SessionCommands', () => {
describe('disconnect', () => {
it('should call SessionCommands.updateStatus and webClient.disconnect', () => {
spyOn(webClient, 'disconnect');
jest.spyOn(webClient, 'disconnect');
SessionCommands.disconnect();
expect(SessionCommands.updateStatus).toHaveBeenCalled();
expect(SessionCommands.updateStatus).toHaveBeenCalledWith(StatusEnum.DISCONNECTING, 'Disconnecting...');
expect(webClient.disconnect).toHaveBeenCalled();
});
});
@ -83,8 +83,8 @@ describe('SessionCommands', () => {
describe('login', () => {
beforeEach(() => {
webClient.protobuf.controller.Command_Login = { create: args => args };
webClient.options.user = 'user';
webClient.options.pass = 'pass';
webClient.options.userName = 'user';
webClient.options.password = 'pass';
});
it('should call protobuf controller methods and sendCommand', () => {
@ -94,11 +94,11 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_Login.ext': {
...webClient.clientConfig,
userName: webClient.options.user,
password: webClient.options.pass,
clientid: jasmine.any(String)
userName: webClient.options.userName,
password: webClient.options.password,
clientid: expect.any(String)
}
}, jasmine.any(Function));
}, expect.any(Function));
});
describe('response', () => {
@ -118,15 +118,15 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response = { ResponseCode: { RespOk } };
sendSessionCommandSpy.and.callFake((_, callback) => callback(response));
sendSessionCommandSpy.mockImplementation((_, callback) => callback(response));
});
it('RespOk should update user/state and list users/games', () => {
spyOn(SessionPersistence, 'updateBuddyList');
spyOn(SessionPersistence, 'updateIgnoreList');
spyOn(SessionPersistence, 'updateUser');
spyOn(SessionCommands, 'listUsers');
spyOn(SessionCommands, 'listRooms');
jest.spyOn(SessionPersistence, 'updateBuddyList').mockImplementation(() => {});
jest.spyOn(SessionPersistence, 'updateIgnoreList').mockImplementation(() => {});
jest.spyOn(SessionPersistence, 'updateUser').mockImplementation(() => {});
jest.spyOn(SessionCommands, 'listUsers').mockImplementation(() => {});
jest.spyOn(SessionCommands, 'listRooms').mockImplementation(() => {});
SessionCommands.login();
@ -270,14 +270,14 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_Register.ext': {
...webClient.clientConfig,
userName: options.user,
password: options.pass,
userName: options.userName,
password: options.password,
email: options.email,
country: options.country,
realName: options.realName,
clientid: jasmine.any(String)
clientid: expect.any(String)
}
}, jasmine.any(Function));
}, expect.any(Function));
});
describe('response', () => {
@ -296,12 +296,12 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response = { ResponseCode: { RespRegistrationAccepted } };
sendSessionCommandSpy.and.callFake((_, callback) => callback(response));
sendSessionCommandSpy.mockImplementation((_, callback) => callback(response));
})
it('should login user if registration accepted without email verification', () => {
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountAwaitingActivation');
jest.spyOn(SessionCommands, 'login').mockImplementation(() => {});
jest.spyOn(SessionPersistence, 'accountAwaitingActivation').mockImplementation(() => {});
SessionCommands.register();
@ -315,8 +315,8 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response.ResponseCode.RespRegistrationAcceptedNeedsActivation =
RespRegistrationAcceptedNeedsActivation;
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountAwaitingActivation');
jest.spyOn(SessionCommands, 'login').mockImplementation(() => {});
jest.spyOn(SessionPersistence, 'accountAwaitingActivation').mockImplementation(() => {});
SessionCommands.register();
@ -355,11 +355,11 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_Activate.ext': {
...webClient.clientConfig,
userName: options.user,
token: options.activationCode,
clientid: jasmine.any(String)
userName: options.userName,
token: options.token,
clientid: expect.any(String)
}
}, jasmine.any(Function));
}, expect.any(Function));
});
describe('response', () => {
@ -377,9 +377,9 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response = { ResponseCode: { RespActivationAccepted } };
sendSessionCommandSpy.and.callFake((_, callback) => callback(response));
spyOn(SessionCommands, 'login');
spyOn(SessionPersistence, 'accountActivationFailed');
sendSessionCommandSpy.mockImplementation((_, callback) => callback(response));
jest.spyOn(SessionCommands, 'login').mockImplementation(() => {});
jest.spyOn(SessionPersistence, 'accountActivationFailed').mockImplementation(() => {});
});
it('should activate user and login if correct activation token used', () => {
@ -413,7 +413,7 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_ListUsers.ext': {}
}, jasmine.any(Function));
}, expect.any(Function));
});
it('should call SessionPersistence.updateUsers if RespOk', () => {
@ -425,8 +425,8 @@ describe('SessionCommands', () => {
};
webClient.protobuf.controller.Response = { ResponseCode: { RespOk } };
sendSessionCommandSpy.and.callFake((_, callback) => callback(response));
spyOn(SessionPersistence, 'updateUsers');
sendSessionCommandSpy.mockImplementation((_, callback) => callback(response));
jest.spyOn(SessionPersistence, 'updateUsers').mockImplementation(() => {});
SessionCommands.listUsers();
@ -460,7 +460,7 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_JoinRoom.ext': { roomId }
}, jasmine.any(Function));
}, expect.any(Function));
});
describe('response', () => {
@ -476,11 +476,11 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response = { ResponseCode: { RespOk } };
sendSessionCommandSpy.and.callFake((_, callback) => callback(response));
sendSessionCommandSpy.mockImplementation((_, callback) => callback(response));
});
it('RespOk should call RoomPersistence.joinRoom', () => {
spyOn(RoomPersistence, 'joinRoom');
jest.spyOn(RoomPersistence, 'joinRoom').mockImplementation(() => {});
SessionCommands.joinRoom(roomId);
@ -534,7 +534,7 @@ describe('SessionCommands', () => {
describe('addToBuddyList', () => {
it('should call SessionCommands.addToList', () => {
spyOn(SessionCommands, 'addToList');
jest.spyOn(SessionCommands, 'addToList').mockImplementation(() => {});
const userName = 'userName';
SessionCommands.addToBuddyList(userName);
@ -545,7 +545,7 @@ describe('SessionCommands', () => {
describe('removeFromBuddyList', () => {
it('should call SessionCommands.removeFromList', () => {
spyOn(SessionCommands, 'removeFromList');
jest.spyOn(SessionCommands, 'removeFromList').mockImplementation(() => {});
const userName = 'userName';
SessionCommands.removeFromBuddyList(userName);
@ -556,7 +556,7 @@ describe('SessionCommands', () => {
describe('addToIgnoreList', () => {
it('should call SessionCommands.addToList', () => {
spyOn(SessionCommands, 'addToList');
jest.spyOn(SessionCommands, 'addToList').mockImplementation(() => {});
const userName = 'userName';
SessionCommands.addToIgnoreList(userName);
@ -567,7 +567,7 @@ describe('SessionCommands', () => {
describe('removeFromIgnoreList', () => {
it('should call SessionCommands.removeFromList', () => {
spyOn(SessionCommands, 'removeFromList');
jest.spyOn(SessionCommands, 'removeFromList').mockImplementation(() => {});
const userName = 'userName';
SessionCommands.removeFromIgnoreList(userName);
@ -588,7 +588,7 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_AddToList.ext': addToList
}, jasmine.any(Function));
}, expect.any(Function));
});
});
@ -604,7 +604,7 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendSessionCommand).toHaveBeenCalledWith({
'.Command_RemoveFromList.ext': removeFromList
}, jasmine.any(Function));
}, expect.any(Function));
});
});
@ -621,7 +621,7 @@ describe('SessionCommands', () => {
expect(webClient.protobuf.sendModeratorCommand).toHaveBeenCalled();
expect(webClient.protobuf.sendModeratorCommand).toHaveBeenCalledWith({
'.Command_ViewLogHistory.ext': filters
}, jasmine.any(Function));
}, expect.any(Function));
});
describe('response', () => {
@ -637,11 +637,11 @@ describe('SessionCommands', () => {
webClient.protobuf.controller.Response = { ResponseCode: { RespOk } };
sendModeratorCommandSpy.and.callFake((_, callback) => callback(response));
sendModeratorCommandSpy.mockImplementation((_, callback) => callback(response));
});
it('RespOk should call SessionPersistence.viewLogs', () => {
spyOn(SessionPersistence, 'viewLogs');
jest.spyOn(SessionPersistence, 'viewLogs').mockImplementation(() => {});
SessionCommands.viewLogHistory(filters);

View file

@ -11,7 +11,7 @@ import { RoomPersistence } from '../persistence/RoomPersistence';
describe('RoomEvents', () => {
it('.Event_JoinRoom.ext should call RoomPersistence.userJoined', () => {
spyOn(RoomPersistence, 'userJoined');
jest.spyOn(RoomPersistence, 'userJoined').mockImplementation(() => {});
const data: JoinRoomData = { userInfo: {} as any };
const event: RoomEvent = { roomEvent: { roomId: 1 } };
@ -24,7 +24,7 @@ describe('RoomEvents', () => {
});
it('.Event_LeaveRoom.ext should call RoomPersistence.userLeft', () => {
spyOn(RoomPersistence, 'userLeft');
jest.spyOn(RoomPersistence, 'userLeft').mockImplementation(() => {});
const data: LeaveRoomData = { name: '' };
const event: RoomEvent = { roomEvent: { roomId: 1 } };
@ -37,7 +37,7 @@ describe('RoomEvents', () => {
});
it('.Event_ListGames.ext should call RoomPersistence.updateGames', () => {
spyOn(RoomPersistence, 'updateGames');
jest.spyOn(RoomPersistence, 'updateGames').mockImplementation(() => {});
const data: ListGamesData = { gameList: [] };
const event: RoomEvent = { roomEvent: { roomId: 1 } };
@ -50,7 +50,7 @@ describe('RoomEvents', () => {
});
it('.Event_RoomSay.ext should call RoomPersistence.addMessage', () => {
spyOn(RoomPersistence, 'addMessage');
jest.spyOn(RoomPersistence, 'addMessage').mockImplementation(() => {});
const data: Message = {} as any;
const event: RoomEvent = { roomEvent: { roomId: 1 } };

View file

@ -1,4 +1,4 @@
import { StatusEnum } from 'types';
import { StatusEnum, WebSocketConnectReason } from 'types';
import {
AddToListData,
@ -15,18 +15,17 @@ import {
import { SessionCommands } from '../commands';
import { RoomPersistence, SessionPersistence } from '../persistence';
import webClient from '../WebClient';
import { WebSocketConnectReason } from '../services/WebSocketService';
describe('SessionEvents', () => {
const roomId = 1;
beforeEach(() => {
spyOn(SessionCommands, 'updateStatus');
jest.spyOn(SessionCommands, 'updateStatus').mockImplementation(() => {});
});
describe('.Event_AddToList.ext', () => {
it('should call SessionPersistence.addToBuddyList if buddy listName', () => {
spyOn(SessionPersistence, 'addToBuddyList');
jest.spyOn(SessionPersistence, 'addToBuddyList').mockImplementation(() => {});
const data: AddToListData = { listName: 'buddy', userInfo: {} as any };
SessionEvents['.Event_AddToList.ext'](data);
@ -37,7 +36,7 @@ describe('SessionEvents', () => {
});
it('should call SessionPersistence.addToIgnoreList if ignore listName', () => {
spyOn(SessionPersistence, 'addToIgnoreList');
jest.spyOn(SessionPersistence, 'addToIgnoreList').mockImplementation(() => {});
const data: AddToListData = { listName: 'ignore', userInfo: {} as any };
SessionEvents['.Event_AddToList.ext'](data);
@ -48,7 +47,7 @@ describe('SessionEvents', () => {
});
it('should call console.log if unknown listName', () => {
spyOn(console, 'log');
jest.spyOn(console, 'log').mockImplementation(() => {});
const data: AddToListData = { listName: 'unknown', userInfo: {} as any };
SessionEvents['.Event_AddToList.ext'](data);
@ -218,7 +217,7 @@ describe('SessionEvents', () => {
describe('.Event_ListRooms.ext', () => {
beforeEach(() => {
webClient.options.autojoinrooms = false;
spyOn(RoomPersistence, 'updateRooms');
jest.spyOn(RoomPersistence, 'updateRooms').mockImplementation(() => {});
});
it('should call RoomPersistence.updateRooms', () => {
@ -231,7 +230,7 @@ describe('SessionEvents', () => {
it('should call SessionCommands.joinRoom if webClient and room is configured for autojoin', () => {
webClient.options.autojoinrooms = true;
spyOn(SessionCommands, 'joinRoom');
jest.spyOn(SessionCommands, 'joinRoom').mockImplementation(() => {});
const data: ListRoomsData = { roomList: [{ roomId, autoJoin: true } as any, { roomId: 2, autoJoin: false } as any] };
SessionEvents['.Event_ListRooms.ext'](data);
@ -243,7 +242,7 @@ describe('SessionEvents', () => {
describe('.Event_RemoveFromList.ext', () => {
it('should call SessionPersistence.removeFromBuddyList if buddy listName', () => {
spyOn(SessionPersistence, 'removeFromBuddyList');
jest.spyOn(SessionPersistence, 'removeFromBuddyList').mockImplementation(() => {});
const data: RemoveFromListData = { listName: 'buddy', userName: '' };
SessionEvents['.Event_RemoveFromList.ext'](data);
@ -254,7 +253,7 @@ describe('SessionEvents', () => {
});
it('should call SessionPersistence.removeFromIgnoreList if ignore listName', () => {
spyOn(SessionPersistence, 'removeFromIgnoreList');
jest.spyOn(SessionPersistence, 'removeFromIgnoreList').mockImplementation(() => {});
const data: RemoveFromListData = { listName: 'ignore', userName: '' };
SessionEvents['.Event_RemoveFromList.ext'](data);
@ -265,7 +264,7 @@ describe('SessionEvents', () => {
});
it('should call console.log if unknown listName', () => {
spyOn(console, 'log');
jest.spyOn(console, 'log').mockImplementation(() => {});
const data: RemoveFromListData = { listName: 'unknown', userName: '' };
SessionEvents['.Event_RemoveFromList.ext'](data);
@ -287,13 +286,15 @@ describe('SessionEvents', () => {
serverName: 'serverName',
serverVersion: 'serverVersion',
protocolVersion: 0,
serverOptions: 0
};
spyOn(SessionPersistence, 'updateInfo');
jest.spyOn(SessionPersistence, 'updateInfo').mockImplementation(() => {});
webClient.protobuf.controller.Event_ServerIdentification = { ServerOptions: { SupportsPasswordHash: 1 } };
});
it('update status/info and login', () => {
spyOn(SessionCommands, 'login');
jest.spyOn(SessionCommands, 'login').mockImplementation(() => {});
webClient.options.reason = WebSocketConnectReason.LOGIN;
@ -305,7 +306,7 @@ describe('SessionEvents', () => {
});
it('should update stat/info and register', () => {
spyOn(SessionCommands, 'register');
jest.spyOn(SessionCommands, 'register').mockImplementation(() => {});
webClient.options.reason = WebSocketConnectReason.REGISTER;
@ -316,7 +317,7 @@ describe('SessionEvents', () => {
});
it('should update stat/info and activate account', () => {
spyOn(SessionCommands, 'activateAccount');
jest.spyOn(SessionCommands, 'activateAccount').mockImplementation(() => {});
webClient.options.reason = WebSocketConnectReason.ACTIVATE_ACCOUNT;
@ -327,14 +328,15 @@ describe('SessionEvents', () => {
});
it('should disconnect if protocolVersion mismatched', () => {
spyOn(SessionCommands, 'login');
spyOn(SessionCommands, 'disconnect');
jest.spyOn(SessionCommands, 'login').mockImplementation(() => {});
jest.spyOn(SessionCommands, 'disconnect').mockImplementation(() => {});
webClient.protocolVersion = 0;
const data: ServerIdentificationData = {
serverName: '',
serverVersion: '',
protocolVersion: 1,
serverOptions: 0
};
event(data);
@ -350,7 +352,7 @@ describe('SessionEvents', () => {
describe('.Event_ServerMessage.ext', () => {
it('should call SessionPersistence.serverMessage', () => {
spyOn(SessionPersistence, 'serverMessage');
jest.spyOn(SessionPersistence, 'serverMessage').mockImplementation(() => {});
const data: ServerMessageData = { message: 'message' };
SessionEvents['.Event_ServerMessage.ext'](data);
@ -363,7 +365,7 @@ describe('SessionEvents', () => {
describe('.Event_UserJoined.ext', () => {
it('should call SessionPersistence.userJoined', () => {
spyOn(SessionPersistence, 'userJoined');
jest.spyOn(SessionPersistence, 'userJoined').mockImplementation(() => {});
const data: UserJoinedData = { userInfo: {} as any };
SessionEvents['.Event_UserJoined.ext'](data);
@ -376,7 +378,7 @@ describe('SessionEvents', () => {
describe('.Event_UserLeft.ext', () => {
it('should call SessionPersistence.userLeft', () => {
spyOn(SessionPersistence, 'userLeft');
jest.spyOn(SessionPersistence, 'userLeft').mockImplementation(() => {});
const data: UserLeftData = { name: '' };
SessionEvents['.Event_UserLeft.ext'](data);

View file

@ -30,8 +30,8 @@ describe('KeepAliveService', () => {
promise = new Promise(resolve => resolvePing = resolve);
ping = (done) => promise.then(done);
checkReadyStateSpy = spyOn(socket, 'checkReadyState');
checkReadyStateSpy.and.returnValue(true);
checkReadyStateSpy = jest.spyOn(socket, 'checkReadyState');
checkReadyStateSpy.mockImplementation(() => true);
service.startPingLoop(interval, ping);
jest.advanceTimersByTime(interval);
@ -52,15 +52,15 @@ describe('KeepAliveService', () => {
});
it('should fire disconnected$ if lastPingPending is still true', () => {
spyOn(service.disconnected$, 'next');
jest.spyOn(service.disconnected$, 'next').mockImplementation(() => {});
jest.advanceTimersByTime(interval);
expect(service.disconnected$.next).toHaveBeenCalled();
});
it('should endPingLoop if socket is not open', () => {
spyOn(service, 'endPingLoop');
checkReadyStateSpy.and.returnValue(false);
jest.spyOn(service, 'endPingLoop').mockImplementation(() => {});
checkReadyStateSpy.mockImplementation(() => false);
resolvePing();
jest.advanceTimersByTime(interval);