Skip to content

Commit

Permalink
refactor: update unlockTool to accept state instead of mutating it
Browse files Browse the repository at this point in the history
  • Loading branch information
lstebner committed Jan 22, 2024
1 parent a0ba8a2 commit 0b9fc4e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/game-logic/reducers/processLevelUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const processLevelUp = (state, oldLevel) => {
true
)
} else if (levelObject?.unlocksTool) {
state.toolLevels = unlockTool(state.toolLevels, levelObject.unlocksTool)
state = unlockTool(state, levelObject.unlocksTool)
} else if (levelObject?.unlocksStageFocusType) {
state = unlockStage(state, levelObject.unlocksStageFocusType)
}
Expand Down
16 changes: 11 additions & 5 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,14 +974,20 @@ export const computeMarketPositions = (
* @param {farmhand.toolType} toolType
* @returns {farmhand.state}
*/
export const unlockTool = (currentToolLevels, toolType) => {
export const unlockTool = (state, toolType) => {
const { currentToolLevels } = state

if (currentToolLevels[toolType] === toolLevel.UNAVAILABLE) {
return Object.assign({}, currentToolLevels, {
[toolType]: toolLevel.DEFAULT,
})
return {
...state,
currentToolLevels: {
...currentToolLevels,
[toolType]: toolLevel.DEFAULT,
},
}
}

return currentToolLevels
return state
}

/*
Expand Down
30 changes: 26 additions & 4 deletions src/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1087,12 +1087,34 @@ describe('getGrowingPhase', () => {

describe('unlockTool', () => {
it('unlocks the specified tool', () => {
const tools = {
[toolType.SHOVEL]: toolLevel.UNAVAILABLE,
const state = {
currentToolLevels: {
[toolType.SHOVEL]: toolLevel.UNAVAILABLE,
},
}

const { currentToolLevels } = unlockTool(state, toolType.SHOVEL)

expect(currentToolLevels[toolType.SHOVEL]).toEqual(toolLevel.DEFAULT)
})

it('does not alter the rest of the tools', () => {
const state = {
currentToolLevels: {
[toolType.SHOVEL]: toolLevel.UNAVAILABLE,
[toolType.HOE]: toolLevel.DEFAULT,
[toolType.SCYTHE]: toolLevel.GOLD,
},
}

const updatedTools = unlockTool(tools, toolType.SHOVEL)
const { currentToolLevels } = unlockTool(state, toolType.SHOVEL)

expect(updatedTools[toolType.SHOVEL]).toEqual(toolLevel.DEFAULT)
expect(currentToolLevels).toMatchInlineSnapshot(`
Object {
"HOE": "DEFAULT",
"SCYTHE": "GOLD",
"SHOVEL": "DEFAULT",
}
`)
})
})

0 comments on commit 0b9fc4e

Please sign in to comment.