Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
feat: write test for current color bug in setColor with callback(#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
junhoyeo committed Jul 11, 2021
1 parent c60eb02 commit d3af392
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/useColor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,31 @@ describe('Set color', () => {
expect(newColor.rgb).toEqual({ r: 0, g: 242, b: 234 })
expect(newColor.strings.hex).toEqual(expectedColor)
})

it('Should pass current color when setColor takes a function', () => {
// given
const { result } = renderHook(() => useColor({ r: 0, g: 0, b: 0, a: 0 }))
const [_, setColor] = result.current

// when
act(() =>
setColor((currentColor) => {
// then
expect(currentColor).toStrictEqual({ r: 0, g: 0, b: 0, a: 0 })
return { r: 255, g: 255, b: 255, a: 1 }
}),
)

const [newColor, newSetColor] = result.current
expect(newColor.rgba).toEqual({ r: 255, g: 255, b: 255, a: 1 })

act(() =>
newSetColor((currentColor) => {
expect(currentColor).toStrictEqual({ r: 255, g: 255, b: 255, a: 1 })
return { r: 200, g: 200, b: 200, a: 0.5 }
}),
)
})
})

it('Update color', () => {
Expand Down

0 comments on commit d3af392

Please sign in to comment.