1- // @vitest -environment jsdom
2-
31import { act } from "react" ;
42import { createRoot , type Root } from "react-dom/client" ;
53import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
4+ import { userEvent } from "vite-plus/test/browser" ;
65
76const families = vi . hoisted ( ( ) => {
87 const families = [
@@ -32,23 +31,6 @@ let container: HTMLDivElement;
3231
3332beforeEach ( async ( ) => {
3433 vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
35- vi . stubGlobal (
36- "ResizeObserver" ,
37- class {
38- observe ( ) { }
39- disconnect ( ) { }
40- unobserve ( ) { }
41- } ,
42- ) ;
43- // jsdom has no layout; give LegendList a 30px viewport and 30px rows so it
44- // renders a window of rows the way it does in a browser.
45- vi . spyOn ( Element . prototype , "getBoundingClientRect" ) . mockReturnValue (
46- DOMRect . fromRect ( { width : 288 , height : 30 } ) ,
47- ) ;
48- Object . defineProperty ( Element . prototype , "getAnimations" , {
49- configurable : true ,
50- value : ( ) => [ ] ,
51- } ) ;
5234 container = document . createElement ( "div" ) ;
5335 document . body . append ( container ) ;
5436 root = createRoot ( container ) ;
@@ -68,14 +50,6 @@ function highlightedOptions(): string[] {
6850 ) ;
6951}
7052
71- async function typeQuery ( input : HTMLInputElement , value : string ) {
72- const setValue = Object . getOwnPropertyDescriptor ( HTMLInputElement . prototype , "value" ) ! . set ! ;
73- await act ( async ( ) => {
74- setValue . call ( input , value ) ;
75- input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
76- } ) ;
77- }
78-
7953describe ( "FontFamilyPicker" , ( ) => {
8054 it ( "highlights, moves through, and selects matches after filtering" , async ( ) => {
8155 const onSelect = vi . fn ( ) ;
@@ -93,14 +67,11 @@ describe("FontFamilyPicker", () => {
9367
9468 const input = document . querySelector < HTMLInputElement > ( 'input[placeholder="Search fonts…"]' ) ;
9569 expect ( input ) . not . toBeNull ( ) ;
96- const press = ( key : string ) =>
97- act ( async ( ) => {
98- input ! . dispatchEvent ( new KeyboardEvent ( "keydown" , { key, bubbles : true } ) ) ;
99- } ) ;
70+ const press = ( key : string ) => act ( ( ) => userEvent . keyboard ( `{${ key } }` ) ) ;
10071
10172 // Both matches were already rendered further down the unfiltered list.
102- await typeQuery ( input ! , "co" ) ;
103- expect ( highlightedOptions ( ) ) . toEqual ( [ "Courier New" ] ) ;
73+ await act ( ( ) => userEvent . type ( input ! , "co" ) ) ;
74+ await expect . poll ( highlightedOptions ) . toEqual ( [ "Courier New" ] ) ;
10475
10576 await press ( "ArrowDown" ) ;
10677 expect ( highlightedOptions ( ) ) . toEqual ( [ "Fira Code" ] ) ;
0 commit comments