|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file ListEditingSelect.test.tsx |
| 5 | + * @input vitest, List with `isEditing` and `editingControl="select"`, a stubbed |
| 6 | + * pointer (a finger, iOS and Android alike, or a mouse) and the list table |
| 7 | + * (motion/uikit/list.ts) |
| 8 | + * @output Unit tests for edit mode's selection control: each row shows the |
| 9 | + * theme's checkbox indicator in its leading column, checked when the row |
| 10 | + * is selected, decorative (the row keeps its state and its one tab stop); |
| 11 | + * activating a row still calls its `onClick`; under a finger the control |
| 12 | + * slides in from UIKit's travel and fades in on the table spring, and on |
| 13 | + * leaving slides out and fades, staying until it has faded while the |
| 14 | + * content moves back at once; a toggle mid-transition turns it around; |
| 15 | + * with a mouse it comes and goes at once |
| 16 | + * @position Beside List/List.tsx; the motion itself is proven in a real |
| 17 | + * engine by List/__tests__/ListEditingSelect.interaction.browser.spec.ts |
| 18 | + */ |
| 19 | + |
| 20 | +import {act, fireEvent, render, screen, waitFor} from '@testing-library/react'; |
| 21 | +import {afterEach, describe, expect, it, vi} from 'vitest'; |
| 22 | +import {list} from '../motion/uikit/list'; |
| 23 | +import {motionOf, presentation, type RowMotion} from '../utils/rowMotion'; |
| 24 | +import {List} from './List'; |
| 25 | +import {ListItem} from './ListItem'; |
| 26 | + |
| 27 | +const TRAVEL = list.metrics.editingControlTravel; |
| 28 | + |
| 29 | +function stubPointer(pointer: 'touch' | 'mouse') { |
| 30 | + vi.stubGlobal( |
| 31 | + 'matchMedia', |
| 32 | + vi.fn((query: string) => ({ |
| 33 | + matches: pointer === 'touch' && query.includes('pointer: coarse'), |
| 34 | + media: query, |
| 35 | + onchange: null, |
| 36 | + addEventListener: () => undefined, |
| 37 | + removeEventListener: () => undefined, |
| 38 | + addListener: () => undefined, |
| 39 | + removeListener: () => undefined, |
| 40 | + dispatchEvent: () => false, |
| 41 | + })), |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + vi.unstubAllGlobals(); |
| 47 | +}); |
| 48 | + |
| 49 | +const SENT_SELECTED = ['Sent']; |
| 50 | +const ignoreClick = () => undefined; |
| 51 | + |
| 52 | +function Rows({ |
| 53 | + isEditing, |
| 54 | + editingControl = 'select', |
| 55 | + selected = SENT_SELECTED, |
| 56 | + onClick = ignoreClick, |
| 57 | +}: { |
| 58 | + isEditing?: boolean; |
| 59 | + editingControl?: 'select'; |
| 60 | + selected?: string[]; |
| 61 | + onClick?: (label: string) => void; |
| 62 | +}) { |
| 63 | + return ( |
| 64 | + <List isEditing={isEditing} editingControl={editingControl}> |
| 65 | + {['Inbox', 'Sent', 'Drafts'].map(label => ( |
| 66 | + <ListItem |
| 67 | + key={label} |
| 68 | + label={label} |
| 69 | + isSelected={selected.includes(label)} |
| 70 | + onClick={() => onClick(label)} |
| 71 | + /> |
| 72 | + ))} |
| 73 | + </List> |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +const rowOf = (label: string) => |
| 78 | + screen.getByText(label).closest('li') as HTMLElement; |
| 79 | +const controlOf = (label: string) => |
| 80 | + rowOf(label).querySelector<HTMLElement>('[data-astryx-editing-control]'); |
| 81 | +const indicatorOf = (label: string) => |
| 82 | + controlOf(label)?.querySelector<HTMLElement>('.astryx-checkbox-indicator') ?? |
| 83 | + null; |
| 84 | + |
| 85 | +describe('List edit mode, selection control', () => { |
| 86 | + it('shows nothing outside edit mode', () => { |
| 87 | + stubPointer('mouse'); |
| 88 | + render(<Rows isEditing={false} />); |
| 89 | + expect(controlOf('Sent')).toBeNull(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('shows the theme’s checkbox indicator in each row, checked when the row is selected', () => { |
| 93 | + stubPointer('mouse'); |
| 94 | + expect(matchMedia('(pointer: coarse)').matches).toBe(false); |
| 95 | + render(<Rows isEditing />); |
| 96 | + for (const label of ['Inbox', 'Sent', 'Drafts']) { |
| 97 | + expect(indicatorOf(label)).not.toBeNull(); |
| 98 | + } |
| 99 | + expect(indicatorOf('Sent')?.getAttribute('data-checked')).toBe('checked'); |
| 100 | + expect(indicatorOf('Inbox')?.hasAttribute('data-checked')).toBe(false); |
| 101 | + }); |
| 102 | + |
| 103 | + it('is decorative: the row keeps its state and its one tab stop', () => { |
| 104 | + stubPointer('mouse'); |
| 105 | + render(<Rows isEditing />); |
| 106 | + const control = controlOf('Sent') as HTMLElement; |
| 107 | + expect(control.getAttribute('aria-hidden')).toBe('true'); |
| 108 | + expect(control.querySelector('input, button, [tabindex]')).toBeNull(); |
| 109 | + // Item's own selected semantics carry the state for assistive technology. |
| 110 | + expect(rowOf('Sent').getAttribute('aria-current')).toBe('true'); |
| 111 | + expect(rowOf('Inbox').hasAttribute('aria-current')).toBe(false); |
| 112 | + }); |
| 113 | + |
| 114 | + it('activating a row still calls its onClick, for the app to toggle its selection', () => { |
| 115 | + stubPointer('mouse'); |
| 116 | + const onClick = vi.fn(); |
| 117 | + render(<Rows isEditing onClick={onClick} />); |
| 118 | + fireEvent.click(screen.getByText('Drafts')); |
| 119 | + expect(onClick).toHaveBeenCalledWith('Drafts'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('with a mouse, comes and goes with edit mode at once', () => { |
| 123 | + stubPointer('mouse'); |
| 124 | + const {rerender} = render(<Rows isEditing={false} />); |
| 125 | + rerender(<Rows isEditing />); |
| 126 | + expect(controlOf('Sent')).not.toBeNull(); |
| 127 | + expect(motionOf(controlOf('Sent') as HTMLElement, 'x')).toBeUndefined(); |
| 128 | + rerender(<Rows isEditing={false} />); |
| 129 | + expect(controlOf('Sent')).toBeNull(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('under a finger, slides in from UIKit’s travel and fades in on the table spring', async () => { |
| 133 | + stubPointer('touch'); |
| 134 | + const {rerender} = render(<Rows isEditing={false} />); |
| 135 | + await act(async () => undefined); |
| 136 | + rerender(<Rows isEditing />); |
| 137 | + const control = controlOf('Sent') as HTMLElement; |
| 138 | + const slide = motionOf(control, 'x') as RowMotion; |
| 139 | + expect(slide).toBeDefined(); |
| 140 | + expect(slide.spring).toBe(list.update.tableSpring); |
| 141 | + expect(motionOf(control, 'opacity')).toBeDefined(); |
| 142 | + const start = await slide.handle.started; |
| 143 | + expect(presentation(control, 'x', start)).toBeCloseTo(-TRAVEL, 6); |
| 144 | + expect(presentation(control, 'opacity', start)).toBeCloseTo(0, 6); |
| 145 | + expect(presentation(control, 'x', start + 700)).toBeCloseTo(0, 1); |
| 146 | + expect(presentation(control, 'opacity', start + 700)).toBeCloseTo(1, 1); |
| 147 | + }); |
| 148 | + |
| 149 | + it('under a finger, leaving slides it out and fades it while the content moves back at once, then it leaves', async () => { |
| 150 | + stubPointer('touch'); |
| 151 | + const {rerender} = render(<Rows isEditing />); |
| 152 | + await act(async () => undefined); |
| 153 | + rerender(<Rows isEditing={false} />); |
| 154 | + const control = controlOf('Sent') as HTMLElement; |
| 155 | + expect(control).not.toBeNull(); |
| 156 | + // The indent left layout at once: the content springs back from it. |
| 157 | + const content = screen.getByText('Sent').closest('button') as HTMLElement; |
| 158 | + expect(motionOf(content, 'x')).toBeDefined(); |
| 159 | + const slide = motionOf(control, 'x') as RowMotion; |
| 160 | + const start = await slide.handle.started; |
| 161 | + expect(presentation(control, 'x', start)).toBeCloseTo(0, 6); |
| 162 | + expect(presentation(control, 'x', start + 700)).toBeCloseTo(-TRAVEL, 1); |
| 163 | + expect(presentation(control, 'opacity', start + 700)).toBeCloseTo(0, 1); |
| 164 | + await slide.handle.finished; |
| 165 | + await waitFor(() => expect(controlOf('Sent')).toBeNull()); |
| 166 | + }); |
| 167 | + |
| 168 | + it('a toggle mid-transition turns the control around from where it is', async () => { |
| 169 | + stubPointer('touch'); |
| 170 | + const {rerender} = render(<Rows isEditing={false} />); |
| 171 | + await act(async () => undefined); |
| 172 | + rerender(<Rows isEditing />); |
| 173 | + const control = controlOf('Sent') as HTMLElement; |
| 174 | + const slide = motionOf(control, 'x') as RowMotion; |
| 175 | + await slide.handle.started; |
| 176 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 177 | + const at = performance.now(); |
| 178 | + const before = presentation(control, 'x', at); |
| 179 | + expect(before).toBeGreaterThan(-TRAVEL); |
| 180 | + expect(before).toBeLessThan(0); |
| 181 | + const clock = vi.spyOn(performance, 'now').mockReturnValue(at); |
| 182 | + rerender(<Rows isEditing={false} />); |
| 183 | + clock.mockRestore(); |
| 184 | + // The same element, from the same place, now heading out. |
| 185 | + expect(controlOf('Sent')).toBe(control); |
| 186 | + expect(presentation(control, 'x', at)).toBeCloseTo(before, 3); |
| 187 | + expect(presentation(control, 'x', at + 900)).toBeCloseTo(-TRAVEL, 1); |
| 188 | + }); |
| 189 | +}); |
0 commit comments