Skip to content

Commit

Permalink
Added Locos reducer and saga tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dcyoung-dev committed Jan 18, 2024
1 parent a23a574 commit 851dd01
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
91 changes: 91 additions & 0 deletions src/__tests__/states/reducers/locos_reducer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {rostersReducer} from "../../../states/reducers/locos_reducer";
import {addOrUpdateLoco} from "../../../states/actions/locos";
import {Loco, LocosState} from "../../../types";
import {userResetState} from "../../../states/actions/stores";

describe("Locos reducer", () => {
it("has initial state", () => {
const locosState = rostersReducer(undefined, {type: 'INIT'})

expect(locosState).toEqual({entities: {}})
})

it("resets all state on USER_RESET_STATE", () => {
const action = userResetState()
const initialLoco: Loco = {
cabId: 1,
name: 'Initial Loco',
throttle: {direction: 1, speed: 0},
functionButtons: {},
sync: {
rosterItemAt: null,
rosterListAt: null
},
id: "",
}

const initialState: LocosState = {
entities: {
1: initialLoco
}
}
const locosState = rostersReducer(initialState, action)

expect(locosState).toEqual({entities: {}})
})

it("adds a Loco on UPDATE_POWER_STATE", () => {
const action = addOrUpdateLoco({cabId: 1, name: 'test'})
const locosState = rostersReducer(undefined, action)

const expectedLoco = {
cabId: 1,
name: 'test',
throttle: expect.any(Object),
sync: expect.any(Object),
functionButtons: expect.any(Object),
id: expect.any(String),
}

expect(locosState).toEqual({entities: {1: expectedLoco}})
})

it("updates an existing Loco on UPDATE_POWER_STATE", () => {
const initialLoco: Loco = {
cabId: 1,
name: 'Initial Loco',
throttle: {direction: 1, speed: 0},
functionButtons: {},
sync: {
rosterItemAt: null,
rosterListAt: null
},
id: "",
}

const initialState: LocosState = {
entities: {
1: initialLoco
}
}

const updatedLoco = {
cabId: 1,
name: 'Updated Loco',
}

const action = addOrUpdateLoco(updatedLoco)
const locosState = rostersReducer(initialState, action)

const expectedLoco = {
cabId: 1,
name: 'Updated Loco',
throttle: expect.any(Object),
sync: expect.any(Object),
functionButtons: expect.any(Object),
id: expect.any(String),
}

expect(locosState).toEqual({entities: {1: expectedLoco}})
})
})
16 changes: 14 additions & 2 deletions src/__tests__/states/sagas/locos_saga.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import {expectSaga} from "redux-saga-test-plan";
import commandSaga from "../../../states/actions";
import {addOrUpdateLoco, newLocoFormSubmit} from "../../../states/actions/locos";
import {addOrUpdateLoco, newLocoFormSubmit, userPopulateRoster} from "../../../states/actions/locos";
import {commandSend} from "../../../states/actions/commands";

describe("User adds a new Loco", () => {
describe("NEW_LOCO_FORM_SUBMIT", () => {
it("puts ADD_OR_UPDATE_LOCO", () => {
return expectSaga(commandSaga)
.dispatch(newLocoFormSubmit({cabId: 1, name: 'test'}))
.put(addOrUpdateLoco({cabId: 1, name: 'test'}))
.put(addOrUpdateLoco({cabId: 1, name: 'test'})) // tested by locos reducer
.silentRun();
});
});
})

describe("User populates Locos from Roster", () => {
describe("USER_POPULATE_ROSTER", () => {
it("puts COMMAND_SEND <J>", () => {
return expectSaga(commandSaga)
.dispatch(userPopulateRoster())
.put(commandSend("<J>"))
.silentRun()
})
})
})

0 comments on commit 851dd01

Please sign in to comment.