modaq
Version:
Quiz Bowl Reader using TypeScript, React, and MobX
211 lines • 11 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const GameFormats = __importStar(require("src/state/GameFormats"));
const RenameTeamDialogController = __importStar(require("src/components/dialogs/RenameTeamDialogController"));
const AppState_1 = require("src/state/AppState");
require("src/state/Events");
const GameState_1 = require("src/state/GameState");
const PacketState_1 = require("src/state/PacketState");
require("src/state/RenameTeamDialogState");
const TeamState_1 = require("src/state/TeamState");
const defaultPacket = new PacketState_1.PacketState();
defaultPacket.setTossups([
new PacketState_1.Tossup("first q", "first a"),
new PacketState_1.Tossup("second q", "second a"),
new PacketState_1.Tossup("third q", "third a"),
new PacketState_1.Tossup("fourth q", "fourth a"),
]);
const defaultTeamNames = ["First Team", "Team2"];
function createDefaultExistingPlayers() {
return [
new TeamState_1.Player("Alex", defaultTeamNames[0], true),
new TeamState_1.Player("Anna", defaultTeamNames[0], true),
new TeamState_1.Player("Bob", defaultTeamNames[1], true),
new TeamState_1.Player("Anna", defaultTeamNames[1], true),
new TeamState_1.Player("Ashok", defaultTeamNames[0], false),
];
}
function initializeApp() {
const gameState = new GameState_1.GameState();
gameState.loadPacket(defaultPacket);
const players = createDefaultExistingPlayers();
gameState.addNewPlayers(players);
AppState_1.AppState.resetInstance();
const appState = AppState_1.AppState.instance;
appState.game = gameState;
appState.uiState.dialogState.showRenameTeamDialog(players[0].teamName);
return appState;
}
function getRenameTeamDialogState(appState) {
if (appState.uiState.dialogState.renameTeamDialog == undefined) {
chai_1.assert.fail("RenameTeamDialog should not be undefined");
}
return appState.uiState.dialogState.renameTeamDialog;
}
describe("RenameTeamDialogControllerTests", () => {
it("changeNewName", () => {
const name = "New team name";
const appState = initializeApp();
RenameTeamDialogController.changeNewName(name);
const state = getRenameTeamDialogState(appState);
chai_1.expect(state.newName).to.equal(name);
chai_1.expect(state.teamName).to.equal(defaultTeamNames[0]);
chai_1.expect(state.errorMessage).to.be.undefined;
});
it("hideDialog", () => {
const appState = initializeApp();
RenameTeamDialogController.hideDialog();
chai_1.expect(appState.uiState.dialogState.renameTeamDialog).to.be.undefined;
});
describe("validate", () => {
it("validate - non-empty name", () => {
initializeApp();
RenameTeamDialogController.changeNewName(" ");
const errorMessage = RenameTeamDialogController.validate();
chai_1.expect(errorMessage).to.not.be.undefined;
});
it("validate - duplicate name", () => {
initializeApp();
RenameTeamDialogController.changeNewName(defaultTeamNames[1]);
const errorMessage = RenameTeamDialogController.validate();
chai_1.expect(errorMessage).to.not.be.undefined;
});
it("validate - new name", () => {
initializeApp();
RenameTeamDialogController.changeNewName("Newbie");
const errorMessage = RenameTeamDialogController.validate();
chai_1.expect(errorMessage).to.be.undefined;
});
});
describe("renameTeam", () => {
it("renameTeam succeeds", () => {
const name = "New Team";
const appState = initializeApp();
const player = appState.game.players[0];
appState.game.cycles[0].addWrongBuzz({
player: player,
points: -5,
position: 10,
}, 0, GameFormats.UndefinedGameFormat);
appState.game.cycles[0].addTossupProtest(player.teamName, 0, 10, "Some answer", "It was right");
appState.game.cycles[1].addCorrectBuzz({
player: player,
points: 15,
position: 11,
}, 0, GameFormats.UndefinedGameFormat, 0, 3);
appState.game.cycles[1].addBonusProtest(0, 2, "Bonus answer", "That should've been right", player.teamName);
appState.game.cycles[1].timeouts = [
{
teamName: defaultTeamNames[0],
},
];
appState.game.cycles[2].addPlayerLeaves(player);
const newPlayer = new TeamState_1.Player("Avery", defaultTeamNames[0], false);
appState.game.cycles[2].addPlayerJoins(newPlayer);
appState.game.cycles[3].addSwapSubstitution(player, newPlayer);
RenameTeamDialogController.changeNewName(name);
RenameTeamDialogController.renameTeam();
// Verify - dialog hidden, player is in game, add player event in cycle
chai_1.expect(appState.uiState.dialogState.renameTeamDialog).to.be.undefined;
chai_1.expect(appState.game.teamNames[0]).to.equal(name);
chai_1.expect(appState.game.teamNames[1]).to.equal(defaultTeamNames[1]);
const wrongBuzzes = appState.game.cycles[0].wrongBuzzes;
if (wrongBuzzes === undefined) {
chai_1.assert.fail("Expected wrongBuzzes event in the first cycle");
}
chai_1.expect(wrongBuzzes.length).to.equal(1);
chai_1.expect(wrongBuzzes[0].marker.player.name).to.equal(player.name);
chai_1.expect(wrongBuzzes[0].marker.player.teamName).to.equal(name);
const tossupProtests = appState.game.cycles[0].tossupProtests;
if (tossupProtests === undefined) {
chai_1.assert.fail("Expected tossupProtests event in the first cycle");
}
chai_1.expect(tossupProtests.length).to.equal(1);
chai_1.expect(tossupProtests[0].teamName).to.equal(name);
const correctBuzz = appState.game.cycles[1].correctBuzz;
if (correctBuzz === undefined) {
chai_1.assert.fail("Expected correctBuzz event in the second cycle");
}
chai_1.expect(correctBuzz.marker.player.name).to.equal(player.name);
chai_1.expect(correctBuzz.marker.player.teamName).to.equal(name);
const bonusProtests = appState.game.cycles[1].bonusProtests;
if (bonusProtests === undefined) {
chai_1.assert.fail("Expected bonusProtests event in the second cycle");
}
chai_1.expect(bonusProtests.length).to.equal(1);
chai_1.expect(bonusProtests[0].teamName).to.equal(name);
const thirdCycle = appState.game.cycles[2];
const playerLeaves = thirdCycle.playerLeaves;
if (playerLeaves === undefined) {
chai_1.assert.fail("Expected playerLeaves event in the third cycle");
}
chai_1.expect(playerLeaves.length).to.equal(1);
chai_1.expect(playerLeaves[0].outPlayer.name).to.equal(player.name);
chai_1.expect(playerLeaves[0].outPlayer.teamName).to.equal(name);
const playerJoins = thirdCycle.playerJoins;
if (playerJoins === undefined) {
chai_1.assert.fail("Expected playerLeaves event in the third cycle");
}
chai_1.expect(playerJoins.length).to.equal(1);
chai_1.expect(playerJoins[0].inPlayer.name).to.equal(newPlayer.name);
chai_1.expect(playerJoins[0].inPlayer.teamName).to.equal(name);
const subs = appState.game.cycles[3].subs;
if (subs === undefined) {
chai_1.assert.fail("Expected substitution event in the fourth cycle");
}
chai_1.expect(subs.length).to.equal(1);
const sub = subs[0];
chai_1.expect(sub.inPlayer.name).to.equal(player.name);
chai_1.expect(sub.inPlayer.teamName).to.equal(name);
chai_1.expect(sub.outPlayer.name).to.equal(newPlayer.name);
chai_1.expect(sub.outPlayer.teamName).to.equal(name);
});
it("renameTeam fails (empty name)", () => {
const appState = initializeApp();
RenameTeamDialogController.changeNewName(" ");
RenameTeamDialogController.renameTeam();
const state = getRenameTeamDialogState(appState);
chai_1.expect(state.errorMessage).to.not.be.undefined;
chai_1.expect(appState.game.teamNames[0]).to.equal(defaultTeamNames[0]);
chai_1.expect(appState.game.teamNames[1]).to.equal(defaultTeamNames[1]);
});
it("renameTeam fails (other team name)", () => {
const appState = initializeApp();
RenameTeamDialogController.changeNewName(defaultTeamNames[1]);
RenameTeamDialogController.renameTeam();
const state = getRenameTeamDialogState(appState);
chai_1.expect(state.errorMessage).to.not.be.undefined;
chai_1.expect(appState.game.teamNames[0]).to.equal(defaultTeamNames[0]);
chai_1.expect(appState.game.teamNames[1]).to.equal(defaultTeamNames[1]);
});
it("renameTeam succeeds (same team name)", () => {
const appState = initializeApp();
RenameTeamDialogController.changeNewName(defaultTeamNames[0]);
RenameTeamDialogController.renameTeam();
chai_1.expect(appState.uiState.dialogState.renameTeamDialog).to.be.undefined;
const oldTeamNameIndex = appState.game.teamNames.indexOf(defaultTeamNames[0]);
chai_1.expect(oldTeamNameIndex).to.equal(0);
});
});
});
//# sourceMappingURL=RenameTeamDialogControllerTests.js.map