There are two reasons for this: 1. I've reconsidered my original plan to store the past games in localStorage, because that would make it difficult to display them in historical order, and would necessitate more complex logic for updating and removing sessions. 2. I've been unhappy with how I did the testing of the serialization and deserialization logic. So I redid it, and now I'm satisfied with it. I've noticed that the testing methodology for the invalid fromStruct method tests is not fully sound. If a check is accidentally removed that test would not detect that, as long as it is not the very last. That is because then the next error triggers. Therefore that will need to be revisited.
89 lines
2.6 KiB
JavaScript
89 lines
2.6 KiB
JavaScript
"use strict";
|
|
|
|
import { Team } from "./round.js";
|
|
import RoundResult from "./round_result.js";
|
|
|
|
export default function() {
|
|
QUnit.module("RoundResult", function() {
|
|
QUnit.test("regular construction", function(assert) {
|
|
let rr = new RoundResult(2, Team.We);
|
|
assert.strictEqual(rr.points, 2, "correct points");
|
|
assert.strictEqual(rr.winner, Team.We, "correct winner");
|
|
});
|
|
|
|
QUnit.test("toStruct", function(assert) {
|
|
let rr = new RoundResult(2, Team.They);
|
|
let struct = rr.toStruct();
|
|
|
|
let expected = {
|
|
points: 2,
|
|
winner: Team.They,
|
|
};
|
|
|
|
assert.deepEqual(struct, expected, "successfull structurizing");
|
|
});
|
|
|
|
QUnit.test("fromStruct - current", function(assert) {
|
|
let orig = new RoundResult(3, Team.We);
|
|
let copy = new RoundResult(orig.toStruct());
|
|
assert.strictEqual(copy.points, orig.points, "points match");
|
|
assert.strictEqual(copy.winner, orig.winner, "winners match");
|
|
});
|
|
|
|
QUnit.test("fromStruct - invalid", function(assert) {
|
|
let struct = {};
|
|
function doIt(message) {
|
|
assert.throws(function() { new Round(struct); }, message);
|
|
}
|
|
|
|
doIt("no points");
|
|
struct.points = "4";
|
|
doIt("string points");
|
|
struct.points = 4.1;
|
|
doIt("non-int points");
|
|
struct.points = 1;
|
|
doIt("small points");
|
|
struct.points = 4;
|
|
|
|
doIt("no winner");
|
|
struct.winner = "they";
|
|
doIt("string winner");
|
|
struct.winner = -1;
|
|
doIt("non-team winner");
|
|
struct.winner = Team.They;
|
|
|
|
new RoundResult(struct);
|
|
});
|
|
|
|
// Data Import Tests
|
|
// =================
|
|
//
|
|
// The tests named "fromStruct - vXX - XXXXX" are there to ensure that
|
|
// future versions of the `RoundResult` class still can correctly read in
|
|
// the structural data exported by earlier versions. This is needed to
|
|
// ensure that the data remains usable.
|
|
//
|
|
// These tests work by importing an old structural object, and then
|
|
// exporting a new one. The new one should match with how the current
|
|
// implementation would represent the same state.
|
|
//
|
|
// Therefore you should not modify the `struct` variables. Instead adjust
|
|
// the `expected` variable, to make sure the reexported data matches what
|
|
// is now correct.
|
|
|
|
QUnit.test("fromStruct - v1", function(assert) {
|
|
let struct = {
|
|
points: 4,
|
|
winner: Team.They,
|
|
};
|
|
let rr = new RoundResult(struct);
|
|
|
|
let expected = {
|
|
points: 4,
|
|
winner: Team.They,
|
|
};
|
|
assert.deepEqual(rr.toStruct(), expected, "reexport matches");
|
|
});
|
|
});
|
|
}
|