Skip to content

Commit

Permalink
fix: update state from wave app when initial and new value are the same
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-mihok authored and mturoci committed Aug 2, 2023
1 parent 68a8754 commit eaf47d1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
24 changes: 16 additions & 8 deletions ui/src/toggle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe('Toggle.tsx', () => {
expect(queryByTestId(name)).toBeInTheDocument()
})

it('Sets args on click', () => {
const { getByTestId } = render(<XToggle model={toggleProps} />)
fireEvent.click(getByTestId(name))

expect(wave.args[name]).toBe(true)
})

it('Calls sync when trigger is on', () => {
const pushMock = jest.fn()
const { getByTestId } = render(<XToggle model={{ ...toggleProps, trigger: true }} />)
Expand All @@ -51,18 +58,19 @@ describe('Toggle.tsx', () => {
expect(pushMock).toHaveBeenCalledTimes(0)
})

it('Sets args on click', () => {
const { getByTestId } = render(<XToggle model={toggleProps} />)
fireEvent.click(getByTestId(name))

expect(wave.args[name]).toBe(true)
})

it('Set args when value is updated', () => {
it('Set args when value is updated to different value', () => {
const { rerender } = render(<XToggle model={toggleProps} />)
expect(wave.args[name]).toBe(false)
rerender(<XToggle model={{ name, value: true }} />)
expect(wave.args[name]).toBe(true)
})

it('Set args when value is updated to initial value', () => {
const { getByTestId, rerender } = render(<XToggle model={toggleProps} />)
expect(wave.args[name]).toBe(false)
fireEvent.click(getByTestId(name))
expect(wave.args[name]).toBe(true)
rerender(<XToggle model={{ name, value: false }} />)
expect(wave.args[name]).toBe(false)
})
})
2 changes: 2 additions & 0 deletions ui/src/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export const
onChange = React.useCallback((_e?: React.FormEvent<HTMLElement>, checked?: B) => {
wave.args[name] = !!checked
setChecked(!!checked)
m.value = !!checked
if (trigger) wave.push()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [name, trigger])

React.useEffect(() => {
Expand Down

0 comments on commit eaf47d1

Please sign in to comment.