Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b5259fd
Basic jest setup
Vui-Chee Jun 14, 2021
5011eec
Test isOutOfBounds function
Vui-Chee Jun 15, 2021
ce377fd
Use jest option to run tests faster
Vui-Chee Jun 15, 2021
c7ec148
Test isMoveValid with simple piece for now
Vui-Chee Jun 15, 2021
cb157f4
Update jest config
Vui-Chee Jun 15, 2021
c305a3b
Git ignore generated coverage folder
Vui-Chee Jun 15, 2021
06b6b03
Initial reducer test
Vui-Chee Jun 16, 2021
354f59c
Test each piece rotation
Vui-Chee Jun 16, 2021
effe402
Test case L2 piece cannot rotate next to left wall
Vui-Chee Jun 16, 2021
e13d969
Test case S piece cannot rotate close to left wall
Vui-Chee Jun 16, 2021
00d9641
Update prev test case
Vui-Chee Jun 16, 2021
fbde6d0
Use toBe instead of toEqual for comparing nums
Vui-Chee Jun 16, 2021
0d745d9
Do not hardcode number of cols/rows during testing
Vui-Chee Jun 16, 2021
9e95a0e
Test case S piece cannot rotate next to right wall
Vui-Chee Jun 16, 2021
eb157ab
Create a new copy of each state slice per test
Vui-Chee Jun 16, 2021
b46e856
Test L2 piece cannot rotate touching right wall
Vui-Chee Jun 16, 2021
905eccd
Test L1 piece cannot rotate touching left wall
Vui-Chee Jun 16, 2021
e6c5ccd
Test L1 piece cannot rotate touching right wall
Vui-Chee Jun 16, 2021
72b4e23
Drop example test
Vui-Chee Jun 17, 2021
2a0ecaf
Test T piece cannot rotate flat side against wall
Vui-Chee Jun 17, 2021
098264e
Test Z piece 1 cannot rotate against left wall
Vui-Chee Jun 17, 2021
b06307c
Test Z piece 2 cannot rotate against right wall
Vui-Chee Jun 17, 2021
ef1626d
Test S piece retains shape after each rotation
Vui-Chee Jun 17, 2021
7ea513b
Test T piece retains shape during rotations
Vui-Chee Jun 17, 2021
85da382
Test L_PIECE_1 retains shape during rotations
Vui-Chee Jun 17, 2021
ff4fc99
Test L_PIECE_2 retains shape during rotations
Vui-Chee Jun 17, 2021
c92ae04
Test Z_PIECE_1 returns shape during rotations
Vui-Chee Jun 17, 2021
a3d43b1
Test Z_PIECE_2 returns shape during rotations
Vui-Chee Jun 17, 2021
b20af8a
Test both orientation and shape retention together
Vui-Chee Jun 17, 2021
9a66633
Initial test movement without blocks combination
Vui-Chee Jun 18, 2021
bf24b6d
Relocate testMove func + params directions
Vui-Chee Jun 18, 2021
fdd8b2b
Test L_PIECE_1 movement in 2 directions
Vui-Chee Jun 18, 2021
d633068
Helper functions to configure current piece
Vui-Chee Jun 18, 2021
4b061ff
Initial test L_PIECE_1 combine with floor
Vui-Chee Jun 18, 2021
2f18b03
Test combine L_PIECE_1 with landed L_PIECE_1
Vui-Chee Jun 18, 2021
2ae9ab0
Test combine L_PIECE_1 on moving in 2 directions
Vui-Chee Jun 18, 2021
afe1f1a
Add right & down movement test case (L_PIECE_1)
Vui-Chee Jun 18, 2021
6378127
Test L_PIECE_1 cannot move into left/right walls
Vui-Chee Jun 19, 2021
294f91d
Test movement for remaining blocks
Vui-Chee Jun 19, 2021
a6c9a0e
Remaining cases for movement in 2 directions
Vui-Chee Jun 19, 2021
5b46daa
Initial splitting of tests
Vui-Chee Jun 20, 2021
053067b
Update package-lock.json
Vui-Chee Jun 20, 2021
88e3b0d
Further split tests according to situations
Vui-Chee Jun 20, 2021
24b7ba1
Update test for NEW_PIECE action
Vui-Chee Jun 20, 2021
79017c5
Create state based on reducer initial state
Vui-Chee Jun 20, 2021
4e3160b
Extract rotation logic from testMove
Vui-Chee Jun 20, 2021
d793fea
Clearer test descriptions for testUtils
Vui-Chee Jun 20, 2021
5338e86
Ignore tools folder
Vui-Chee Jun 20, 2021
01bcdd2
Test movement for L_PIECE_1 in all orientations
Vui-Chee Jun 20, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"browser": true,
"es2021": true,
"node": true
"node": true,
"jest": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parser": "babel-eslint",
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/node_modules
/release-builds
/dist
/coverage
/tools
45 changes: 45 additions & 0 deletions __tests__/blocksReducer/createPiece.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import blocksReducer from "../../src/reducers/blocksReducer";
import { NEW_PIECE } from "../../src/actions/types";
import {
L_PIECE_1,
T_PIECE,
L_PIECE_2,
S_PIECE,
Z_PIECE_1,
Z_PIECE_2,
B_PIECE,
} from "../../src/reducers/blocksReducer/pieces";
import { createState } from "./utils";

const pieces = [
L_PIECE_1,
T_PIECE,
L_PIECE_2,
S_PIECE,
Z_PIECE_1,
Z_PIECE_2,
B_PIECE,
];

describe("blocks reducer", () => {
const state = createState();
const newState = blocksReducer(state, { type: NEW_PIECE });

it("should create valid current piece", () => {
// The new piece created must match one of the pieces.
const index = pieces.findIndex(
(piece) => piece.type === newState.currentPiece.type
);
expect(index).not.toBe(-1);
expect(newState.currentPiece).toEqual(pieces[index]);
});

it("should create valid next piece", () => {
// The new piece created must match one of the pieces.
const index = pieces.findIndex(
(piece) => piece.type === newState.nextPiece.type
);
expect(index).not.toBe(-1);
expect(newState.nextPiece).toEqual(pieces[index]);
});
});
64 changes: 64 additions & 0 deletions __tests__/blocksReducer/movement/combine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { L_PIECE_1 } from "../../../src/reducers/blocksReducer/pieces";
import {
LEFT_KEYCODE,
RIGHT_KEYCODE,
DOWN_KEYCODE,
} from "../../../src/utils/constants";
import { createState, moveCurrentPiece } from "../utils";

describe("Can move piece and combine with floor", () => {
it("L_PIECE_1", () => {
let state = createState(L_PIECE_1);
// Must bring L piece closer to landed blocks
state = moveCurrentPiece(state, [DOWN_KEYCODE], 19);
// Current piece is removed when combined with floor
expect(state.currentPiece).toEqual({ blocks: [] });
expect(state.blocks).toEqual({
15: { 4: 0 },
16: { 4: 0 },
17: { 3: 0, 4: 0 },
});
});
});

describe("Can move piece and combine with landed blocks", () => {
// Landed L_PIECE_1
const landedBlocks = {
15: { 4: 0 },
16: { 4: 0 },
17: { 3: 0, 4: 0 },
};
// Ignore
const nextPiece = { blocks: [] };

it("L_PIECE_1 single direction", () => {
let state = createState(L_PIECE_1, nextPiece, landedBlocks);
// Must bring L piece closer to landed blocks
state = moveCurrentPiece(state, [DOWN_KEYCODE], 16);
// Current piece is removed when combined with landed blocks
expect(state.currentPiece).toEqual({ blocks: [] });
expect(state.blocks).toEqual({
12: { 4: 0 },
13: { 4: 0 },
14: { 3: 0, 4: 0 },
15: { 4: 0 },
16: { 4: 0 },
17: { 3: 0, 4: 0 },
});
});

it("L_PIECE_1 two directions", () => {
let state = createState(L_PIECE_1, nextPiece, landedBlocks);
state = moveCurrentPiece(state, [LEFT_KEYCODE], 2);
state = moveCurrentPiece(state, [DOWN_KEYCODE], 16);
// Now move in 2 directions
state = moveCurrentPiece(state, [RIGHT_KEYCODE, DOWN_KEYCODE], 2);
expect(state.currentPiece).toEqual({ blocks: [] });
expect(state.blocks).toEqual({
14: { 3: 0 },
15: { 3: 0, 4: 0 },
16: { 2: 0, 3: 0, 4: 0 },
17: { 3: 0, 4: 0 },
});
});
});
25 changes: 25 additions & 0 deletions __tests__/blocksReducer/movement/noMoveIntoWalls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { L_PIECE_1 } from "../../../src/reducers/blocksReducer/pieces";
import { LEFT_KEYCODE, RIGHT_KEYCODE } from "../../../src/utils/constants";
import { createState, moveCurrentPiece } from "../utils";

describe("Cannot move into walls", () => {
it("L_PIECE_1", () => {
let state = createState(L_PIECE_1);
state = moveCurrentPiece(state, [LEFT_KEYCODE], 10);
// Cannot move into left wall
expect(state.currentPiece.blocks).toEqual([
{ x: -1, y: 0, color: 0, type: 0 },
{ x: -1, y: 1, color: 0, type: 1 },
{ x: -2, y: 1, color: 0, type: 2 },
{ x: -3, y: 1, color: 0, type: 1 },
]);
state = moveCurrentPiece(state, [RIGHT_KEYCODE], 10);
// Cannot move into right wall
expect(state.currentPiece.blocks).toEqual([
{ x: -1, y: 8, color: 0, type: 0 },
{ x: -1, y: 9, color: 0, type: 1 },
{ x: -2, y: 9, color: 0, type: 2 },
{ x: -3, y: 9, color: 0, type: 1 },
]);
});
});
Loading