Created
August 7, 2019 02:31
-
-
Save JoelCodes/dbbb07222f3026b233394f4cacb795f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = { | |
| preset: 'ts-jest', | |
| testEnvironment: 'node', | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "stas", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "test": "jest" | |
| }, | |
| "keywords": [], | |
| "author": "Joel Shinness <[email protected]> (http://joelshinness.com)", | |
| "license": "ISC", | |
| "dependencies": { | |
| "@types/jest": "^24.0.17", | |
| "@types/react": "^16.8.24", | |
| "@types/react-dom": "^16.8.5", | |
| "jest": "^24.8.0", | |
| "parcel-bundler": "^1.12.3", | |
| "react": "^16.8.6", | |
| "react-dom": "^16.8.6", | |
| "ts-jest": "^24.0.2" | |
| }, | |
| "devDependencies": { | |
| "typescript": "^3.5.3" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { TicTacToeState, ticTacToeReducer, initialState } from "./reducer"; | |
| describe('TicTacToeReducer', () => { | |
| it('returns the initial state if the state is undefined', () => { | |
| const initial:TicTacToeState = ticTacToeReducer(undefined, null); | |
| expect(initial).toEqual(initialState); | |
| }); | |
| it('sets a spot', () => { | |
| const afterMove = ticTacToeReducer(initialState, {type: 'move', spot: 4}); | |
| expect(afterMove).toEqual({ | |
| ...initialState, | |
| grid: ['empty', 'empty', 'empty', 'empty', 'X', 'empty', 'empty', 'empty', 'empty' ], | |
| currentPlayer: "O" | |
| } as TicTacToeState) | |
| }) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Reducer<TState, TAction> = (state:TState | undefined, action:TAction) => TState; | |
| export type PlayerMark = "X" | "O"; | |
| export type CellMark = PlayerMark| "empty" | |
| export interface TicTacToeState { | |
| grid: CellMark[]; | |
| currentPlayer: PlayerMark; | |
| }; | |
| export const initialState:TicTacToeState = { | |
| grid: [...new Array(9).fill("empty")].map(():CellMark => "empty"), | |
| currentPlayer: "X" | |
| }; | |
| interface MoveAction { type : 'move', spot: number }; | |
| interface ResetAction { type: 'reset'}; | |
| interface SomeThirdAction{ type: 'third thing'}; | |
| interface BlankAction {type: 'blank'} | |
| type TicTacToeAction = MoveAction | ResetAction | SomeThirdAction | BlankAction; | |
| export function _ticTacToeReducer(state:TicTacToeState | undefined, action:TicTacToeAction | null):TicTacToeState { | |
| if(state === undefined) return initialState; | |
| if(action === null) return state; | |
| switch(action.type){ | |
| case "move": { | |
| action | |
| const newGrid = [...state.grid]; | |
| newGrid[action.spot] = state.currentPlayer; | |
| return { | |
| ...state, | |
| grid: newGrid, | |
| currentPlayer: state.currentPlayer === "X" ? "O": "X" | |
| } | |
| } | |
| } | |
| return state; | |
| } | |
| export const ticTacToeReducer: Reducer<TicTacToeState, TicTacToeAction | null> = _ticTacToeReducer | |
| // let reducerA: Reducer<AState, AppAction>; | |
| // let reducerB: Reducer<BState, AppAction>; | |
| // type CombinedStates = { | |
| // theAs: AState, | |
| // theBs: BState | |
| // }; | |
| // let combinedReducer: Reducer<CombinedStates, AppAction> = combineReducer({ | |
| // theAs: reducerA, | |
| // theBs: reducerB | |
| // }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
| { | |
| "compilerOptions": { | |
| "strictNullChecks": true, | |
| "target": "es6" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment