From ea0a5814b67c47255c532def4e8b817270c8e019 Mon Sep 17 00:00:00 2001 From: Alexis DRAI Date: Fri, 16 Jun 2023 14:28:04 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20all=20reducer=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- redux/reducers/__tests__/moveReducer.test.ts | 100 +++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 redux/reducers/__tests__/moveReducer.test.ts diff --git a/README.md b/README.md index 7827381..ece59de 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,10 @@ a [backend API](https://github.com/draialexis/pokemong_api) that provides the da - [x] Retrieve data using the Web API (6 pts) + [x] Handle fetch success callback (3 pts) + [x] Handle fetch error callback (3 pts) - - [ ] Store favorite data into phone storage (2 pts) + - [ ] ~~Store favorite data into phone storage (2 pts)~~ - [ ] Write Tests (6 pts) + [ ] all actions payload (1 pts) - + [ ] all reducers case (2 pts) + + [x] all reducers case (2 pts) + [x] one UI Component (3 pts) ## Sketches diff --git a/redux/reducers/__tests__/moveReducer.test.ts b/redux/reducers/__tests__/moveReducer.test.ts new file mode 100644 index 0000000..082e76e --- /dev/null +++ b/redux/reducers/__tests__/moveReducer.test.ts @@ -0,0 +1,100 @@ +// redux/reducers/__tests__/moveReducer.test.ts +import moveReducer from '../moveReducer'; +import { CREATE_MOVE, DELETE_MOVE, GET_MOVES, MOVE_ERROR, UPDATE_MOVE } from '../../constants'; +import { Move } from "../../../entities/Move"; +import { MoveCategoryName } from "../../../entities/MoveCategoryName"; +import { TypeName } from "../../../entities/TypeName"; + +describe('moveReducer', () => { + const initialState = { + moves: [], + error: null + }; + + it('returns the initial state when an action type is not passed', () => { + // @ts-ignore + const reducer = moveReducer(undefined, { type: null }); + + expect(reducer).toEqual(initialState); + }); + + it('handles GET_MOVES action', () => { + const moves: Move[] = [{ + id: '1', + name: 'Test Move', + category: MoveCategoryName.PHYSICAL, + power: 100, + accuracy: 100, + type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] }, + schemaVersion: 2 + }]; + const reducer = moveReducer( + initialState, + { type: GET_MOVES, payload: moves } + ); + + expect(reducer).toEqual({ ...initialState, moves }); + }); + + it('handles CREATE_MOVE action', () => { + const move: Move = { + id: '1', + name: 'Test Move', + category: MoveCategoryName.PHYSICAL, + power: 100, + accuracy: 100, + type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] }, + schemaVersion: 2 + }; + const reducer = moveReducer( + initialState, + { type: CREATE_MOVE, payload: move } + ); + + expect(reducer).toEqual({ ...initialState, moves: [move] }); + }); + + it('handles UPDATE_MOVE action', () => { + const initialMove: Move = { + id: '1', + name: 'Test Move', + category: MoveCategoryName.PHYSICAL, + power: 100, + accuracy: 100, + type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] }, + schemaVersion: 2 + }; + const updatedMove: Move = { ...initialMove, name: 'Updated Move' }; + const reducer = moveReducer( + { ...initialState, moves: [initialMove] }, + { type: UPDATE_MOVE, payload: updatedMove } + ); + + expect(reducer).toEqual({ ...initialState, moves: [updatedMove] }); + }); + + it('handles DELETE_MOVE action', () => { + const move: Move = { + id: '1', + name: 'Test Move', + category: MoveCategoryName.PHYSICAL, + power: 100, + accuracy: 100, + type: { name: TypeName.NORMAL, weakAgainst: [], effectiveAgainst: [] }, + schemaVersion: 2 + }; + const reducer = moveReducer( + { ...initialState, moves: [move] }, + // @ts-ignore + { type: DELETE_MOVE, payload: move.id } + ); + + expect(reducer).toEqual(initialState); + }); + + it('handles MOVE_ERROR action', () => { + const reducer = moveReducer(initialState, { type: MOVE_ERROR, payload: 'Error message' }); + + expect(reducer).toEqual({ ...initialState, error: 'Error message' }); + }); +});