@@ -17,6 +17,8 @@ class TestClock {
1717 private animationFrames = new Map < number , FrameRequestCallback > ( ) ;
1818 private timeouts = new Map < number , { at : number ; callback : ( ) => void } > ( ) ;
1919
20+ reducedMotion = false ;
21+
2022 readonly env = {
2123 now : ( ) => this . currentTime ,
2224 requestAnimationFrame : ( callback : FrameRequestCallback ) => {
@@ -37,6 +39,7 @@ class TestClock {
3739 clearTimeout : ( handle : number ) => {
3840 this . timeouts . delete ( handle ) ;
3941 } ,
42+ prefersReducedMotion : ( ) => this . reducedMotion ,
4043 } ;
4144
4245 advanceBy ( ms : number , frameMs = 16 ) {
@@ -201,33 +204,74 @@ describe("createPageScrollController", () => {
201204 ) ;
202205 } ) ;
203206
204- test ( "continues scrolling on hold without repeated keydown events and stops on keyup " , ( ) => {
207+ test ( "uses the current motion preference for page taps in both directions " , ( ) => {
205208 const clock = new TestClock ( ) ;
206209 const container = {
207210 clientHeight : 600 ,
208211 scrollHeight : 4_000 ,
209- scrollTop : 0 ,
212+ scrollTop : 1_200 ,
210213 getBoundingClientRect : ( ) => ( { height : 600 } ) ,
211214 } ;
212215 const controller = createPageScrollController ( {
213216 getContainer : ( ) => container ,
214217 getScrollPaddingBottomPx : ( ) => 24 ,
215218 env : clock . env ,
216219 } ) ;
217- controller . handleKeyDown ( "PageDown" ) ;
218- clock . advanceBy ( PAGE_SCROLL_ANIMATION_MS + 50 ) ;
219220
220- const afterHoldStarts = container . scrollTop ;
221- clock . advanceBy ( 200 ) ;
221+ for ( const reducedMotion of [ false , true , false ] ) {
222+ clock . reducedMotion = reducedMotion ;
223+ for ( const [ key , destination ] of [
224+ [ "PageDown" , 1_740 ] ,
225+ [ "PageUp" , 1_200 ] ,
226+ ] as const ) {
227+ const start = container . scrollTop ;
228+ controller . handleKeyDown ( key ) ;
229+ controller . handleKeyUp ( key ) ;
230+
231+ if ( ! reducedMotion ) {
232+ clock . advanceBy ( 16 ) ;
233+ expect ( container . scrollTop ) . toBeGreaterThan ( Math . min ( start , destination ) ) ;
234+ expect ( container . scrollTop ) . toBeLessThan ( Math . max ( start , destination ) ) ;
235+ clock . advanceBy ( PAGE_SCROLL_ANIMATION_MS ) ;
236+ }
237+ expect ( container . scrollTop ) . toBeCloseTo ( destination , 5 ) ;
238+ clock . advanceBy ( PAGE_SCROLL_ANIMATION_MS * 2 ) ;
239+ expect ( container . scrollTop ) . toBeCloseTo ( destination , 5 ) ;
240+ }
241+ }
242+ } ) ;
243+
244+ test . each ( [ false , true ] ) (
245+ "continues scrolling on hold without repeated keydown events and stops on keyup (reduced motion: %s)" ,
246+ ( reducedMotion ) => {
247+ const clock = new TestClock ( ) ;
248+ clock . reducedMotion = reducedMotion ;
249+ const container = {
250+ clientHeight : 600 ,
251+ scrollHeight : 4_000 ,
252+ scrollTop : 0 ,
253+ getBoundingClientRect : ( ) => ( { height : 600 } ) ,
254+ } ;
255+ const controller = createPageScrollController ( {
256+ getContainer : ( ) => container ,
257+ getScrollPaddingBottomPx : ( ) => 24 ,
258+ env : clock . env ,
259+ } ) ;
260+ controller . handleKeyDown ( "PageDown" ) ;
261+ clock . advanceBy ( PAGE_SCROLL_ANIMATION_MS + 50 ) ;
222262
223- expect ( container . scrollTop ) . toBeGreaterThan ( afterHoldStarts ) ;
263+ const afterHoldStarts = container . scrollTop ;
264+ clock . advanceBy ( 200 ) ;
224265
225- const stoppedAt = container . scrollTop ;
226- controller . handleKeyUp ( "PageDown" ) ;
227- clock . advanceBy ( 250 ) ;
266+ expect ( container . scrollTop ) . toBeGreaterThan ( afterHoldStarts ) ;
228267
229- expect ( container . scrollTop ) . toBe ( stoppedAt ) ;
230- } ) ;
268+ const stoppedAt = container . scrollTop ;
269+ controller . handleKeyUp ( "PageDown" ) ;
270+ clock . advanceBy ( 250 ) ;
271+
272+ expect ( container . scrollTop ) . toBe ( stoppedAt ) ;
273+ } ,
274+ ) ;
231275
232276 test ( "notifies once when a page scroll starts" , ( ) => {
233277 const clock = new TestClock ( ) ;
0 commit comments