1- // Control UI tests cover the shared reduced-motion-aware scroll behavior resolver.
21import { afterEach , describe , expect , it , vi } from "vitest" ;
32import { resolveScrollBehavior } from "./scroll-behavior.ts" ;
43
5- // jsdom does not implement window.matchMedia, so each test installs a
6- // controllable stub and removes it afterwards to keep behavior observable.
7- function stubMatchMedia ( reduced : boolean ) : ReturnType < typeof vi . fn > {
4+ function stubMatchMedia ( reduced : boolean ) {
85 const matchMedia = vi . fn ( ) . mockReturnValue ( { matches : reduced } ) ;
96 vi . stubGlobal ( "matchMedia" , matchMedia ) ;
107 return matchMedia ;
@@ -15,30 +12,25 @@ afterEach(() => {
1512} ) ;
1613
1714describe ( "resolveScrollBehavior" , ( ) => {
18- it ( "downgrades smooth scrolling to auto under prefers- reduced-motion: reduce " , ( ) => {
15+ it ( "uses auto for smooth scrolling under reduced motion " , ( ) => {
1916 stubMatchMedia ( true ) ;
2017 expect ( resolveScrollBehavior ( ) ) . toBe ( "auto" ) ;
21- expect ( resolveScrollBehavior ( "smooth" ) ) . toBe ( "auto" ) ;
2218 } ) ;
2319
24- it ( "keeps smooth scrolling when reduced motion is not requested " , ( ) => {
20+ it ( "keeps smooth scrolling without reduced motion" , ( ) => {
2521 const matchMedia = stubMatchMedia ( false ) ;
26- expect ( resolveScrollBehavior ( ) ) . toBe ( "smooth" ) ;
2722 expect ( resolveScrollBehavior ( "smooth" ) ) . toBe ( "smooth" ) ;
2823 expect ( matchMedia ) . toHaveBeenCalledWith ( "(prefers-reduced-motion: reduce)" ) ;
2924 } ) ;
3025
31- it ( "keeps the requested behavior when matchMedia is unavailable" , ( ) => {
32- // Node/SSR-style environments may lack window.matchMedia entirely.
26+ it ( "keeps smooth scrolling when matchMedia is unavailable" , ( ) => {
3327 vi . stubGlobal ( "matchMedia" , undefined ) ;
34- expect ( resolveScrollBehavior ( ) ) . toBe ( "smooth" ) ;
3528 expect ( resolveScrollBehavior ( "smooth" ) ) . toBe ( "smooth" ) ;
3629 } ) ;
3730
38- it ( "preserves non-animated behaviors without consulting the media query ", ( ) => {
31+ it . each ( [ "auto" , "instant" ] as const ) ( "preserves %s without querying motion ", ( behavior ) => {
3932 const matchMedia = stubMatchMedia ( true ) ;
40- expect ( resolveScrollBehavior ( "auto" ) ) . toBe ( "auto" ) ;
41- expect ( resolveScrollBehavior ( "instant" ) ) . toBe ( "instant" ) ;
33+ expect ( resolveScrollBehavior ( behavior ) ) . toBe ( behavior ) ;
4234 expect ( matchMedia ) . not . toHaveBeenCalled ( ) ;
4335 } ) ;
4436} ) ;
0 commit comments