Skip to content

Commit

Permalink
chore: refactor, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-mihok committed Aug 7, 2023
1 parent f8a95b5 commit f46dcde
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
6 changes: 4 additions & 2 deletions ui/src/dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,15 @@ describe('Dropdown.tsx', () => {
{ name: 'E', label: 'Choice E' },
{ name: 'F', label: 'Choice F' },
],
{ getByTestId, getByText, rerender } = render(<XDropdown model={{ ...dialogProps, choices }} />)
{ getByTestId, getByText, queryByText, rerender } = render(<XDropdown model={{ ...dialogProps, choices }} />)

fireEvent.click(getByTestId(name))
expect(getByText('Choice A')).toBeInTheDocument()

rerender(<XDropdown model={{ ...dialogProps, choices: updatedChoices }} />)
fireEvent.click(getByTestId(name))

expect(queryByText('Choice A')).not.toBeInTheDocument()
expect(getByText('Choice D')).toBeInTheDocument()
expect(getByText('Choice E')).toBeInTheDocument()
expect(getByText('Choice F')).toBeInTheDocument()
Expand Down Expand Up @@ -834,14 +835,15 @@ describe('Dropdown.tsx', () => {
{ name: 'E', label: 'Choice E' },
{ name: 'F', label: 'Choice F' },
],
{ getByTestId, getByText, rerender } = render(<XDropdown model={{ ...dialogProps, choices, values: ['A'] }} />)
{ getByTestId, getByText, queryByText, rerender } = render(<XDropdown model={{ ...dialogProps, choices, values: ['A'] }} />)

fireEvent.click(getByTestId(name))
expect(getByText('Choice A')).toBeInTheDocument()

rerender(<XDropdown model={{ ...dialogProps, choices: updatedChoices, values: ['D'] }} />)
fireEvent.click(getByTestId(name))

expect(queryByText('Choice A')).not.toBeInTheDocument()
expect(getByText('Choice D')).toBeInTheDocument()
expect(getByText('Choice E')).toBeInTheDocument()
expect(getByText('Choice F')).toBeInTheDocument()
Expand Down
22 changes: 6 additions & 16 deletions ui/src/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,9 @@ const
}

React.useEffect(() => {
if (model.value !== undefined) {
wave.args[name] = model.value ?? null
setItems(items => items.map(i => ({ ...i, checked: model.value === i.name })))
}
}, [name, model.value, setItems])

// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => setItems(choicesToItems(choices, model.value)), [choices])
wave.args[name] = model.value ?? null
setItems(choicesToItems(choices, model.value))
}, [name, model.value, choices, setItems])

return (
<>
Expand Down Expand Up @@ -277,14 +272,9 @@ const
}

React.useEffect(() => {
if (values) {
wave.args[name] = values
setItems(items => items.map(i => ({ ...i, checked: values.includes(i.name) })))
}
}, [name, values, setItems])

// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => setItems(choicesToItems(choices, values)), [choices])
wave.args[name] = values
setItems(choicesToItems(choices, values))
}, [name, values, choices, setItems])

return (
<>
Expand Down

0 comments on commit f46dcde

Please sign in to comment.