|
| 1 | +/* |
| 2 | + * Copyright 2024 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {act, pointerMap, render} from '@react-spectrum/test-utils-internal'; |
| 14 | +import {Checkbox, CheckboxGroup, Form} from '../src'; |
| 15 | +import React from 'react'; |
| 16 | +import userEvent from '@testing-library/user-event'; |
| 17 | + |
| 18 | +describe('CheckboxGroup', () => { |
| 19 | + let user; |
| 20 | + beforeAll(() => { |
| 21 | + user = userEvent.setup({delay: null, pointerMap}); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should not require all checkboxes to be checked when Form has isRequired', async () => { |
| 25 | + let {getByRole, getAllByRole, getByTestId} = render( |
| 26 | + <Form data-testid="form" isRequired> |
| 27 | + <CheckboxGroup label="Favorite sports"> |
| 28 | + <Checkbox value="soccer">Soccer</Checkbox> |
| 29 | + <Checkbox value="baseball">Baseball</Checkbox> |
| 30 | + <Checkbox value="basketball">Basketball</Checkbox> |
| 31 | + </CheckboxGroup> |
| 32 | + </Form> |
| 33 | + ); |
| 34 | + |
| 35 | + |
| 36 | + let group = getByRole('group'); |
| 37 | + let checkbox = getAllByRole('checkbox')[0]; |
| 38 | + |
| 39 | + await user.click(checkbox); |
| 40 | + act(() => {getByTestId('form').checkValidity();}); |
| 41 | + expect(group).not.toHaveAttribute('aria-describedby'); |
| 42 | + expect(group).not.toHaveAttribute('data-invalid'); |
| 43 | + |
| 44 | + await user.click(checkbox); |
| 45 | + act(() => {getByTestId('form').checkValidity();}); |
| 46 | + expect(group).toHaveAttribute('data-invalid'); |
| 47 | + expect(group).toHaveAttribute('aria-describedby'); |
| 48 | + let errorMsg = document.getElementById(group.getAttribute('aria-describedby')); |
| 49 | + expect(errorMsg).toHaveTextContent('Constraints not satisfied'); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | + |
0 commit comments