Skip to content

Commit e1462a1

Browse files
Add test helpers for Combobox component (#987)
1 parent f8def51 commit e1462a1

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- `ActionListItem` accepts `actionsButtonLabel` prop to help with testing
1616
- `ComboboxList` and `ComboboxOption` now both support a custom `indicator`
1717
- `Select` and `SelectMulti` also support `indicator` at both the component and option level
18+
- Test helper functions for `Combobox` component using react-testing-library
1819

1920
### Changed
2021

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2020 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
import {
28+
closeCombobox,
29+
getAllComboboxOptionText,
30+
getComboboxOptions,
31+
getComboboxOptionText,
32+
openCombobox,
33+
renderWithTheme,
34+
} from '@looker/components-test-utils'
35+
import { screen } from '@testing-library/react'
36+
import React from 'react'
37+
import { Combobox, ComboboxInput, ComboboxList, ComboboxOption } from './'
38+
39+
describe('Combobox helpers', () => {
40+
const renderCombobox = () =>
41+
renderWithTheme(
42+
<Combobox onChange={jest.fn()}>
43+
<ComboboxInput placeholder="Type here" />
44+
<ComboboxList>
45+
<ComboboxOption label="Foo" value="101" onClick={jest.fn()} />
46+
<ComboboxOption label="Bar" value="102" />
47+
</ComboboxList>
48+
</Combobox>
49+
)
50+
51+
test('openCombobox opens the combobox using the provided placeholder text', () => {
52+
renderCombobox()
53+
openCombobox('Type here')
54+
55+
expect(screen.getByText('Foo')).toBeVisible()
56+
57+
closeCombobox()
58+
})
59+
60+
test('closeCombobox closes any open comboboxes', () => {
61+
renderCombobox()
62+
openCombobox('Type here')
63+
64+
expect(screen.getByText('Foo')).toBeVisible()
65+
66+
closeCombobox()
67+
68+
expect(screen.queryByText('Foo')).toBeNull()
69+
})
70+
71+
test('getComboboxOptions returns the expected number of options for an open Combobox', () => {
72+
renderCombobox()
73+
openCombobox('Type here')
74+
75+
expect(getComboboxOptions()).toHaveLength(2)
76+
77+
closeCombobox()
78+
})
79+
80+
test('getComboboxOptionText returns the expected text content of the provided option element', () => {
81+
renderCombobox()
82+
openCombobox('Type here')
83+
84+
const options = getComboboxOptions()
85+
expect(getComboboxOptionText(options[0])).toEqual('Foo')
86+
expect(getComboboxOptionText(options[1])).toEqual('Bar')
87+
88+
closeCombobox()
89+
})
90+
91+
test('getAllComboboxOptionText returns the expected text content of the provided option element', () => {
92+
renderCombobox()
93+
openCombobox('Type here')
94+
95+
const optionTexts = getAllComboboxOptionText()
96+
expect(optionTexts).toMatchInlineSnapshot(`
97+
Array [
98+
"Foo",
99+
"Bar",
100+
]
101+
`)
102+
103+
closeCombobox()
104+
})
105+
})
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2020 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
import { fireEvent, getNodeText, screen } from '@testing-library/react'
28+
29+
export const openCombobox = (placeholderText: string) =>
30+
fireEvent.mouseDown(screen.getByPlaceholderText(placeholderText))
31+
32+
export const closeCombobox = () => fireEvent.click(document)
33+
34+
export const getComboboxOptions = () => screen.getAllByRole('option')
35+
36+
export const getComboboxOptionText = (el: HTMLElement) =>
37+
getNodeText(el.children[1] as HTMLElement)
38+
39+
export const getAllComboboxOptionText = () =>
40+
getComboboxOptions().map(getComboboxOptionText)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2020 Looker Data Sciences, Inc.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
export {
28+
closeCombobox,
29+
getAllComboboxOptionText,
30+
getComboboxOptions,
31+
getComboboxOptionText,
32+
openCombobox,
33+
} from './comboboxHelpers'

packages/test-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
export { ReactWrapper } from 'enzyme'
2828
export * from './create_with_theme'
2929
export * from './snapshot'
30+
export * from './helpers/react-testing-library'

0 commit comments

Comments
 (0)