select.spec.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. import { useCallback, FormEvent } from 'react'
  2. import OLButton from '@/features/ui/components/ol/ol-button'
  3. import OLForm from '@/features/ui/components/ol/ol-form'
  4. import OLFormControl from '@/features/ui/components/ol/ol-form-control'
  5. import {
  6. Select,
  7. SelectProps,
  8. } from '../../../../frontend/js/shared/components/select'
  9. const testData = [1, 2, 3].map(index => ({
  10. key: index,
  11. value: `Demo item ${index}`,
  12. sub: `Subtitle ${index}`,
  13. }))
  14. type RenderProps = Partial<SelectProps<(typeof testData)[number]>> & {
  15. onSubmit?: (formData: object) => void
  16. }
  17. function render(props: RenderProps) {
  18. const submitHandler = (event: FormEvent<HTMLFormElement>) => {
  19. event.preventDefault()
  20. if (props.onSubmit) {
  21. const formData = new FormData(event.target as HTMLFormElement)
  22. // a plain object is more convenient to work later with assertions
  23. props.onSubmit(Object.fromEntries(formData.entries()))
  24. }
  25. }
  26. cy.mount(
  27. <div
  28. style={{
  29. display: 'flex',
  30. alignItems: 'center',
  31. justifyContent: 'center',
  32. height: '100vh',
  33. }}
  34. >
  35. <form onSubmit={submitHandler}>
  36. <Select
  37. items={testData}
  38. itemToString={x => String(x?.value)}
  39. label={props.label}
  40. name="select_control"
  41. defaultText={props.defaultText}
  42. defaultItem={props.defaultItem}
  43. itemToSubtitle={props.itemToSubtitle}
  44. itemToKey={x => String(x.key)}
  45. onSelectedItemChanged={props.onSelectedItemChanged}
  46. selected={props.selected}
  47. disabled={props.disabled}
  48. itemToDisabled={props.itemToDisabled}
  49. optionalLabel={props.optionalLabel}
  50. loading={props.loading}
  51. selectedIcon={props.selectedIcon}
  52. />
  53. <button type="submit">submit</button>
  54. </form>
  55. </div>
  56. )
  57. }
  58. describe('<Select />', function () {
  59. describe('initial rendering', function () {
  60. it('renders default text', function () {
  61. render({ defaultText: 'Choose an item' })
  62. cy.findByTestId('spinner').should('not.exist')
  63. cy.findByRole('combobox').should('have.value', 'Choose an item')
  64. })
  65. it('renders default item', function () {
  66. render({ defaultItem: testData[2] })
  67. cy.findByRole('combobox').should('have.value', 'Demo item 3')
  68. })
  69. it('default item takes precedence over default text', function () {
  70. render({ defaultText: 'Choose an item', defaultItem: testData[2] })
  71. cy.findByRole('combobox').should('have.value', 'Demo item 3')
  72. })
  73. it('renders label', function () {
  74. render({
  75. defaultText: 'Choose an item',
  76. label: 'test label',
  77. optionalLabel: false,
  78. })
  79. cy.findByRole('combobox', { name: 'test label' })
  80. cy.findByRole('combobox', { name: '(Optional)' }).should('not.exist')
  81. })
  82. it('renders optional label', function () {
  83. render({
  84. defaultText: 'Choose an item',
  85. label: 'test label',
  86. optionalLabel: true,
  87. })
  88. cy.findByRole('combobox', { name: 'test label (Optional)' })
  89. })
  90. it('renders a spinner while loading when there is a label', function () {
  91. render({
  92. defaultText: 'Choose an item',
  93. label: 'test label',
  94. loading: true,
  95. })
  96. cy.findByTestId('spinner')
  97. })
  98. it('does not render a spinner while loading if there is no label', function () {
  99. render({
  100. defaultText: 'Choose an item',
  101. loading: true,
  102. })
  103. cy.findByTestId('spinner').should('not.exist')
  104. })
  105. })
  106. describe('items rendering', function () {
  107. it('renders all items', function () {
  108. render({ defaultText: 'Choose an item' })
  109. cy.findByRole('combobox').click()
  110. cy.findByRole('option', { name: 'Demo item 1' })
  111. cy.findByRole('option', { name: 'Demo item 2' })
  112. cy.findByRole('option', { name: 'Demo item 3' })
  113. })
  114. it('renders subtitles', function () {
  115. render({
  116. defaultText: 'Choose an item',
  117. itemToSubtitle: x => String(x?.sub),
  118. })
  119. cy.findByRole('combobox').click()
  120. cy.findByRole('option', { name: 'Demo item 1 Subtitle 1' })
  121. cy.findByRole('option', { name: 'Demo item 2 Subtitle 2' })
  122. cy.findByRole('option', { name: 'Demo item 3 Subtitle 3' })
  123. })
  124. })
  125. describe('item selection', function () {
  126. it('cannot select an item when disabled', function () {
  127. render({ defaultText: 'Choose an item', disabled: true })
  128. cy.findByRole('combobox').click({
  129. force: true,
  130. })
  131. cy.findByRole('option', { name: 'Demo item 1' }).should('not.exist')
  132. cy.findByRole('option', { name: 'Demo item 2' }).should('not.exist')
  133. cy.findByRole('option', { name: 'Demo item 3' }).should('not.exist')
  134. cy.findByRole('combobox').should('have.value', 'Choose an item')
  135. })
  136. it('renders only the selected item after selection', function () {
  137. render({ defaultText: 'Choose an item' })
  138. cy.findByRole('combobox').click()
  139. cy.findByRole('option', { name: 'Demo item 1' })
  140. cy.findByRole('option', { name: 'Demo item 2' })
  141. cy.findByRole('option', { name: 'Demo item 3' }).click()
  142. cy.findByRole('combobox').should('not.have.value', 'Choose an item')
  143. cy.findByRole('option', { name: 'Demo item 1' }).should('not.exist')
  144. cy.findByRole('option', { name: 'Demo item 2' }).should('not.exist')
  145. cy.findByRole('combobox').should('have.value', 'Demo item 3')
  146. })
  147. it('invokes callback after selection', function () {
  148. const selectionHandler = cy.stub().as('selectionHandler')
  149. render({
  150. defaultText: 'Choose an item',
  151. onSelectedItemChanged: selectionHandler,
  152. })
  153. cy.findByRole('combobox').click()
  154. cy.findByRole('option', { name: 'Demo item 2' }).click()
  155. cy.get('@selectionHandler').should(
  156. 'have.been.calledOnceWith',
  157. testData[1]
  158. )
  159. })
  160. })
  161. describe('when the form is submitted', function () {
  162. it('populates FormData with the default selected item', function () {
  163. const submitHandler = cy.stub().as('submitHandler')
  164. render({ defaultItem: testData[1], onSubmit: submitHandler })
  165. cy.findByText('submit').click()
  166. cy.get('@submitHandler').should('have.been.calledOnceWith', {
  167. select_control: 'Demo item 2',
  168. })
  169. })
  170. it('populates FormData with the selected item', function () {
  171. const submitHandler = cy.stub().as('submitHandler')
  172. render({ defaultItem: testData[1], onSubmit: submitHandler })
  173. cy.findByRole('combobox').click() // open dropdown
  174. cy.findByText('Demo item 3').click() // choose a different item
  175. cy.findByText('submit').click()
  176. cy.get('@submitHandler').should('have.been.calledOnceWith', {
  177. select_control: 'Demo item 3',
  178. })
  179. })
  180. it('does not populate FormData when no item is selected', function () {
  181. const submitHandler = cy.stub().as('submitHandler')
  182. render({ defaultText: 'Choose an item', onSubmit: submitHandler })
  183. cy.findByText('submit').click()
  184. cy.get('@submitHandler').should('have.been.calledOnceWith', {})
  185. })
  186. })
  187. describe('with react-bootstrap forms', function () {
  188. type FormWithSelectProps = {
  189. onSubmit: (formData: object) => void
  190. }
  191. const FormWithSelect = ({ onSubmit }: FormWithSelectProps) => {
  192. const selectComponent = useCallback(
  193. () => (
  194. <Select
  195. name="select_control"
  196. items={testData}
  197. defaultItem={testData[0]}
  198. itemToString={x => String(x?.value)}
  199. itemToKey={x => String(x.key)}
  200. />
  201. ),
  202. []
  203. )
  204. function handleSubmit(event: FormEvent<HTMLFormElement>) {
  205. event.preventDefault()
  206. const formData = new FormData(event.target as HTMLFormElement)
  207. // a plain object is more convenient to work later with assertions
  208. onSubmit(Object.fromEntries(formData.entries()))
  209. }
  210. return (
  211. <OLForm onSubmit={handleSubmit}>
  212. <OLFormControl as={selectComponent} />
  213. <OLButton type="submit">submit</OLButton>
  214. </OLForm>
  215. )
  216. }
  217. it('populates FormData with the selected item when the form is submitted', function () {
  218. const submitHandler = cy.stub().as('submitHandler')
  219. cy.mount(
  220. <div
  221. style={{
  222. display: 'flex',
  223. alignItems: 'center',
  224. justifyContent: 'center',
  225. height: '100vh',
  226. }}
  227. >
  228. <FormWithSelect onSubmit={submitHandler} />
  229. </div>
  230. )
  231. cy.findByRole('combobox').click() // open dropdown
  232. cy.findByRole('option', { name: 'Demo item 3' }).click() // choose a different item
  233. cy.findByText('submit').click()
  234. cy.get('@submitHandler').should('have.been.calledOnceWith', {
  235. select_control: 'Demo item 3',
  236. })
  237. })
  238. })
  239. describe('keyboard navigation', function () {
  240. it('can select an item using the keyboard', function () {
  241. render({ defaultText: 'Choose an item' })
  242. cy.findByRole('combobox').type('{Enter}{downArrow}{Enter}', {
  243. force: true,
  244. })
  245. cy.findByRole('combobox').should('exist')
  246. cy.findByRole('option', { name: 'Demo item 2' }).should('not.exist')
  247. })
  248. })
  249. describe('selectedIcon', function () {
  250. it('renders a selected icon if the prop is set', function () {
  251. render({
  252. defaultText: 'Choose an item',
  253. selectedIcon: true,
  254. })
  255. cy.findByRole('combobox').click()
  256. cy.findByRole('option', { name: 'Demo item 1' }).click()
  257. cy.findByRole('combobox').click()
  258. cy.findByText('check').should('exist')
  259. })
  260. it('renders no selected icon if the prop is not set', function () {
  261. render({
  262. defaultText: 'Choose an item',
  263. selectedIcon: false,
  264. })
  265. cy.findByRole('combobox').click()
  266. cy.findByRole('option', { name: 'Demo item 1' }).click()
  267. cy.findByRole('combobox').click()
  268. cy.findByText('check').should('not.exist')
  269. })
  270. })
  271. describe('itemToDisabled', function () {
  272. it('prevents selecting a disabled item', function () {
  273. render({
  274. defaultText: 'Choose an item',
  275. itemToDisabled: x => x?.key === 2,
  276. })
  277. cy.findByRole('combobox').click()
  278. cy.findByRole('option', { name: 'Demo item 2' }).click({ force: true })
  279. // still showing other list items
  280. cy.findByRole('option', { name: 'Demo item 3' }).should('exist')
  281. cy.findByRole('option', { name: 'Demo item 1' }).click()
  282. // clicking an enabled item dismisses the list
  283. cy.findByRole('option', { name: 'Demo item 3' }).should('not.exist')
  284. })
  285. })
  286. describe('selected', function () {
  287. it('shows the item provided in the selected prop', function () {
  288. render({
  289. defaultText: 'Choose an item',
  290. selected: testData[1],
  291. })
  292. cy.findByRole('combobox').should('have.value', 'Demo item 2')
  293. })
  294. it('should show default text when selected is null', function () {
  295. render({
  296. selected: null,
  297. defaultText: 'Choose an item',
  298. })
  299. cy.findByRole('combobox').should('have.value', 'Choose an item')
  300. })
  301. })
  302. })