Skip to content

Commit e337037

Browse files
committed
Add simple tests to reducer
1 parent b7fddf5 commit e337037

1 file changed

Lines changed: 116 additions & 1 deletion

File tree

src/reducer.test.ts

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,123 @@
11
import * as React from "react";
2+
import { Action } from "@reduxjs/toolkit";
23
import * as Types from "./types";
3-
import { INITIAL_STATE, hasKeyDownHandler } from "./reducer";
4+
import * as Actions from "./actions";
5+
import reducer, { INITIAL_STATE, hasKeyDownHandler } from "./reducer";
6+
import { createEmptyMatrix } from "./util";
7+
import * as Point from "./point";
8+
import * as Matrix from "./matrix";
9+
import * as PointRange from "./point-range";
10+
import * as PointMap from "./point-map";
11+
import * as PointSet from "./point-set";
412

513
const EDIT_STATE: Types.StoreState = { ...INITIAL_STATE, mode: "edit" };
14+
const EXAMPLE_DATA = createEmptyMatrix<Types.CellBase>(4, 4);
15+
const EXAMPLE_POINT: Point.Point = { row: 2, column: 2 };
16+
const EXAMPLE_CELL: Types.CellBase = { value: 42 };
17+
const MOCK_GET_BINDINGS_FOR_CELL = jest.fn(() => []);
18+
const EXAMPLE_DIMENSIONS: Types.Dimensions = {
19+
left: 0,
20+
top: 0,
21+
width: 42,
22+
height: 42,
23+
};
24+
25+
describe("reducer", () => {
26+
const cases: Array<
27+
[
28+
name: string,
29+
state: Types.StoreState,
30+
action: Action<unknown>,
31+
expected: Types.StoreState
32+
]
33+
> = [
34+
[
35+
"setData",
36+
INITIAL_STATE,
37+
Actions.setData(EXAMPLE_DATA),
38+
{ ...INITIAL_STATE, data: EXAMPLE_DATA },
39+
],
40+
[
41+
"select",
42+
{ ...INITIAL_STATE, active: Point.ORIGIN },
43+
Actions.select(EXAMPLE_POINT),
44+
{
45+
...INITIAL_STATE,
46+
active: Point.ORIGIN,
47+
selected: PointRange.create(Point.ORIGIN, EXAMPLE_POINT),
48+
},
49+
],
50+
[
51+
"activate",
52+
INITIAL_STATE,
53+
Actions.activate(Point.ORIGIN),
54+
{
55+
...INITIAL_STATE,
56+
active: Point.ORIGIN,
57+
selected: PointRange.create(Point.ORIGIN, Point.ORIGIN),
58+
},
59+
],
60+
[
61+
"setCellData",
62+
INITIAL_STATE,
63+
Actions.setCellData(
64+
Point.ORIGIN,
65+
EXAMPLE_CELL,
66+
MOCK_GET_BINDINGS_FOR_CELL
67+
),
68+
{
69+
...INITIAL_STATE,
70+
data: Matrix.set(Point.ORIGIN, EXAMPLE_CELL, INITIAL_STATE.data),
71+
bindings: PointMap.from([[Point.ORIGIN, PointSet.from([])]]),
72+
mode: "edit",
73+
lastChanged: Point.ORIGIN,
74+
},
75+
],
76+
[
77+
"setCellDimensions",
78+
INITIAL_STATE,
79+
Actions.setCellDimensions(Point.ORIGIN, EXAMPLE_DIMENSIONS),
80+
{
81+
...INITIAL_STATE,
82+
rowDimensions: {
83+
[Point.ORIGIN.row]: {
84+
height: EXAMPLE_DIMENSIONS.height,
85+
top: EXAMPLE_DIMENSIONS.top,
86+
},
87+
},
88+
columnDimensions: {
89+
[Point.ORIGIN.column]: {
90+
width: EXAMPLE_DIMENSIONS.width,
91+
left: EXAMPLE_DIMENSIONS.left,
92+
},
93+
},
94+
},
95+
],
96+
["edit", INITIAL_STATE, Actions.edit(), EDIT_STATE],
97+
["view", EDIT_STATE, Actions.view(), INITIAL_STATE],
98+
[
99+
"blur",
100+
{ ...INITIAL_STATE, active: Point.ORIGIN },
101+
Actions.blur(),
102+
INITIAL_STATE,
103+
],
104+
[
105+
"dragStart",
106+
INITIAL_STATE,
107+
Actions.dragStart(),
108+
{ ...INITIAL_STATE, dragging: true },
109+
],
110+
[
111+
"dragEnd",
112+
{ ...INITIAL_STATE, dragging: true },
113+
Actions.dragEnd(),
114+
INITIAL_STATE,
115+
],
116+
];
117+
test.each(cases)("%s", (name, state, action, expected) => {
118+
expect(reducer(state, action)).toEqual(expected);
119+
});
120+
});
6121

7122
describe("hasKeyDownHandler", () => {
8123
const cases = [

0 commit comments

Comments
 (0)