|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file presentation.test.ts |
| 5 | + * @input Uses vitest, React Testing Library, react-dom/server and a |
| 6 | + * query-aware matchMedia and CSS.supports stub |
| 7 | + * @output A caller's explicit presentation wins: `'popover'` pins the fine |
| 8 | + * surface and input modes on an iPhone; an omitted value follows the |
| 9 | + * component's default (`'popover'` or `'touch'`), distinguishable from an |
| 10 | + * explicit `'popover'`; `'adaptive'` and `'bottom-sheet'` keep their sheet |
| 11 | + * meaning with touch modes under a finger; the idiom and motion follow the |
| 12 | + * touch modes only; the surface equals useAdaptivePresentation's for every |
| 13 | + * value on every device; live pointer changes; the server default and |
| 14 | + * hints; the one-moment read |
| 15 | + * @position Unit tests of packages/core/src/utils/presentation.ts |
| 16 | + */ |
| 17 | + |
| 18 | +import {act, renderHook} from '@testing-library/react'; |
| 19 | +import {createElement} from 'react'; |
| 20 | +import {renderToString} from 'react-dom/server'; |
| 21 | +import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; |
| 22 | +import { |
| 23 | + useAdaptivePresentation, |
| 24 | + type AdaptivePresentation, |
| 25 | +} from '../hooks/useAdaptivePresentation'; |
| 26 | +import { |
| 27 | + readPresentation, |
| 28 | + usePresentation, |
| 29 | + type PresentationDefault, |
| 30 | +} from './presentation'; |
| 31 | +import {IOS_SUPPORTS_QUERY} from './touchPresentation'; |
| 32 | + |
| 33 | +const device = {coarse: false, width: 1280, height: 800, iosWebKit: false}; |
| 34 | +const listeners = new Set<() => void>(); |
| 35 | + |
| 36 | +/** Answers a media query list: `,` is or, `and` is and. */ |
| 37 | +function answer(query: string): boolean { |
| 38 | + return query.split(', ').some(alternative => |
| 39 | + alternative.split(' and ').every(part => { |
| 40 | + if (part === '(pointer: coarse)') { |
| 41 | + return device.coarse; |
| 42 | + } |
| 43 | + const range = /^\((width|height) < (\d+)px\)$/.exec(part); |
| 44 | + if (range != null) { |
| 45 | + return device[range[1] as 'width' | 'height'] < Number(range[2]); |
| 46 | + } |
| 47 | + throw new Error(`unexpected query ${query}`); |
| 48 | + }), |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +function setDevice(next: Partial<typeof device>): void { |
| 53 | + Object.assign(device, next); |
| 54 | + for (const listener of [...listeners]) { |
| 55 | + listener(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +const IPHONE = {coarse: true, width: 393, height: 852, iosWebKit: true}; |
| 60 | +const IPAD = {coarse: true, width: 820, height: 1180, iosWebKit: true}; |
| 61 | +const ANDROID = {coarse: true, width: 412, height: 915, iosWebKit: false}; |
| 62 | +const DESKTOP = {coarse: false, width: 1280, height: 800, iosWebKit: false}; |
| 63 | +const MAC_SAFARI = {coarse: false, width: 1440, height: 900, iosWebKit: false}; |
| 64 | + |
| 65 | +beforeEach(() => { |
| 66 | + setDevice(DESKTOP); |
| 67 | + vi.stubGlobal('CSS', { |
| 68 | + supports: (condition: string) => |
| 69 | + condition === IOS_SUPPORTS_QUERY && device.iosWebKit, |
| 70 | + }); |
| 71 | + vi.stubGlobal( |
| 72 | + 'matchMedia', |
| 73 | + vi.fn((query: string) => ({ |
| 74 | + get matches() { |
| 75 | + return answer(query); |
| 76 | + }, |
| 77 | + media: query, |
| 78 | + addEventListener: (_type: string, listener: () => void) => |
| 79 | + listeners.add(listener), |
| 80 | + removeEventListener: (_type: string, listener: () => void) => |
| 81 | + listeners.delete(listener), |
| 82 | + })), |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +afterEach(() => { |
| 87 | + listeners.clear(); |
| 88 | + vi.unstubAllGlobals(); |
| 89 | +}); |
| 90 | + |
| 91 | +const resolve = ( |
| 92 | + presentation: AdaptivePresentation | undefined, |
| 93 | + whenOmitted?: PresentationDefault, |
| 94 | +) => |
| 95 | + renderHook(() => usePresentation(presentation, {whenOmitted})).result.current; |
| 96 | + |
| 97 | +describe('presentation: a caller choice wins over touch defaults', () => { |
| 98 | + it('pins the fine surface and input modes with an explicit popover, even on an iPhone', () => { |
| 99 | + setDevice(IPHONE); |
| 100 | + for (const whenOmitted of ['popover', 'touch'] as const) { |
| 101 | + expect(resolve('popover', whenOmitted)).toEqual({ |
| 102 | + surface: 'popover', |
| 103 | + touchModes: false, |
| 104 | + idiom: 'default', |
| 105 | + motion: 'theme', |
| 106 | + }); |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + it("tells an omitted value from an explicit popover: the component's touch default applies only when omitted", () => { |
| 111 | + setDevice(IPHONE); |
| 112 | + expect(resolve(undefined, 'touch')).toEqual({ |
| 113 | + surface: 'popover', |
| 114 | + touchModes: true, |
| 115 | + idiom: 'ios', |
| 116 | + motion: 'uikit', |
| 117 | + }); |
| 118 | + // Today's components: omitted is popover. |
| 119 | + expect(resolve(undefined)).toEqual(resolve('popover')); |
| 120 | + expect(resolve(undefined, 'popover')).toEqual(resolve('popover')); |
| 121 | + }); |
| 122 | + |
| 123 | + it('turns touch modes on under a finger only, and the UIKit idiom on iOS only', () => { |
| 124 | + setDevice(ANDROID); |
| 125 | + expect(resolve(undefined, 'touch')).toEqual({ |
| 126 | + surface: 'popover', |
| 127 | + touchModes: true, |
| 128 | + idiom: 'default', |
| 129 | + motion: 'theme', |
| 130 | + }); |
| 131 | + for (const desk of [DESKTOP, MAC_SAFARI]) { |
| 132 | + setDevice(desk); |
| 133 | + expect(resolve(undefined, 'touch')).toMatchObject({ |
| 134 | + touchModes: false, |
| 135 | + idiom: 'default', |
| 136 | + }); |
| 137 | + } |
| 138 | + }); |
| 139 | + |
| 140 | + it('keeps the sheet meaning of adaptive and bottom-sheet, with touch modes under a finger', () => { |
| 141 | + setDevice(IPHONE); |
| 142 | + expect(resolve('adaptive')).toEqual({ |
| 143 | + surface: 'bottom-sheet', |
| 144 | + touchModes: true, |
| 145 | + idiom: 'ios', |
| 146 | + motion: 'uikit', |
| 147 | + }); |
| 148 | + setDevice(IPAD); |
| 149 | + expect(resolve('adaptive')).toMatchObject({ |
| 150 | + surface: 'popover', |
| 151 | + touchModes: true, |
| 152 | + }); |
| 153 | + setDevice(DESKTOP); |
| 154 | + expect(resolve('adaptive')).toMatchObject({ |
| 155 | + surface: 'popover', |
| 156 | + touchModes: false, |
| 157 | + }); |
| 158 | + expect(resolve('bottom-sheet')).toMatchObject({ |
| 159 | + surface: 'bottom-sheet', |
| 160 | + touchModes: false, |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + it("renders useAdaptivePresentation's surface for every value on every device", () => { |
| 165 | + for (const next of [IPHONE, IPAD, ANDROID, DESKTOP, MAC_SAFARI]) { |
| 166 | + setDevice(next); |
| 167 | + for (const value of ['popover', 'bottom-sheet', 'adaptive'] as const) { |
| 168 | + const {result} = renderHook(() => ({ |
| 169 | + surface: usePresentation(value).surface, |
| 170 | + adaptive: useAdaptivePresentation(value), |
| 171 | + })); |
| 172 | + expect(result.current.surface).toBe(result.current.adaptive); |
| 173 | + } |
| 174 | + } |
| 175 | + }); |
| 176 | + |
| 177 | + it('follows the pointer when it changes (an iPad docking a trackpad)', () => { |
| 178 | + setDevice(IPAD); |
| 179 | + const {result} = renderHook(() => |
| 180 | + usePresentation(undefined, {whenOmitted: 'touch'}), |
| 181 | + ); |
| 182 | + expect(result.current.touchModes).toBe(true); |
| 183 | + act(() => setDevice({coarse: false})); |
| 184 | + expect(result.current).toMatchObject({touchModes: false, idiom: 'default'}); |
| 185 | + act(() => setDevice({coarse: true})); |
| 186 | + expect(result.current).toMatchObject({touchModes: true, idiom: 'ios'}); |
| 187 | + }); |
| 188 | + |
| 189 | + it('renders the server default until hydrated, and honours a server hint', () => { |
| 190 | + function Probe({ |
| 191 | + presentation, |
| 192 | + hint, |
| 193 | + }: { |
| 194 | + presentation?: AdaptivePresentation; |
| 195 | + hint?: boolean; |
| 196 | + }) { |
| 197 | + const resolved = usePresentation(presentation, { |
| 198 | + whenOmitted: 'touch', |
| 199 | + serverDefault: hint === true ? {idiom: 'ios', isCompact: true} : {}, |
| 200 | + }); |
| 201 | + return `${resolved.surface}/${String(resolved.touchModes)}/${resolved.idiom}`; |
| 202 | + } |
| 203 | + expect(renderToString(createElement(Probe))).toContain( |
| 204 | + 'popover/false/default', |
| 205 | + ); |
| 206 | + expect(renderToString(createElement(Probe, {hint: true}))).toContain( |
| 207 | + 'popover/true/ios', |
| 208 | + ); |
| 209 | + expect( |
| 210 | + renderToString( |
| 211 | + createElement(Probe, {presentation: 'adaptive', hint: true}), |
| 212 | + ), |
| 213 | + ).toContain('bottom-sheet/true/ios'); |
| 214 | + expect( |
| 215 | + renderToString( |
| 216 | + createElement(Probe, {presentation: 'popover', hint: true}), |
| 217 | + ), |
| 218 | + ).toContain('popover/false/default'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('answers at one moment as the hook does, and as the server would with no window', () => { |
| 222 | + setDevice(IPHONE); |
| 223 | + expect(readPresentation(undefined, {whenOmitted: 'touch'})).toEqual( |
| 224 | + resolve(undefined, 'touch'), |
| 225 | + ); |
| 226 | + expect(readPresentation('popover', {whenOmitted: 'touch'})).toEqual( |
| 227 | + resolve('popover', 'touch'), |
| 228 | + ); |
| 229 | + expect(readPresentation('adaptive')).toEqual(resolve('adaptive')); |
| 230 | + expect( |
| 231 | + readPresentation(undefined, {whenOmitted: 'touch', view: null}), |
| 232 | + ).toEqual({ |
| 233 | + surface: 'popover', |
| 234 | + touchModes: false, |
| 235 | + idiom: 'default', |
| 236 | + motion: 'theme', |
| 237 | + }); |
| 238 | + }); |
| 239 | +}); |
0 commit comments