|
| 1 | +import * as stylex from '@stylexjs/stylex'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | +import React from 'react'; |
| 5 | +import { describe, expect, it, vi } from 'vitest'; |
| 6 | + |
| 7 | +import { Checkbox } from './checkbox'; |
| 8 | + |
| 9 | +const atoms = stylex.create({ |
| 10 | + spaced: { marginTop: '8px' }, |
| 11 | +}); |
| 12 | + |
| 13 | +describe('Mosaic Checkbox', () => { |
| 14 | + it('renders a native checkbox wrapped in the slot element', () => { |
| 15 | + render(<Checkbox aria-label='Select row' />); |
| 16 | + const input = screen.getByRole('checkbox', { name: 'Select row' }); |
| 17 | + expect(input).toHaveClass('cl-checkbox-input'); |
| 18 | + expect(input.parentElement).toHaveClass('cl-checkbox'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('toggles and reports changes', async () => { |
| 22 | + const onChange = vi.fn(); |
| 23 | + render( |
| 24 | + <Checkbox |
| 25 | + aria-label='Select row' |
| 26 | + onChange={onChange} |
| 27 | + />, |
| 28 | + ); |
| 29 | + const input = screen.getByRole('checkbox'); |
| 30 | + await userEvent.click(input); |
| 31 | + expect(input).toBeChecked(); |
| 32 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it('supports a controlled checked state', () => { |
| 36 | + render( |
| 37 | + <Checkbox |
| 38 | + aria-label='Select row' |
| 39 | + checked |
| 40 | + onChange={() => {}} |
| 41 | + />, |
| 42 | + ); |
| 43 | + expect(screen.getByRole('checkbox')).toBeChecked(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('applies the indeterminate state through the DOM property', () => { |
| 47 | + const { rerender } = render( |
| 48 | + <Checkbox |
| 49 | + aria-label='Select all' |
| 50 | + indeterminate |
| 51 | + />, |
| 52 | + ); |
| 53 | + const input = screen.getByRole<HTMLInputElement>('checkbox'); |
| 54 | + expect(input.indeterminate).toBe(true); |
| 55 | + |
| 56 | + rerender(<Checkbox aria-label='Select all' />); |
| 57 | + expect(input.indeterminate).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('reflects the size axis on the slot element', () => { |
| 61 | + render(<Checkbox aria-label='Select row' />); |
| 62 | + expect(screen.getByRole('checkbox').parentElement).toHaveAttribute('data-size', 'md'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('disables the input', () => { |
| 66 | + render( |
| 67 | + <Checkbox |
| 68 | + aria-label='Select row' |
| 69 | + disabled |
| 70 | + />, |
| 71 | + ); |
| 72 | + expect(screen.getByRole('checkbox')).toBeDisabled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('merges xstyle atoms onto the slot element', () => { |
| 76 | + render( |
| 77 | + <Checkbox |
| 78 | + aria-label='Select row' |
| 79 | + xstyle={atoms.spaced} |
| 80 | + />, |
| 81 | + ); |
| 82 | + expect(screen.getByRole('checkbox').parentElement).toHaveClass( |
| 83 | + 'cl-checkbox', |
| 84 | + stylex.props(atoms.spaced).className ?? '', |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('forwards the ref and arbitrary input props to the input', () => { |
| 89 | + const ref = React.createRef<HTMLInputElement>(); |
| 90 | + render( |
| 91 | + <Checkbox |
| 92 | + ref={ref} |
| 93 | + aria-label='Select row' |
| 94 | + name='rows' |
| 95 | + value='1' |
| 96 | + />, |
| 97 | + ); |
| 98 | + const input = screen.getByRole('checkbox'); |
| 99 | + expect(ref.current).toBe(input); |
| 100 | + expect(input).toHaveAttribute('name', 'rows'); |
| 101 | + expect(input).toHaveAttribute('value', '1'); |
| 102 | + }); |
| 103 | +}); |
0 commit comments