|
| 1 | +import { Box, Checkbox, ChakraProvider } from '@chakra-ui/react'; |
| 2 | +import { applyThemeToRoot } from '@theme/applyTheme'; |
| 3 | +import { compositeColors, getContrastRatio } from '@theme/contrastRatio.testing'; |
| 4 | +import { system } from '@theme/system'; |
| 5 | +import { THEMES } from '@theme/themes'; |
| 6 | +import { act } from 'react'; |
| 7 | +import { createRoot, type Root } from 'react-dom/client'; |
| 8 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 9 | +import { userEvent } from 'vitest/browser'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Hovering anywhere in a checkbox's target (label, control, or the invisible outset around the control) must read as |
| 13 | + * a stronger boundary or a shifted fill in every theme, while a disabled checkbox stays as it rests. |
| 14 | + */ |
| 15 | + |
| 16 | +let host: HTMLDivElement | null = null; |
| 17 | +let root: Root | null = null; |
| 18 | +(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 19 | + |
| 20 | +beforeEach(() => { |
| 21 | + // Read settled colors: reduced motion shortens the hover transition to a millisecond. |
| 22 | + document.documentElement.dataset.reduceMotion = 'true'; |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(async () => { |
| 26 | + await userEvent.unhover(document.body); |
| 27 | + await act(() => root?.unmount()); |
| 28 | + host?.remove(); |
| 29 | + delete document.documentElement.dataset.highContrast; |
| 30 | + delete document.documentElement.dataset.reduceMotion; |
| 31 | + applyThemeToRoot('classic'); |
| 32 | + host = null; |
| 33 | + root = null; |
| 34 | +}); |
| 35 | + |
| 36 | +const SAMPLES = [ |
| 37 | + { id: 'unchecked', checked: false, disabled: false, invalid: false }, |
| 38 | + { id: 'checked', checked: true, disabled: false, invalid: false }, |
| 39 | + { id: 'disabled', checked: false, disabled: true, invalid: false }, |
| 40 | + { id: 'invalid', checked: false, disabled: false, invalid: true }, |
| 41 | + { id: 'invalid-checked', checked: true, disabled: false, invalid: true }, |
| 42 | + { id: 'invalid-indeterminate', checked: 'indeterminate', disabled: false, invalid: true }, |
| 43 | +] as const; |
| 44 | + |
| 45 | +const renderCheckboxes = async () => { |
| 46 | + host = document.createElement('div'); |
| 47 | + document.body.append(host); |
| 48 | + root = createRoot(host); |
| 49 | + await act(() => { |
| 50 | + root?.render( |
| 51 | + <ChakraProvider value={system}> |
| 52 | + <Box bg="bg.panel" display="flex" flexDirection="column" gap="4" p="4"> |
| 53 | + {SAMPLES.map(({ checked, disabled, id, invalid }) => ( |
| 54 | + <Checkbox.Root |
| 55 | + key={id} |
| 56 | + checked={checked} |
| 57 | + colorPalette="accent" |
| 58 | + data-testid={id} |
| 59 | + disabled={disabled} |
| 60 | + invalid={invalid} |
| 61 | + size="sm" |
| 62 | + > |
| 63 | + <Checkbox.HiddenInput /> |
| 64 | + <Checkbox.Control /> |
| 65 | + <Checkbox.Label>{id}</Checkbox.Label> |
| 66 | + </Checkbox.Root> |
| 67 | + ))} |
| 68 | + </Box> |
| 69 | + </ChakraProvider> |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + const panel = getComputedStyle(host.firstElementChild!).backgroundColor; |
| 74 | + const part = (id: string, name: string) => |
| 75 | + host!.querySelector<HTMLElement>(`[data-testid="${id}"] [data-part="${name}"]`)!; |
| 76 | + const settle = () => |
| 77 | + new Promise<void>((resolve) => { |
| 78 | + requestAnimationFrame(() => requestAnimationFrame(() => resolve())); |
| 79 | + }); |
| 80 | + // A point in the outset: left of the control's visible box, level with its middle. |
| 81 | + const hoverOutset = async (id: string) => { |
| 82 | + const label = host!.querySelector<HTMLElement>(`[data-testid="${id}"]`)!; |
| 83 | + const labelRect = label.getBoundingClientRect(); |
| 84 | + const control = part(id, 'control').getBoundingClientRect(); |
| 85 | + |
| 86 | + await userEvent.hover(label, { |
| 87 | + position: { x: control.left - labelRect.left - 4, y: control.top - labelRect.top + control.height / 2 }, |
| 88 | + }); |
| 89 | + await settle(); |
| 90 | + }; |
| 91 | + const hoverLabel = async (id: string) => { |
| 92 | + await userEvent.hover(part(id, 'label')); |
| 93 | + await settle(); |
| 94 | + }; |
| 95 | + const read = (id: string) => { |
| 96 | + const style = getComputedStyle(part(id, 'control')); |
| 97 | + const fill = compositeColors(style.backgroundColor, panel); |
| 98 | + |
| 99 | + return { |
| 100 | + border: getContrastRatio(style.borderTopColor, panel, 1), |
| 101 | + fill, |
| 102 | + mark: getContrastRatio(style.color, fill, 1), |
| 103 | + }; |
| 104 | + }; |
| 105 | + |
| 106 | + return { hoverLabel, hoverOutset, read }; |
| 107 | +}; |
| 108 | + |
| 109 | +describe('checkbox hover', () => { |
| 110 | + for (const theme of THEMES) { |
| 111 | + for (const highContrast of [false, true]) { |
| 112 | + it(`strengthens the boundary and shifts the fill on ${theme.id}${highContrast ? ' (high contrast)' : ''}`, async () => { |
| 113 | + applyThemeToRoot(theme.id); |
| 114 | + if (highContrast) { |
| 115 | + document.documentElement.dataset.highContrast = 'true'; |
| 116 | + } |
| 117 | + const { hoverOutset, read } = await renderCheckboxes(); |
| 118 | + const unchecked = read('unchecked'); |
| 119 | + const checked = read('checked'); |
| 120 | + |
| 121 | + await hoverOutset('unchecked'); |
| 122 | + expect(read('unchecked').border).toBeGreaterThan(unchecked.border * 1.1); |
| 123 | + |
| 124 | + await hoverOutset('checked'); |
| 125 | + const hoveredChecked = read('checked'); |
| 126 | + |
| 127 | + expect(hoveredChecked.fill).not.toBe(checked.fill); |
| 128 | + // The check stays a legible graphic on the hovered fill. |
| 129 | + expect(hoveredChecked.mark).toBeGreaterThanOrEqual(3); |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + it('shows the hover from the label, not on a disabled checkbox', async () => { |
| 135 | + const { hoverLabel, hoverOutset, read } = await renderCheckboxes(); |
| 136 | + const unchecked = read('unchecked'); |
| 137 | + const disabled = read('disabled'); |
| 138 | + |
| 139 | + await hoverLabel('unchecked'); |
| 140 | + expect(read('unchecked').border).toBeGreaterThan(unchecked.border); |
| 141 | + |
| 142 | + await hoverOutset('disabled'); |
| 143 | + expect(read('disabled')).toEqual(disabled); |
| 144 | + await hoverLabel('disabled'); |
| 145 | + expect(read('disabled')).toEqual(disabled); |
| 146 | + }); |
| 147 | + |
| 148 | + it.each(['invalid', 'invalid-checked', 'invalid-indeterminate'])( |
| 149 | + 'preserves the error appearance of %s when hovering the label or outset', |
| 150 | + async (id) => { |
| 151 | + const { hoverLabel, hoverOutset, read } = await renderCheckboxes(); |
| 152 | + const invalid = read(id); |
| 153 | + |
| 154 | + await hoverLabel(id); |
| 155 | + expect(read(id)).toEqual(invalid); |
| 156 | + await hoverOutset(id); |
| 157 | + expect(read(id)).toEqual(invalid); |
| 158 | + } |
| 159 | + ); |
| 160 | +}); |
0 commit comments