Created
March 4, 2020 08:36
-
-
Save rbinksy/f6d06eca36ce67bad8ed785e4fc73d28 to your computer and use it in GitHub Desktop.
This file contains 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 configureMockStore from 'redux-mock-store'; | |
import thunk from 'redux-thunk'; | |
import rollDice from 'utils/rollDice'; | |
import { | |
initialState, | |
monsterActions, | |
monsterReducer, | |
monsterAttack, | |
getMonsterIsAttacking, | |
getMonsterDamage, | |
getMonsterDice2, | |
getMonsterDice1, | |
getMonsterHealth | |
} from './monsterSlice'; | |
import { RootState } from 'reducers'; | |
const mockStore = configureMockStore([thunk]); | |
jest.mock('utils/rollDice'); | |
describe('Monster Slice', () => { | |
describe('Reducer', () => { | |
test('When initialised, return initial state', () => { | |
expect(monsterReducer(undefined, {} as any)).toBe(initialState); | |
}); | |
test('When a attack start action is fired, reset damage taken', () => { | |
const nextState = monsterReducer( | |
initialState, | |
monsterActions.attackStart() | |
); | |
const rootState = { monster: nextState } as RootState; | |
expect(getMonsterIsAttacking(rootState)).toEqual(true); | |
expect(getMonsterDamage(rootState)).toEqual(initialState.damageTaken); | |
}); | |
test('When a attack end action is fired, set dice values', () => { | |
const nextState = monsterReducer( | |
initialState, | |
monsterActions.attackEnd({ | |
dice1Value: 3, | |
dice2Value: 5 | |
}) | |
); | |
const rootState = { monster: nextState } as RootState; | |
expect(getMonsterIsAttacking(rootState)).toEqual(false); | |
expect(getMonsterDice1(rootState)).toEqual(3); | |
expect(getMonsterDice2(rootState)).toEqual(5); | |
}); | |
test('When a take damage end action is fired, reduce health', () => { | |
const nextState = monsterReducer( | |
initialState, | |
monsterActions.takeDamage(6) | |
); | |
const rootState = { monster: nextState } as RootState; | |
expect(getMonsterDamage(rootState)).toEqual(6); | |
expect(getMonsterHealth(rootState)).toEqual(94); | |
}); | |
}); | |
describe('Thunks', () => { | |
describe('monsterAttack', () => { | |
test('When attacking, roll dice and return the total result', async () => { | |
rollDice.mockReturnValueOnce(3).mockReturnValueOnce(5); | |
const store = mockStore(initialState); | |
const expectedResult = await monsterAttack(store.dispatch); | |
const expectedActions = [ | |
monsterActions.attackStart(), | |
monsterActions.attackEnd({ | |
dice1Value: 3, | |
dice2Value: 5 | |
}) | |
]; | |
expect(expectedResult).toEqual(8); | |
expect(store.getActions()).toEqual(expectedActions); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment