end to end tests for the game page

This commit is contained in:
AlecM33
2022-09-02 15:53:15 -04:00
parent a891251ac7
commit b05afb85ae
32 changed files with 1276 additions and 1585 deletions

View File

@@ -1,21 +1,17 @@
// TODO: clean up these deep relative paths? jsconfig.json is not working...
import { GameCreationStepManager } from '../../client/src/modules/GameCreationStepManager.js';
import { DeckStateManager } from '../../client/src/modules/DeckStateManager.js';
import createTemplate from '../../client/src/view_templates/CreateTemplate.js';
import { createHandler } from '../../client/src/modules/page_handlers/createHandler.js';
import { GameCreationStepManager } from '../../client/src/modules/game_creation/GameCreationStepManager.js';
import { DeckStateManager } from '../../client/src/modules/game_creation/DeckStateManager.js';
describe('Create page', function () {
let gameCreationStepManager;
const gameCreationStepManager = new GameCreationStepManager(new DeckStateManager());
beforeAll(function () {
spyOn(window, 'confirm').and.returnValue(true);
const container = document.createElement('div');
container.setAttribute('id', 'game-creation-container');
document.body.appendChild(container);
document.getElementById('game-creation-container').innerHTML = createTemplate;
const deckManager = new DeckStateManager();
gameCreationStepManager = new GameCreationStepManager(deckManager);
gameCreationStepManager.renderStep('creation-step-container', 1);
createHandler(gameCreationStepManager);
});
describe('deck builder page', function () {
@@ -119,5 +115,9 @@ describe('Create page', function () {
expect(gameCreationStepManager.deckManager.deck.length).toEqual(5);
expect(document.querySelectorAll('.added-role').length).toEqual(5);
});
afterAll(() => {
document.body.innerHTML = '';
});
});
});

145
spec/e2e/game_spec.js Normal file
View File

@@ -0,0 +1,145 @@
import { gameHandler } from '../../client/src/modules/page_handlers/gameHandler.js';
import { mockGames } from '../support/MockGames.js';
import gameTemplate from '../../client/src/view_templates/GameTemplate.js';
import { globals } from '../../client/src/config/globals.js';
describe('game page', () => {
const XHRUtility = {
xhr (url, method = 'GET', headers, body = null) {
switch (url) {
case '/api/games/environment':
return new Promise((resolve, reject) => {
resolve({ content: 'production' });
});
}
}
};
describe('lobby game', () => {
const mockSocket = {
eventHandlers: {},
on: function (message, handler) {
console.log('REGISTERED MOCK SOCKET HANDLER: ' + message);
this.eventHandlers[message] = handler;
},
emit: function (eventName, ...args) {
switch (args[0]) { // eventName is currently always "inGameMessage" - the first arg after that is the specific message type
case globals.EVENT_IDS.FETCH_GAME_STATE:
args[args.length - 1](mockGames.gameInLobby);
}
},
hasListeners: function (listener) {
return false;
}
};
beforeAll(async () => {
await gameHandler(mockSocket, XHRUtility, { location: { href: 'host/game/ABCD' } }, gameTemplate);
mockSocket.eventHandlers.connect();
});
it('should display the connected client', () => {
expect(document.getElementById('client-name').innerText).toEqual('Alec');
expect(document.getElementById('client-user-type').innerText).toEqual('moderator \uD83D\uDC51');
});
it('should display the QR Code', () => {
expect(document.getElementById('canvas').innerText).not.toBeNull();
});
it('should display a new player when they join', () => {
mockSocket.eventHandlers[globals.EVENT_IDS.PLAYER_JOINED]({
name: 'Jane',
id: '123',
userType: globals.USER_TYPES.PLAYER,
out: false,
revealed: false
}, false);
expect(document.querySelectorAll('.lobby-player').length).toEqual(2);
expect(document.getElementById('current-info-message').innerText).toEqual('Jane joined!');
});
it('should activate the start button for the moderator when the game is full', () => {
expect(document.getElementById('start-game-button').classList.contains('disabled')).toBeTrue();
mockSocket.eventHandlers[globals.EVENT_IDS.PLAYER_JOINED]({
name: 'Jack',
id: '456',
userType: globals.USER_TYPES.PLAYER,
out: false,
revealed: false
}, true);
expect(document.getElementById('start-game-button').classList.contains('disabled')).toBeFalse();
});
afterAll(() => {
document.body.innerHTML = '';
});
});
describe('in-progress game', () => {
const mockSocket = {
eventHandlers: {},
on: function (message, handler) {
console.log('REGISTERED MOCK SOCKET HANDLER: ' + message);
this.eventHandlers[message] = handler;
},
emit: function (eventName, ...args) {
switch (args[0]) { // eventName is currently always "inGameMessage" - the first arg after that is the specific message type
case globals.EVENT_IDS.FETCH_GAME_STATE:
args[args.length - 1](mockGames.inProgressGame);
break;
default:
break;
}
},
hasListeners: function (listener) {
return false;
}
};
beforeAll(async () => {
await gameHandler(mockSocket, XHRUtility, { location: { href: 'host/game/ABCD' } }, gameTemplate);
mockSocket.eventHandlers.connect();
mockSocket.eventHandlers.getTimeRemaining(120000, true);
});
it('should display the game role of the client', () => {
expect(document.getElementById('role-name').innerText).toEqual('Parity Hunter');
expect(document.getElementById('role-image').getAttribute('src')).toEqual('../images/roles/ParityHunter.png');
expect(document.getElementById('game-timer').innerText).toEqual('00:02:00');
expect(document.getElementById('game-timer').classList.contains('paused')).toEqual(true);
expect(document.getElementById('players-alive-label').innerText).toEqual('Players: 4 / 5 Alive');
});
it('should flip the role card of the client', () => {
const clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('dblclick', true, true);
document.getElementById('game-role-back').dispatchEvent(clickEvent);
expect(document.getElementById('game-role').style.display).toEqual('flex');
expect(document.getElementById('game-role-back').style.display).toEqual('none');
});
it('should display the timer', () => {
expect(document.getElementById('game-timer').innerText).toEqual('00:02:00');
expect(document.getElementById('game-timer').classList.contains('paused')).toEqual(true);
});
it('should display the number of alive players', () => {
expect(document.getElementById('players-alive-label').innerText).toEqual('Players: 4 / 5 Alive');
});
it('should display the role info modal when the button is clicked', () => {
document.getElementById('role-info-button').click();
expect(document.getElementById('role-info-modal').style.display).toEqual('flex');
});
it('should NOT display the ability to play/pause the timer when the client is NOT a moderator', () => {
expect(document.getElementById('play-pause')).toBeNull();
});
afterAll(() => {
document.body.innerHTML = '';
});
});
});

168
spec/support/MockGames.js Normal file
View File

@@ -0,0 +1,168 @@
export const mockGames = {
gameInLobby: {
accessCode: 'ZS6M',
status: 'lobby',
moderator: {
name: 'Alec',
id: 'HZM64BVGXCSXS9L5YMGK2WTTQ',
userType: 'moderator',
out: false,
revealed: false
},
client: {
name: 'Alec',
hasEnteredName: false,
id: 'HZM64BVGXCSXS9L5YMGK2WTTQ',
cookie: 'Q68BYSMM7DB5CH338TNPMF9CK',
userType: 'moderator'
},
deck: [
{
role: 'Parity Hunter',
team: 'good',
description: 'You beat a werewolf in a 1v1 situation, winning the game for the village.',
id: 'wli3r2i9zxxmnns5euvtc01v0',
quantity: 1
},
{
role: 'Seer',
team: 'good',
description: 'Each night, learn if a chosen person is a Werewolf.',
id: '7q0xxfuflsjetzit1elu5rd2k',
quantity: 1
},
{
role: 'Villager',
team: 'good',
description: 'During the day, find the wolves and kill them.',
id: '33pw77odkdt3042yumxtxbrda',
quantity: 1
},
{
role: 'Sorceress',
team: 'evil',
description: 'Each night, learn if a chosen person is the Seer.',
id: '6fboglgqwua8n0twgh2f4a0xh',
quantity: 1
},
{
role: 'Werewolf',
team: 'evil',
description: "During the night, choose a villager to kill. Don't get killed.",
id: 'ixpmpaouc3oj1llkm6gttxbor',
quantity: 1
}
],
people: [],
timerParams: {
hours: null,
minutes: 15,
paused: false
},
isFull: false,
spectators: []
},
inProgressGame: {
accessCode: 'VVVG',
status: 'in progress',
moderator: {
name: 'Alec',
id: 'H24358C4GQ238LFK66RYMST9P',
userType: 'moderator',
out: false,
revealed: false
},
client: {
name: 'Andrea',
hasEnteredName: false,
id: 'THCX9K6MCKZXBXYH95FPLP68Y',
cookie: 'ZLPHS946H33W7LVJ28M8XCRVZ',
userType: 'player',
gameRole: 'Parity Hunter',
gameRoleDescription: 'You beat a werewolf in a 1v1 situation, winning the game for the village.',
alignment: 'good',
out: false
},
deck: [
{
role: 'Parity Hunter',
team: 'good',
description: 'You beat a werewolf in a 1v1 situation, winning the game for the village.',
id: 'gw82x923gde5pcf3ru8y0w6mr',
quantity: 1
},
{
role: 'Seer',
team: 'good',
description: 'Each night, learn if a chosen person is a Werewolf.',
id: '0it2wybz7mdoatqs60b847x5v',
quantity: 1
},
{
role: 'Villager',
team: 'good',
description: 'During the day, find the wolves and kill them.',
id: 'v8oeyscxu53bg0a29uxsh4mzc',
quantity: 1
},
{
role: 'Sorceress',
team: 'evil',
description: 'Each night, learn if a chosen person is the Seer.',
id: '52ooljj12xpah0dgirxay2lma',
quantity: 1
},
{
role: 'Werewolf',
team: 'evil',
description: "During the night, choose a villager to kill. Don't get killed.",
id: '1oomauy0wc9pn5q55d2f4zq64',
quantity: 1
}
],
people: [
{
name: 'Andrea',
id: 'THCX9K6MCKZXBXYH95FPLP68Y',
userType: 'player',
out: false,
revealed: false
},
{
name: 'Greg',
id: 'SFVBXJZNF3G3QDML63X34KG5X',
userType: 'player',
out: false,
revealed: false
},
{
name: 'Lys',
id: 'S2496LVXL9CFP5B493XX6XMYL',
userType: 'player',
out: false,
revealed: false
},
{
name: 'Hannah',
id: 'Y7P2LGDZL6NV283525PL5GZTB',
userType: 'player',
out: true,
revealed: true
},
{
name: 'Matthew',
id: 'Z9YZ2JBM2GPRXFJB9J6ZFNSP9',
userType: 'player',
out: false,
revealed: false
}
],
timerParams: {
hours: null,
minutes: 2,
paused: true,
timeRemaining: 120000
},
isFull: true
}
};