Skip to content

Commit

Permalink
feat: Allow dynamic value change for checklist #1154 (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-mihok authored Aug 4, 2023
1 parent 9d22c12 commit 04df281
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
29 changes: 29 additions & 0 deletions ui/src/checklist.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,35 @@ describe('Checklist.tsx', () => {
expect(wave.args[name]).toMatchObject(['Choice1', 'Choice2', 'Choice3'])
})

it('Set args when value is updated to different value', () => {
const { rerender } = render(<XChecklist model={checklistProps} />)
expect(wave.args[name]).toMatchObject([])
rerender(<XChecklist model={{ ...checklistProps, values: ['Choice1'] }} />)
expect(wave.args[name]).toMatchObject(['Choice1'])
})

it('Set args when value is updated to initial value', () => {
const { getByText, rerender } = render(<XChecklist model={{ ...checklistProps, values: ['Choice1'] }} />)
expect(wave.args[name]).toMatchObject(['Choice1'])

fireEvent.click(getByText('Choice2').parentElement!)
expect(wave.args[name]).toMatchObject(['Choice1', 'Choice2'])

rerender(<XChecklist model={{ ...checklistProps, values: ['Choice1'] }} />)
expect(wave.args[name]).toMatchObject(['Choice1'])
})

it('Updates choices', () => {
const { rerender, getByText } = render(<XChecklist model={checklistProps} />)
expect(getByText('Choice1')).toBeInTheDocument()
expect(getByText('Choice2')).toBeInTheDocument()
expect(getByText('Choice3')).toBeInTheDocument()
rerender(<XChecklist model={{ ...checklistProps, choices: [{ name: 'Choice3' }, { name: 'Choice4' }] }} />)

expect(getByText('Choice3')).toBeInTheDocument()
expect(getByText('Choice4')).toBeInTheDocument()
})

it('Sets all choices as args after select all', () => {
const { getByText } = render(<XChecklist model={checklistProps} />)
fireEvent.click(getByText('Select All'))
Expand Down
3 changes: 1 addition & 2 deletions ui/src/checklist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export const
styles={{ root: { marginBottom: 4 }, checkmark: { display: 'flex' } }} // Fix: Center the checkmark in the checkbox.
/>
))
// eslint-disable-next-line react-hooks/exhaustive-deps
React.useEffect(() => { wave.args[m.name] = m.values || [] }, [])
React.useEffect(() => { wave.args[m.name] = m.values || [] }, [m.name, m.values])
React.useEffect(() => { setChoices(getMappedChoices()) }, [getMappedChoices, m.choices])
return (
<div data-test={m.name}>
Expand Down

0 comments on commit 04df281

Please sign in to comment.