1616 * List/__tests__/ListTouchHitArea.interaction.browser.spec.ts
1717 */
1818
19- import { useRef } from 'react' ;
19+ import { act , useRef , type ReactNode } from 'react' ;
2020import * as stylex from '@stylexjs/stylex' ;
2121import { afterEach , describe , expect , it , vi } from 'vitest' ;
2222import { render , screen } from '@testing-library/react' ;
@@ -49,10 +49,19 @@ vi.mock('./hitArea', async importOriginal => {
4949 } ;
5050} ) ;
5151
52+ import { ThemeContext , type ThemeContextValue } from '../theme/useTheme' ;
5253import { useTouchHitArea } from './touchHitArea' ;
5354import { touchHitAreaStyles } from './touchHitArea.stylex' ;
5455
55- function Host ( { enabled, target} : { enabled : boolean ; target ?: string } ) {
56+ function Host ( {
57+ enabled,
58+ target,
59+ fontSize,
60+ } : {
61+ enabled : boolean ;
62+ target ?: string ;
63+ fontSize ?: string ;
64+ } ) {
5665 const ref = useRef < HTMLSpanElement | null > ( null ) ;
5766 useTouchHitArea ( ref , enabled ) ;
5867 return (
@@ -64,6 +73,9 @@ function Host({enabled, target}: {enabled: boolean; target?: string}) {
6473 if ( node != null && target != null ) {
6574 node . style . setProperty ( '--size-touch-target' , target ) ;
6675 }
76+ if ( node != null && fontSize != null ) {
77+ node . style . setProperty ( 'font-size' , fontSize ) ;
78+ }
6779 ref . current = node ;
6880 } }
6981 { ...stylex . props ( touchHitAreaStyles . layer ) }
@@ -72,6 +84,32 @@ function Host({enabled, target}: {enabled: boolean; target?: string}) {
7284 ) ;
7385}
7486
87+ /** A matchMedia whose primary pointer can change, firing its listeners. */
88+ function stubPointer ( coarse : boolean ) : ( coarse : boolean ) => void {
89+ let isCoarse = coarse ;
90+ const listeners = new Set < ( ) => void > ( ) ;
91+ vi . stubGlobal ( 'matchMedia' , ( query : string ) => ( {
92+ get matches ( ) {
93+ return query === '(pointer: coarse)' && isCoarse ;
94+ } ,
95+ media : query ,
96+ addEventListener : ( _type : string , listener : ( ) => void ) =>
97+ listeners . add ( listener ) ,
98+ removeEventListener : ( _type : string , listener : ( ) => void ) =>
99+ listeners . delete ( listener ) ,
100+ } ) ) ;
101+ return next => {
102+ isCoarse = next ;
103+ for ( const listener of [ ...listeners ] ) {
104+ listener ( ) ;
105+ }
106+ } ;
107+ }
108+
109+ function themed ( value : ThemeContextValue , children : ReactNode ) : ReactNode {
110+ return < ThemeContext value = { value } > { children } </ ThemeContext > ;
111+ }
112+
75113/** The element's injected `::after` rules, split by the touch gate. */
76114function afterRules ( el : Element ) : { touch : string [ ] ; other : string [ ] } {
77115 const classes = el . className
@@ -152,6 +190,39 @@ describe('useTouchHitArea', () => {
152190 expect ( watches [ 1 ] . minSize ) . toBe ( 44 ) ;
153191 } ) ;
154192
193+ it ( 'reads the page’s touch target in rem through the root’s font size and in em through the element’s' , ( ) => {
194+ stubCoarse ( ) ;
195+ document . documentElement . style . setProperty ( 'font-size' , '16px' ) ;
196+ try {
197+ const rem = render ( < Host enabled target = "3.75rem" /> ) ;
198+ expect ( watches [ 0 ] . minSize ) . toBe ( 60 ) ;
199+ rem . unmount ( ) ;
200+ render ( < Host enabled target = "3em" fontSize = "20px" /> ) ;
201+ expect ( watches [ 1 ] . minSize ) . toBe ( 60 ) ;
202+ } finally {
203+ document . documentElement . style . removeProperty ( 'font-size' ) ;
204+ }
205+ } ) ;
206+
207+ it ( 'reads the touch target again when the primary pointer turns coarse' , ( ) => {
208+ const setCoarse = stubPointer ( false ) ;
209+ render ( < Host enabled target = "60px" /> ) ;
210+ expect ( watches . at ( - 1 ) ?. minSize ) . toBe ( 44 ) ;
211+ act ( ( ) => setCoarse ( true ) ) ;
212+ expect ( watches . at ( - 1 ) ?. minSize ) . toBe ( 60 ) ;
213+ expect ( watches . at ( - 2 ) ?. stop ) . toHaveBeenCalledTimes ( 1 ) ;
214+ } ) ;
215+
216+ it ( 'reads the touch target again when the theme changes' , ( ) => {
217+ stubCoarse ( ) ;
218+ const first = { mode : 'light' } as unknown as ThemeContextValue ;
219+ const second = { mode : 'dark' } as unknown as ThemeContextValue ;
220+ const { rerender} = render ( themed ( first , < Host enabled target = "48px" /> ) ) ;
221+ expect ( watches . at ( - 1 ) ?. minSize ) . toBe ( 48 ) ;
222+ rerender ( themed ( second , < Host enabled target = "60px" /> ) ) ;
223+ expect ( watches . at ( - 1 ) ?. minSize ) . toBe ( 60 ) ;
224+ } ) ;
225+
155226 it ( 'writes the insets it hears on the element' , ( ) => {
156227 render ( < Host enabled /> ) ;
157228 watches [ 0 ] . onInsets ( { top : 3 , right : 2 , bottom : 1 , left : 0 } ) ;
0 commit comments