@instructure/ui-checkbox 8.46.1 → 8.46.2-snapshot-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/es/Checkbox/{locator.js → CheckboxFacade/__new-tests__/CheckboxFacade.test.js} +20 -3
  3. package/es/Checkbox/{CheckboxLocator.js → ToggleFacade/__new-tests__/ToggleFacade.test.js} +20 -5
  4. package/es/Checkbox/__new-tests__/Checkbox.test.js +210 -0
  5. package/es/CheckboxGroup/__new-tests__/CheckboxGroup.test.js +196 -0
  6. package/lib/Checkbox/{locator.js → CheckboxFacade/__new-tests__/CheckboxFacade.test.js} +21 -13
  7. package/lib/Checkbox/{CheckboxLocator.js → ToggleFacade/__new-tests__/ToggleFacade.test.js} +21 -10
  8. package/lib/Checkbox/__new-tests__/Checkbox.test.js +213 -0
  9. package/lib/CheckboxGroup/__new-tests__/CheckboxGroup.test.js +198 -0
  10. package/package.json +22 -19
  11. package/src/{CheckboxGroup/CheckboxGroupLocator.ts → Checkbox/CheckboxFacade/__new-tests__/CheckboxFacade.test.tsx} +22 -4
  12. package/src/Checkbox/{CheckboxLocator.ts → ToggleFacade/__new-tests__/ToggleFacade.test.tsx} +22 -4
  13. package/src/Checkbox/__new-tests__/Checkbox.test.tsx +221 -0
  14. package/src/CheckboxGroup/__new-tests__/CheckboxGroup.test.tsx +202 -0
  15. package/tsconfig.build.json +2 -2
  16. package/tsconfig.build.tsbuildinfo +1 -1
  17. package/types/Checkbox/CheckboxFacade/__new-tests__/CheckboxFacade.test.d.ts +2 -0
  18. package/types/Checkbox/CheckboxFacade/__new-tests__/CheckboxFacade.test.d.ts.map +1 -0
  19. package/types/Checkbox/ToggleFacade/__new-tests__/ToggleFacade.test.d.ts +2 -0
  20. package/types/Checkbox/ToggleFacade/__new-tests__/ToggleFacade.test.d.ts.map +1 -0
  21. package/types/Checkbox/__new-tests__/Checkbox.test.d.ts +2 -0
  22. package/types/Checkbox/__new-tests__/Checkbox.test.d.ts.map +1 -0
  23. package/types/CheckboxGroup/__new-tests__/CheckboxGroup.test.d.ts +2 -0
  24. package/types/CheckboxGroup/__new-tests__/CheckboxGroup.test.d.ts.map +1 -0
  25. package/es/CheckboxGroup/CheckboxGroupLocator.js +0 -29
  26. package/es/CheckboxGroup/locator.js +0 -27
  27. package/lib/CheckboxGroup/CheckboxGroupLocator.js +0 -35
  28. package/lib/CheckboxGroup/locator.js +0 -38
  29. package/src/Checkbox/locator.ts +0 -28
  30. package/src/CheckboxGroup/locator.ts +0 -28
  31. package/types/Checkbox/CheckboxLocator.d.ts +0 -566
  32. package/types/Checkbox/CheckboxLocator.d.ts.map +0 -1
  33. package/types/Checkbox/locator.d.ts +0 -4
  34. package/types/Checkbox/locator.d.ts.map +0 -1
  35. package/types/CheckboxGroup/CheckboxGroupLocator.d.ts +0 -566
  36. package/types/CheckboxGroup/CheckboxGroupLocator.d.ts.map +0 -1
  37. package/types/CheckboxGroup/locator.d.ts +0 -4
  38. package/types/CheckboxGroup/locator.d.ts.map +0 -1
@@ -0,0 +1,221 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
27
+ import userEvent from '@testing-library/user-event'
28
+ import '@testing-library/jest-dom/extend-expect'
29
+
30
+ import { runAxeCheck } from '@instructure/ui-axe-check'
31
+ import { Checkbox } from '../index'
32
+ import { CheckboxProps } from '../props'
33
+
34
+ const TEST_VALUE = 'test-value'
35
+ const TEST_NAME = 'test-name'
36
+ const TEST_LABEL = 'test-label'
37
+
38
+ const initProps = {
39
+ label: TEST_LABEL,
40
+ defaultChecked: true,
41
+ value: TEST_VALUE,
42
+ name: TEST_NAME
43
+ }
44
+
45
+ const renderCheckbox = (props?: Partial<CheckboxProps>) => {
46
+ const allProps: CheckboxProps = {
47
+ ...initProps,
48
+ ...props
49
+ }
50
+ return render(<Checkbox {...allProps} />)
51
+ }
52
+
53
+ describe('<Checkbox />', () => {
54
+ it('renders an input with type "checkbox"', () => {
55
+ renderCheckbox()
56
+ const inputElem = screen.getByRole('checkbox')
57
+
58
+ expect(inputElem).toBeInTheDocument()
59
+ expect(inputElem.tagName).toBe('INPUT')
60
+ expect(inputElem).toHaveAttribute('type', 'checkbox')
61
+ })
62
+
63
+ it('`simple` variant only displays a checkmark when checked', async () => {
64
+ const { container } = renderCheckbox({
65
+ variant: 'simple',
66
+ defaultChecked: false
67
+ })
68
+ const checkboxElement = container.querySelector('input[type="checkbox"]')
69
+ const svgElement = container.querySelector('svg')
70
+
71
+ expect(svgElement).not.toBeInTheDocument()
72
+
73
+ userEvent.click(checkboxElement!)
74
+ await waitFor(() => {
75
+ const svgElementAfterClick = container.querySelector('svg')
76
+ expect(svgElementAfterClick).toBeInTheDocument()
77
+ })
78
+ })
79
+
80
+ it('`simple` variant supports indeterminate/mixed state', () => {
81
+ renderCheckbox({ variant: 'simple', indeterminate: true })
82
+
83
+ const inputElem = screen.getByRole('checkbox')
84
+
85
+ expect(inputElem).toBeInTheDocument()
86
+ expect(inputElem).toHaveAttribute('aria-checked', 'mixed')
87
+ })
88
+
89
+ describe('events', () => {
90
+ it('when clicked, fires onClick and onChange events', async () => {
91
+ const onClick = jest.fn()
92
+ const onChange = jest.fn()
93
+ renderCheckbox({ onClick, onChange })
94
+ const checkboxElement = screen.getByRole('checkbox')
95
+
96
+ userEvent.click(checkboxElement)
97
+
98
+ await waitFor(() => {
99
+ expect(onClick).toHaveBeenCalled()
100
+ expect(onChange).toHaveBeenCalled()
101
+ })
102
+ })
103
+
104
+ it('when clicked, does not call onClick or onChange when disabled', async () => {
105
+ const onClick = jest.fn()
106
+ const onChange = jest.fn()
107
+ renderCheckbox({ onClick, onChange, disabled: true })
108
+ const checkboxElement = screen.getByRole('checkbox')
109
+
110
+ fireEvent.click(checkboxElement)
111
+
112
+ await waitFor(() => {
113
+ expect(onClick).not.toHaveBeenCalled()
114
+ expect(onChange).not.toHaveBeenCalled()
115
+ expect(checkboxElement).toBeDisabled()
116
+ })
117
+ })
118
+
119
+ it('when clicked, does not call onClick or onChange when readOnly', async () => {
120
+ const onClick = jest.fn()
121
+ const onChange = jest.fn()
122
+ renderCheckbox({ onClick, onChange, readOnly: true })
123
+ const checkboxElement = screen.getByRole('checkbox')
124
+
125
+ fireEvent.click(checkboxElement)
126
+
127
+ await waitFor(() => {
128
+ expect(onClick).not.toHaveBeenCalled()
129
+ expect(onChange).not.toHaveBeenCalled()
130
+ })
131
+ })
132
+
133
+ it('calls onChange when enter key is pressed', async () => {
134
+ const onChange = jest.fn()
135
+ renderCheckbox({ onChange })
136
+ const checkboxElement = screen.getByRole('checkbox')
137
+
138
+ userEvent.type(checkboxElement, '{enter}')
139
+
140
+ await waitFor(() => {
141
+ expect(onChange).toHaveBeenCalled()
142
+ })
143
+ })
144
+
145
+ it('responds to onBlur event', async () => {
146
+ const onBlur = jest.fn()
147
+ renderCheckbox({ onBlur })
148
+
149
+ userEvent.tab()
150
+ userEvent.tab()
151
+
152
+ await waitFor(() => {
153
+ expect(onBlur).toHaveBeenCalled()
154
+ })
155
+ })
156
+
157
+ it('responds to onFocus event', async () => {
158
+ const onFocus = jest.fn()
159
+ renderCheckbox({ onFocus })
160
+
161
+ userEvent.tab()
162
+
163
+ await waitFor(() => {
164
+ expect(onFocus).toHaveBeenCalled()
165
+ })
166
+ })
167
+
168
+ it('focuses with the focus helper', () => {
169
+ const checkboxRef = React.createRef<Checkbox>()
170
+ render(<Checkbox ref={checkboxRef} {...initProps} />)
171
+ const checkboxElement = screen.getByRole('checkbox')
172
+
173
+ expect(checkboxElement).not.toHaveFocus()
174
+
175
+ checkboxRef.current?.focus()
176
+
177
+ expect(checkboxElement).toHaveFocus()
178
+ })
179
+
180
+ it('calls onMouseOver', async () => {
181
+ const onMouseOver = jest.fn()
182
+ renderCheckbox({ onMouseOver })
183
+ const checkboxElement = screen.getByRole('checkbox')
184
+
185
+ userEvent.hover(checkboxElement)
186
+
187
+ await waitFor(() => {
188
+ expect(onMouseOver).toHaveBeenCalled()
189
+ })
190
+ })
191
+
192
+ it('calls onMouseOut', async () => {
193
+ const onMouseOut = jest.fn()
194
+ renderCheckbox({ onMouseOut })
195
+ const checkboxElement = screen.getByRole('checkbox')
196
+
197
+ userEvent.hover(checkboxElement)
198
+ userEvent.unhover(checkboxElement)
199
+
200
+ await waitFor(() => {
201
+ expect(onMouseOut).toHaveBeenCalled()
202
+ })
203
+ })
204
+ })
205
+
206
+ describe('for a11y', () => {
207
+ it('`simple` variant should meet standards', async () => {
208
+ const { container } = renderCheckbox({ variant: 'simple' })
209
+ const axeCheck = await runAxeCheck(container)
210
+
211
+ expect(axeCheck).toBe(true)
212
+ })
213
+
214
+ it('`toggle` variant should meet standards', async () => {
215
+ const { container } = renderCheckbox({ variant: 'toggle' })
216
+ const axeCheck = await runAxeCheck(container)
217
+
218
+ expect(axeCheck).toBe(true)
219
+ })
220
+ })
221
+ })
@@ -0,0 +1,202 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
27
+ import userEvent from '@testing-library/user-event'
28
+ import '@testing-library/jest-dom/extend-expect'
29
+
30
+ import { runAxeCheck } from '@instructure/ui-axe-check'
31
+ import { CheckboxGroup } from '../index'
32
+ import { CheckboxGroupProps } from '../props'
33
+ import { Checkbox } from '../../Checkbox'
34
+
35
+ const TEST_NAME = 'test-name'
36
+ const TEST_DESCRIPTION = 'test-description'
37
+ const TEST_ERROR_MESSAGE = 'test-error-message'
38
+ const TEST_VALUE_1 = 'test-value-1'
39
+ const TEST_VALUE_2 = 'test-value-2'
40
+ const TEST_LABEL_1 = 'test-label-1'
41
+ const TEST_LABEL_2 = 'test-label-2'
42
+
43
+ const renderCheckboxGroup = (props?: Partial<CheckboxGroupProps>) => {
44
+ const allProps: CheckboxGroupProps = {
45
+ name: TEST_NAME,
46
+ description: TEST_DESCRIPTION,
47
+ ...props
48
+ }
49
+
50
+ return render(
51
+ <CheckboxGroup {...allProps}>
52
+ <Checkbox label={TEST_LABEL_1} value={TEST_VALUE_1} />
53
+ <Checkbox label={TEST_LABEL_2} value={TEST_VALUE_2} />
54
+ </CheckboxGroup>
55
+ )
56
+ }
57
+
58
+ describe('<CheckboxGroup />', () => {
59
+ it('adds the name props to all Checkbox types', () => {
60
+ renderCheckboxGroup({ name: TEST_NAME })
61
+ const checkboxes = screen.getAllByRole('checkbox')
62
+
63
+ expect(checkboxes.length).toBe(2)
64
+ expect(checkboxes[0]).toHaveAttribute('name', TEST_NAME)
65
+ expect(checkboxes[1]).toHaveAttribute('name', TEST_NAME)
66
+ })
67
+
68
+ it('links the messages to the fieldset via aria-describedby', () => {
69
+ const { container } = renderCheckboxGroup({
70
+ messages: [{ text: TEST_ERROR_MESSAGE, type: 'error' }]
71
+ })
72
+ const fieldset = screen.getByRole('group')
73
+ const ariaDesc = fieldset.getAttribute('aria-describedby')
74
+ const messageById = container.querySelector(`[id="${ariaDesc}"]`)
75
+
76
+ expect(messageById).toBeInTheDocument()
77
+ expect(messageById).toHaveTextContent(TEST_ERROR_MESSAGE)
78
+ })
79
+
80
+ it('displays description message inside the legend', () => {
81
+ const { container } = renderCheckboxGroup({ description: TEST_DESCRIPTION })
82
+ const legend = container.querySelector('legend')
83
+
84
+ expect(legend).toBeInTheDocument()
85
+ expect(legend).toHaveTextContent(TEST_DESCRIPTION)
86
+ })
87
+
88
+ it('does not call the onChange prop when disabled', async () => {
89
+ const onChange = jest.fn()
90
+ renderCheckboxGroup({ onChange, disabled: true })
91
+ const checkboxElement = screen.getAllByRole('checkbox')[0]
92
+
93
+ fireEvent.click(checkboxElement)
94
+
95
+ await waitFor(() => {
96
+ expect(onChange).not.toHaveBeenCalled()
97
+ expect(checkboxElement).toBeDisabled()
98
+ })
99
+ })
100
+
101
+ it('does not call the onChange prop when readOnly', async () => {
102
+ const onChange = jest.fn()
103
+ renderCheckboxGroup({ onChange, readOnly: true })
104
+ const checkboxElement = screen.getAllByRole('checkbox')[0]
105
+
106
+ fireEvent.click(checkboxElement)
107
+
108
+ await waitFor(() => {
109
+ expect(onChange).not.toHaveBeenCalled()
110
+ expect(checkboxElement).toBeDisabled()
111
+ })
112
+ })
113
+
114
+ it('should not update the value when the value prop is set', async () => {
115
+ const onChange = jest.fn()
116
+ renderCheckboxGroup({ onChange, value: ['tester'] })
117
+ const checkboxes = screen.getAllByRole('checkbox')
118
+
119
+ expect(checkboxes[0]).not.toBeChecked()
120
+ expect(checkboxes[1]).not.toBeChecked()
121
+
122
+ userEvent.click(checkboxes[0])
123
+
124
+ await waitFor(() => {
125
+ expect(onChange).not.toHaveBeenCalled()
126
+ expect(checkboxes[0]).not.toBeChecked()
127
+ expect(checkboxes[1]).not.toBeChecked()
128
+ })
129
+ })
130
+
131
+ it('should add the checkbox value to the value list when it is checked', async () => {
132
+ const onChange = jest.fn()
133
+ renderCheckboxGroup({ onChange })
134
+ const checkboxes = screen.getAllByRole('checkbox')
135
+
136
+ expect(checkboxes[0]).not.toBeChecked()
137
+ expect(checkboxes[1]).not.toBeChecked()
138
+
139
+ userEvent.click(checkboxes[0])
140
+ userEvent.click(checkboxes[1])
141
+
142
+ await waitFor(() => {
143
+ expect(checkboxes[0]).toBeChecked()
144
+ expect(checkboxes[1]).toBeChecked()
145
+ expect(onChange).toHaveBeenCalledWith([TEST_VALUE_1, TEST_VALUE_2])
146
+ })
147
+ })
148
+
149
+ it('should check the checkboxes based on the defaultValue prop', () => {
150
+ const defaultValue = [TEST_VALUE_2]
151
+ renderCheckboxGroup({ defaultValue })
152
+ const checkboxes = screen.getAllByRole('checkbox')
153
+
154
+ expect(checkboxes[0]).not.toBeChecked()
155
+ expect(checkboxes[1]).toBeChecked()
156
+ })
157
+
158
+ it('should remove the checkbox value from the value list when it is unchecked', async () => {
159
+ const onChange = jest.fn()
160
+ const defaultValue = [TEST_VALUE_1, TEST_VALUE_2]
161
+ renderCheckboxGroup({ onChange, defaultValue })
162
+ const checkboxes = screen.getAllByRole('checkbox')
163
+
164
+ expect(checkboxes[0]).toBeChecked()
165
+ expect(checkboxes[1]).toBeChecked()
166
+
167
+ userEvent.click(checkboxes[0])
168
+
169
+ await waitFor(() => {
170
+ expect(checkboxes[0]).not.toBeChecked()
171
+ expect(checkboxes[1]).toBeChecked()
172
+ expect(onChange).toHaveBeenCalledWith([TEST_VALUE_2])
173
+ })
174
+ })
175
+
176
+ it('passes the array of selected values to onChange handler', async () => {
177
+ const onChange = jest.fn()
178
+ const defaultValue = [TEST_VALUE_2]
179
+ renderCheckboxGroup({ onChange, defaultValue })
180
+ const checkboxes = screen.getAllByRole('checkbox')
181
+
182
+ expect(checkboxes[0]).not.toBeChecked()
183
+ expect(checkboxes[1]).toBeChecked()
184
+
185
+ userEvent.click(checkboxes[0])
186
+
187
+ await waitFor(() => {
188
+ expect(checkboxes[0]).toBeChecked()
189
+ expect(checkboxes[1]).toBeChecked()
190
+ expect(onChange).toHaveBeenCalledWith([TEST_VALUE_2, TEST_VALUE_1])
191
+ })
192
+ })
193
+
194
+ describe('for a11y', () => {
195
+ it('should meet standards', async () => {
196
+ const { container } = renderCheckboxGroup()
197
+ const axeCheck = await runAxeCheck(container)
198
+
199
+ expect(axeCheck).toBe(true)
200
+ })
201
+ })
202
+ })
@@ -22,8 +22,8 @@
22
22
  { "path": "../uid/tsconfig.build.json" },
23
23
  { "path": "../ui-babel-preset/tsconfig.build.json" },
24
24
  { "path": "../ui-color-utils/tsconfig.build.json" },
25
- { "path": "../ui-test-locator/tsconfig.build.json" },
26
25
  { "path": "../ui-test-utils/tsconfig.build.json" },
27
- { "path": "../ui-themes/tsconfig.build.json" }
26
+ { "path": "../ui-themes/tsconfig.build.json" },
27
+ { "path": "../ui-axe-check/tsconfig.build.json" }
28
28
  ]
29
29
  }