-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize ProviderSelection to Select
- Loading branch information
1 parent
f1eae2d
commit 32a26bf
Showing
6 changed files
with
229 additions
and
56 deletions.
There are no files selected for viewing
37 changes: 0 additions & 37 deletions
37
assets/js/components/ChecksCatalog/ProviderSelection.stories.jsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import React, { useState } from 'react'; | ||
|
||
import { PROVIDERS } from '@lib/model'; | ||
import ProviderLabel from '@components/ProviderLabel'; | ||
import Select from './Select'; | ||
|
||
export default { | ||
title: 'Components/Select', | ||
components: Select, | ||
argTypes: { | ||
optionsName: { | ||
type: 'string', | ||
description: | ||
'The name of the options to be used in the "All `optionsName`" option', | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
options: { | ||
type: 'array', | ||
description: 'The list of options to be rendered in the dropdown', | ||
control: { | ||
type: 'array', | ||
}, | ||
}, | ||
selected: { | ||
type: 'string', | ||
description: 'The currently selected option', | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
optionRenderer: { | ||
description: 'A function to render each option in the dropdown', | ||
table: { | ||
type: { summary: '(item) => item' }, | ||
}, | ||
}, | ||
onChange: { | ||
description: 'A function to be called when the selected option changes', | ||
table: { | ||
type: { summary: '() => {}' }, | ||
}, | ||
}, | ||
className: { | ||
type: 'string', | ||
description: 'Extra classes to be applied to the component', | ||
control: { | ||
type: 'text', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const providerOptionRenderer = (provider) => ( | ||
<ProviderLabel provider={provider} /> | ||
); | ||
|
||
export function ProviderSelection() { | ||
const [selected, setSelected] = useState('azure'); | ||
|
||
return ( | ||
<Select | ||
optionsName="providers" | ||
options={PROVIDERS} | ||
selected={selected} | ||
optionRenderer={providerOptionRenderer} | ||
onChange={setSelected} | ||
/> | ||
); | ||
} | ||
|
||
const emojiOptions = ['foo', 'bar', 'baz', 'qux']; | ||
|
||
const emojiOptionsToLabel = { | ||
foo: '😁 Foo', | ||
bar: '😛 Bar', | ||
baz: '🤪 Baz', | ||
qux: '🧐 Qux', | ||
}; | ||
const itemsOptionRenderer = (item) => <span>{emojiOptionsToLabel[item]}</span>; | ||
|
||
export function EmojiSelection() { | ||
const [selected, setSelected] = useState('all'); | ||
|
||
return ( | ||
<Select | ||
optionsName="emojis" | ||
options={emojiOptions} | ||
selected={selected} | ||
optionRenderer={itemsOptionRenderer} | ||
onChange={setSelected} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
import userEvent from '@testing-library/user-event'; | ||
import Select from './Select'; | ||
|
||
describe('Select Component', () => { | ||
it('should render the `all options` option as selected', () => { | ||
const options = ['option1', 'option2', 'option3']; | ||
render(<Select optionsName="foobars" options={options} selected="all" />); | ||
|
||
options.forEach((option) => { | ||
expect(screen.queryByText(option)).not.toBeInTheDocument(); | ||
}); | ||
expect(screen.getByRole('button')).toHaveTextContent('All foobars'); | ||
}); | ||
|
||
const someOptions = ['option1', 'option2', 'option3']; | ||
it.each(someOptions)( | ||
'should render the selected option', | ||
(selectedOption) => { | ||
render( | ||
<Select | ||
optionsName="foobars" | ||
options={someOptions} | ||
selected={selectedOption} | ||
/> | ||
); | ||
|
||
someOptions | ||
.filter((option) => option !== selectedOption) | ||
.forEach((notSelectedOption) => { | ||
expect(screen.queryByText(notSelectedOption)).not.toBeInTheDocument(); | ||
}); | ||
expect(screen.getByRole('button')).toHaveTextContent(selectedOption); | ||
} | ||
); | ||
|
||
it('should render the options when clicked', async () => { | ||
const user = userEvent.setup(); | ||
const options = ['option1', 'option2', 'option3']; | ||
|
||
render(<Select optionsName="foobars" options={options} selected="all" />); | ||
|
||
expect(screen.getByRole('button')).toHaveTextContent('All foobars'); | ||
|
||
await user.click(screen.getByText('All foobars')); | ||
|
||
expect(screen.getAllByText('All foobars')).toHaveLength(2); | ||
|
||
options.forEach((option) => { | ||
expect(screen.getByText(option)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should render options via a custom option renderer', async () => { | ||
const user = userEvent.setup(); | ||
const options = ['option1', 'option2', 'option3']; | ||
|
||
const optionRenderer = (option) => `custom ${option}`; | ||
|
||
render( | ||
<Select | ||
optionsName="foobars" | ||
options={options} | ||
selected="all" | ||
optionRenderer={optionRenderer} | ||
/> | ||
); | ||
|
||
await user.click(screen.getByText('All foobars')); | ||
|
||
options.forEach((option) => { | ||
expect(screen.getByText(`custom ${option}`)).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should notify about a new option being selected', async () => { | ||
const user = userEvent.setup(); | ||
const mockOnChange = jest.fn(); | ||
|
||
const options = ['option1', 'option2', 'option3']; | ||
|
||
render( | ||
<Select | ||
optionsName="foobars" | ||
options={options} | ||
selected="all" | ||
onChange={mockOnChange} | ||
/> | ||
); | ||
|
||
await user.click(screen.getByText('All foobars')); | ||
await user.click(screen.getByText('option2')); | ||
|
||
expect(mockOnChange).toHaveBeenCalledWith('option2'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Select, { ALL_FILTER } from './Select'; | ||
|
||
export { ALL_FILTER }; | ||
|
||
export default Select; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters