|
1 | 1 | import { mazeReducer } from "./mazeReducer";
|
2 |
| -import { initialState } from "../../models/maze/initialState"; |
3 |
| -import { CLEAR_GRID } from "../../actions/menuActions/menuActions"; |
| 2 | +import { initialState, generateMaze } from "../../models/maze/initialState"; |
| 3 | +import { |
| 4 | + CLEAR_GRID, |
| 5 | + UPDATE_GRID_SIZE, |
| 6 | + LOAD_MAZE, |
| 7 | +} from "../../actions/menuActions/menuActions"; |
4 | 8 | import {
|
5 | 9 | CHANGE_START,
|
6 | 10 | CHANGE_END,
|
7 | 11 | MAKE_WALL,
|
8 | 12 | MAKE_EMPTY,
|
| 13 | + PROGRESS_BFS, |
| 14 | + PROGRESS_DFS, |
| 15 | + PROGRESS_ASTAR, |
9 | 16 | } from "../../actions/mazeActions/mazeActions";
|
10 |
| -import { Maze } from "../../models/maze"; |
| 17 | +import { Maze, Coord } from "../../models/maze"; |
11 | 18 | import { SpaceTypes } from "../../components/Space/types";
|
| 19 | +import { getCoord } from "../../components/Maze/algorithms"; |
| 20 | +import { heuristic } from "../../components/Maze/algorithms/A*"; |
12 | 21 |
|
13 | 22 | describe("Maze Reducer Tests", () => {
|
14 | 23 | it("Clear Grid Expected State", () => {
|
@@ -108,17 +117,160 @@ describe("Maze Reducer Tests", () => {
|
108 | 117 | expect(updatedState).toEqual(expectedState);
|
109 | 118 | });
|
110 | 119 |
|
111 |
| - // TODO: progressBFS |
| 120 | + it("Updates MazeInfo and bfsQueue", () => { |
| 121 | + const queue: Coord[] = [ |
| 122 | + { x: 0, y: 1 }, |
| 123 | + { x: 1, y: 0 }, |
| 124 | + ]; |
| 125 | + const currCoord: Coord = { x: 0, y: 0 }; |
| 126 | + const neighbors: Coord[] = [ |
| 127 | + { x: 0, y: 1 }, |
| 128 | + { x: 1, y: 0 }, |
| 129 | + ]; |
| 130 | + |
| 131 | + const action = { |
| 132 | + type: PROGRESS_BFS, |
| 133 | + payload: { |
| 134 | + queue: queue, |
| 135 | + coord: currCoord, |
| 136 | + neighbors: neighbors, |
| 137 | + }, |
| 138 | + }; |
| 139 | + |
| 140 | + let newMaze = initialState.mazeInfo; |
| 141 | + // Sets current position to visited |
| 142 | + newMaze[currCoord.y][currCoord.x].visited = true; |
| 143 | + // Sets the parent of all the neighbors |
| 144 | + neighbors.forEach((neighbor) => { |
| 145 | + newMaze[neighbor.y][neighbor.x].parent = currCoord; |
| 146 | + }); |
112 | 147 |
|
113 |
| - // TODO: progressDFS |
| 148 | + const updatedState = mazeReducer(initialState, action); |
| 149 | + const expectedState: Maze = { |
| 150 | + ...initialState, |
| 151 | + mazeInfo: newMaze, |
| 152 | + bfsQueue: queue, |
| 153 | + }; |
| 154 | + expect(updatedState).toEqual(expectedState); |
| 155 | + }); |
114 | 156 |
|
115 |
| - // TODO: progressAstar |
| 157 | + it("Updates MazeInfo and dfsStack", () => { |
| 158 | + const stack: Coord[] = [ |
| 159 | + { x: 0, y: 1 }, |
| 160 | + { x: 1, y: 0 }, |
| 161 | + ]; |
| 162 | + const currCoord: Coord = { x: 0, y: 0 }; |
| 163 | + const neighbors: Coord[] = [ |
| 164 | + { x: 0, y: 1 }, |
| 165 | + { x: 1, y: 0 }, |
| 166 | + ]; |
116 | 167 |
|
117 |
| - // TODO: stopVis |
| 168 | + const action = { |
| 169 | + type: PROGRESS_DFS, |
| 170 | + payload: { |
| 171 | + stack: stack, |
| 172 | + coord: currCoord, |
| 173 | + neighbors: neighbors, |
| 174 | + }, |
| 175 | + }; |
| 176 | + |
| 177 | + let newMaze = initialState.mazeInfo; |
| 178 | + // Sets current position to visited |
| 179 | + newMaze[currCoord.y][currCoord.x].visited = true; |
| 180 | + // Sets the parent of all the neighbors |
| 181 | + neighbors.forEach((neighbor) => { |
| 182 | + newMaze[neighbor.y][neighbor.x].parent = currCoord; |
| 183 | + }); |
| 184 | + |
| 185 | + const updatedState = mazeReducer(initialState, action); |
| 186 | + const expectedState: Maze = { |
| 187 | + ...initialState, |
| 188 | + mazeInfo: newMaze, |
| 189 | + dfsStack: stack, |
| 190 | + }; |
| 191 | + expect(updatedState).toEqual(expectedState); |
| 192 | + }); |
| 193 | + |
| 194 | + it("Updates MazeInfo and openSet and closedSet", () => { |
| 195 | + const currentSpace = { x: 0, y: 0 }; |
| 196 | + const openSet: Coord[] = []; |
| 197 | + const closedSet: Coord[] = [currentSpace]; |
| 198 | + let newMazeInfo = initialState.mazeInfo; |
| 199 | + const end = getCoord(initialState.mazeInfo, SpaceTypes.end); |
| 200 | + |
| 201 | + // Update the G value of all the neighbors of the current space |
| 202 | + const neighbors = [ |
| 203 | + { x: 0, y: 1 }, |
| 204 | + { x: 1, y: 0 }, |
| 205 | + ]; |
| 206 | + neighbors.forEach((neighbor) => { |
| 207 | + const tempG = |
| 208 | + initialState.mazeInfo[currentSpace.y][currentSpace.x].g + |
| 209 | + heuristic(neighbor, currentSpace); |
| 210 | + newMazeInfo[neighbor.y][neighbor.x].g = tempG; |
| 211 | + }); |
| 212 | + |
| 213 | + const action = { |
| 214 | + type: PROGRESS_ASTAR, |
| 215 | + payload: { |
| 216 | + openSet: openSet, |
| 217 | + closedSet: closedSet, |
| 218 | + newMazeInfo: newMazeInfo, |
| 219 | + end: end, |
| 220 | + }, |
| 221 | + }; |
| 222 | + |
| 223 | + const updatedState = mazeReducer(initialState, action); |
| 224 | + const expectedState: Maze = { |
| 225 | + ...initialState, |
| 226 | + mazeInfo: newMazeInfo, |
| 227 | + astarOpenSet: openSet, |
| 228 | + astarClosedSet: closedSet, |
| 229 | + }; |
| 230 | + expect(updatedState).toEqual(expectedState); |
| 231 | + }); |
118 | 232 |
|
119 | 233 | // TODO: randomizeWalls
|
120 | 234 |
|
121 |
| - // TODO: updateGridSize |
| 235 | + it("Changes the size of the gird", () => { |
| 236 | + const newHeight = 30; |
| 237 | + const newWidth = 30; |
| 238 | + |
| 239 | + const action = { |
| 240 | + type: UPDATE_GRID_SIZE, |
| 241 | + payload: { |
| 242 | + cols: newWidth, |
| 243 | + rows: newHeight, |
| 244 | + }, |
| 245 | + }; |
| 246 | + |
| 247 | + let newMaze = generateMaze(newWidth, newHeight, true); |
| 248 | + |
| 249 | + const updatedState = mazeReducer(initialState, action); |
| 250 | + const expectedState: Maze = { |
| 251 | + ...initialState, |
| 252 | + mazeInfo: newMaze, |
| 253 | + }; |
| 254 | + |
| 255 | + expect(updatedState).toEqual(expectedState); |
| 256 | + }); |
| 257 | + |
| 258 | + it("Loads a maze", () => { |
| 259 | + const loadedMaze = generateMaze(30, 30, true); |
| 260 | + |
| 261 | + const action = { |
| 262 | + type: LOAD_MAZE, |
| 263 | + payload: { |
| 264 | + mazeInfo: loadedMaze, |
| 265 | + }, |
| 266 | + }; |
122 | 267 |
|
123 |
| - // TODO: loadMaze |
| 268 | + const updatedState = mazeReducer(initialState, action); |
| 269 | + const expectedState: Maze = { |
| 270 | + ...initialState, |
| 271 | + mazeInfo: loadedMaze, |
| 272 | + }; |
| 273 | + |
| 274 | + expect(updatedState).toEqual(expectedState); |
| 275 | + }); |
124 | 276 | });
|
0 commit comments