|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import CheckboxCards from '../CheckboxCards'; |
| 4 | + |
| 5 | +describe('CheckboxCards', () => { |
| 6 | + it('renders items and content, and toggles checked state (uncontrolled)', () => { |
| 7 | + render( |
| 8 | + <CheckboxCards.Root name="fruits" defaultValue={['apple']}> |
| 9 | + <CheckboxCards.Item value="apple"> |
| 10 | + Apple |
| 11 | + <CheckboxCards.Content> |
| 12 | + <CheckboxCards.Indicator /> |
| 13 | + </CheckboxCards.Content> |
| 14 | + </CheckboxCards.Item> |
| 15 | + <CheckboxCards.Item value="banana"> |
| 16 | + Banana |
| 17 | + <CheckboxCards.Content> |
| 18 | + <CheckboxCards.Indicator /> |
| 19 | + </CheckboxCards.Content> |
| 20 | + </CheckboxCards.Item> |
| 21 | + </CheckboxCards.Root> |
| 22 | + ); |
| 23 | + |
| 24 | + // Apple is checked, Banana is not |
| 25 | + const appleCheckbox = screen.getByText('Apple').parentElement?.querySelector('[role="checkbox"]'); |
| 26 | + const bananaCheckbox = screen.getByText('Banana').parentElement?.querySelector('[role="checkbox"]'); |
| 27 | + expect(appleCheckbox).toHaveAttribute('aria-checked', 'true'); |
| 28 | + expect(bananaCheckbox).toHaveAttribute('aria-checked', 'false'); |
| 29 | + |
| 30 | + // Click Banana to check it |
| 31 | + if (bananaCheckbox) fireEvent.click(bananaCheckbox); |
| 32 | + expect(bananaCheckbox).toHaveAttribute('aria-checked', 'true'); |
| 33 | + |
| 34 | + // Click Apple to uncheck it |
| 35 | + if (appleCheckbox) fireEvent.click(appleCheckbox); |
| 36 | + expect(appleCheckbox).toHaveAttribute('aria-checked', 'false'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('supports controlled usage', () => { |
| 40 | + const handleChange = jest.fn(); |
| 41 | + const value = ['banana']; |
| 42 | + render( |
| 43 | + <CheckboxCards.Root name="fruits" value={value} onValueChange={handleChange}> |
| 44 | + <CheckboxCards.Item value="apple"> |
| 45 | + Apple |
| 46 | + <CheckboxCards.Content> |
| 47 | + <CheckboxCards.Indicator /> |
| 48 | + </CheckboxCards.Content> |
| 49 | + </CheckboxCards.Item> |
| 50 | + <CheckboxCards.Item value="banana"> |
| 51 | + Banana |
| 52 | + <CheckboxCards.Content> |
| 53 | + <CheckboxCards.Indicator /> |
| 54 | + </CheckboxCards.Content> |
| 55 | + </CheckboxCards.Item> |
| 56 | + </CheckboxCards.Root> |
| 57 | + ); |
| 58 | + // Only banana is checked |
| 59 | + const checkboxes = screen.getAllByRole('checkbox'); |
| 60 | + expect(checkboxes[0]).toHaveAttribute('aria-checked', 'false'); |
| 61 | + expect(checkboxes[1]).toHaveAttribute('aria-checked', 'true'); |
| 62 | + // Click apple |
| 63 | + fireEvent.click(checkboxes[0]); |
| 64 | + expect(handleChange).toHaveBeenCalledWith(['banana', 'apple']); |
| 65 | + // Click banana |
| 66 | + fireEvent.click(checkboxes[1]); |
| 67 | + expect(handleChange).toHaveBeenCalledWith([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('respects disabled and required props', () => { |
| 71 | + render( |
| 72 | + <CheckboxCards.Root name="fruits" required disabled> |
| 73 | + <CheckboxCards.Item value="apple"> |
| 74 | + Apple |
| 75 | + <CheckboxCards.Content> |
| 76 | + <CheckboxCards.Indicator /> |
| 77 | + </CheckboxCards.Content> |
| 78 | + </CheckboxCards.Item> |
| 79 | + </CheckboxCards.Root> |
| 80 | + ); |
| 81 | + const checkbox = screen.getByRole('checkbox'); |
| 82 | + expect(checkbox).toBeDisabled(); |
| 83 | + expect(checkbox).toHaveAttribute('aria-required', 'true'); |
| 84 | + // The hidden input should also be disabled and required |
| 85 | + const input = checkbox.parentElement?.parentElement?.querySelector('input[type="checkbox"]'); |
| 86 | + expect(input).toBeDisabled(); |
| 87 | + expect(input).toBeRequired(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('works in a form and submits checked values', () => { |
| 91 | + let entries: [string, FormDataEntryValue][] = []; |
| 92 | + const handleSubmit = jest.fn((e: React.FormEvent<HTMLFormElement>) => { |
| 93 | + e.preventDefault(); |
| 94 | + const formData = new FormData(e.target as HTMLFormElement); |
| 95 | + // @ts-ignore |
| 96 | + entries = Array.from(formData.entries()); |
| 97 | + }); |
| 98 | + render( |
| 99 | + <form onSubmit={handleSubmit}> |
| 100 | + <CheckboxCards.Root name="fruits" defaultValue={['apple']}> |
| 101 | + <CheckboxCards.Item value="apple"> |
| 102 | + Apple |
| 103 | + <CheckboxCards.Content> |
| 104 | + <CheckboxCards.Indicator /> |
| 105 | + </CheckboxCards.Content> |
| 106 | + </CheckboxCards.Item> |
| 107 | + <CheckboxCards.Item value="banana"> |
| 108 | + Banana |
| 109 | + <CheckboxCards.Content> |
| 110 | + <CheckboxCards.Indicator /> |
| 111 | + </CheckboxCards.Content> |
| 112 | + </CheckboxCards.Item> |
| 113 | + </CheckboxCards.Root> |
| 114 | + <button type="submit">Submit</button> |
| 115 | + </form> |
| 116 | + ); |
| 117 | + const bananaCheckbox = screen.getByText('Banana').parentElement?.querySelector('[role="checkbox"]'); |
| 118 | + if (bananaCheckbox) fireEvent.click(bananaCheckbox); |
| 119 | + fireEvent.click(screen.getByText('Submit')); |
| 120 | + // The form should submit both apple and banana |
| 121 | + expect(handleSubmit).toHaveBeenCalled(); |
| 122 | + const values = entries.filter(([key]) => key === 'fruits').map(([, value]) => value); |
| 123 | + expect(values).toEqual(expect.arrayContaining(['apple', 'banana'])); |
| 124 | + }); |
| 125 | + |
| 126 | + it('CheckboxCards itself renders null and warns', () => { |
| 127 | + const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); |
| 128 | + const { container } = render(<CheckboxCards />); |
| 129 | + expect(container).toBeEmptyDOMElement(); |
| 130 | + expect(warn).toHaveBeenCalledWith( |
| 131 | + 'Direct usage of CheckboxCards is not supported. Please use CheckboxCards.Root, CheckboxCards.Item instead.' |
| 132 | + ); |
| 133 | + warn.mockRestore(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments