name:
src/test/js/models/GridTest.ts
-rw-r--r--
4181
1import { assert } from "chai";
2import { it, describe, beforeEach } from "../testutils/TestUtils";
3import { Grid } from "../../../main/js/models/common/Grid";
4
5let grid: Grid<any>;
6
7describe("Grid.constructor", function () {
8 beforeEach(() => {
9 grid = new Grid(4, 6);
10 });
11
12 it("should have a valid constructor", function () {
13 assert.equal(grid.getColumns(), 4);
14 assert.equal(grid.getRows(), 6);
15 });
16
17 it("should fail to construct when given negative column sizes", function () {
18 grid = null;
19 try {
20 grid = new Grid(-1, 4);
21 assert.isNull(grid);
22 } catch (e) {
23 assert.isNotNull(e);
24 }
25 assert.isNull(grid);
26 });
27
28 it("should fail to construct when given negative row sizes", function () {
29 grid = null;
30 try {
31 grid = new Grid(1, -4);
32 assert.isNull(grid);
33 } catch (e) {
34 assert.isNotNull(e);
35 }
36 assert.isNull(grid);
37 });
38});
39
40describe("Grid.set", function () {
41 beforeEach(() => (grid = new Grid(4, 6)));
42
43 it("should set values", function () {
44 grid.set(0, 0, "0-0");
45 assert.deepEqual(grid.get(0, 0), "0-0");
46 grid.set(0, 1, "0-1");
47 assert.deepEqual(grid.get(0, 1), "0-1");
48 grid.set(1, 1, "1-1");
49 assert.deepEqual(grid.get(1, 1), "1-1");
50 grid.set(1, 0, "1-0");
51 assert.deepEqual(grid.get(1, 0), "1-0");
52 });
53
54 it("should bump column and row sizes", function () {
55 assert.deepEqual(grid.getColumns(), 4);
56 assert.deepEqual(grid.getRows(), 6);
57 grid.set(10, 21, "10-21");
58 assert.deepEqual(grid.get(10, 21), "10-21");
59 assert.deepEqual(grid.getColumns(), 11);
60 assert.deepEqual(grid.getRows(), 22);
61 });
62});
63
64describe("Grid.addGridToBottom", function () {
65 beforeEach(() => (grid = new Grid(4, 6)));
66
67 it("should add grid to bottom, as row(s)", function () {
68 const first = new Grid<string>(2, 2);
69 first.set(0, 0, "A");
70 first.set(0, 1, "B");
71 first.set(1, 1, "C");
72 first.set(1, 0, "D");
73 const second = new Grid<string>(2, 2);
74 second.set(0, 0, "E");
75 second.set(0, 1, "F");
76 second.set(1, 1, "G");
77 second.set(1, 0, "H");
78 first.addGridToBottom(second);
79 assert.deepEqual(first.getColumns(), 2);
80 assert.deepEqual(first.getRows(), 4);
81 assert.deepEqual(first.get(0, 0), "A");
82 assert.deepEqual(first.get(0, 1), "B");
83 assert.deepEqual(first.get(1, 1), "C");
84 assert.deepEqual(first.get(1, 0), "D");
85 assert.deepEqual(first.get(1, 2), "H");
86 assert.deepEqual(first.get(1, 3), "G");
87 assert.deepEqual(first.get(0, 2), "E");
88 assert.deepEqual(first.get(0, 3), "F");
89 });
90});
91
92describe("Grid.addGridToRight", function () {
93 beforeEach(() => (grid = new Grid(4, 6)));
94
95 it("should add grid to right, as columns(s)", function () {
96 const first = new Grid<string>(2, 2);
97 first.set(0, 0, "A");
98 first.set(0, 1, "B");
99 first.set(1, 1, "C");
100 first.set(1, 0, "D");
101 const second = new Grid<string>(2, 2);
102 second.set(0, 0, "E");
103 second.set(0, 1, "F");
104 second.set(1, 1, "G");
105 second.set(1, 0, "H");
106 first.addGridToRight(second);
107 assert.deepEqual(first.getColumns(), 4);
108 assert.deepEqual(first.getRows(), 2);
109 assert.deepEqual(first.get(0, 0), "A");
110 assert.deepEqual(first.get(0, 1), "B");
111 assert.deepEqual(first.get(1, 1), "C");
112 assert.deepEqual(first.get(1, 0), "D");
113 assert.deepEqual(first.get(2, 0), "E");
114 assert.deepEqual(first.get(3, 0), "H");
115 assert.deepEqual(first.get(2, 1), "F");
116 assert.deepEqual(first.get(3, 1), "G");
117 });
118});
119
120describe("Grid.raw", function () {
121 beforeEach(() => (grid = new Grid(2, 3)));
122
123 it("should return raw values", function () {
124 grid.set(1, 2, "A");
125 assert.deepEqual(grid.raw(), [
126 [undefined, undefined],
127 [undefined, undefined],
128 [undefined, "A"],
129 ]);
130 });
131});
132
133describe("Grid.map", function () {
134 beforeEach(() => {
135 grid = Grid.from([
136 [1, 2, 3],
137 [4, 5, 6],
138 [7, 8, 9],
139 ]);
140 });
141
142 it("should map all values", function () {
143 const expected = Grid.from([
144 [10, 20, 30],
145 [40, 50, 60],
146 [70, 80, 90],
147 ]);
148 assert.deepEqual(
149 grid.map((v) => v * 10),
150 expected
151 );
152 });
153});