2323 */
2424
2525import React from 'react'
26-
27- import {
28- expect ,
29- mount ,
30- stub ,
31- wait ,
32- wrapQueryResult
33- } from '@instructure/ui-test-utils'
26+ import { render , screen , waitFor } from '@testing-library/react'
27+ import { vi } from 'vitest'
28+ import '@testing-library/jest-dom'
3429
3530import { Tray } from '../index'
36- import { TrayLocator } from '../TrayLocator'
3731import type { TrayProps } from '../props'
3832
3933describe ( '<Tray />' , async ( ) => {
4034 it ( 'should render nothing and have a node with no parent when closed' , async ( ) => {
41- await mount ( < Tray label = "Tray Example" > Hello World</ Tray > )
42- const tray = await TrayLocator . find ( { expectEmpty : true } )
43- expect ( tray ) . to . not . exist ( )
35+ render ( < Tray label = "Tray Example" > Hello Tray</ Tray > )
36+
37+ const trayContent = screen . queryByText ( 'Hello Tray' )
38+ expect ( trayContent ) . not . toBeInTheDocument ( )
4439 } )
4540
4641 it ( 'should render children and have a node with a parent when open' , async ( ) => {
47- await mount (
42+ render (
4843 < Tray label = "Tray Example" open >
49- Hello World
44+ Hello Tray
5045 </ Tray >
5146 )
47+ const trayContent = screen . getByText ( 'Hello Tray' )
5248
53- const tray = await TrayLocator . find ( ':label(Tray Example)' )
54- await wait ( ( ) => expect ( tray . getTextContent ( ) ) . to . equal ( 'Hello World' ) )
55- } )
56-
57- it ( 'should apply theme overrides when open' , async ( ) => {
58- await mount (
59- < Tray
60- label = "Tray Example"
61- open
62- size = "small"
63- placement = "start"
64- themeOverride = { { smallWidth : '10em' } }
65- >
66- < div > Hello</ div >
67- </ Tray >
68- )
69- const tray = await TrayLocator . find ( )
70- const dialog = await tray . find ( '[role="dialog"]' )
71- await wait ( ( ) => {
72- expect ( dialog . getComputedStyle ( ) . width ) . to . equal ( '160px' )
73- } )
49+ expect ( trayContent ) . toBeInTheDocument ( )
7450 } )
7551
7652 it ( 'should apply the a11y attributes' , async ( ) => {
77- await mount (
53+ render (
7854 < Tray label = "Tray Example" open >
79- Hello World
55+ Hello Tray
8056 </ Tray >
8157 )
82- const tray = await TrayLocator . find ( )
83- const dialog = await tray . find ( '[role="dialog"]' )
58+ const tray = screen . getByRole ( 'dialog' )
8459
85- expect ( dialog . getAttribute ( 'aria-label' ) ) . to . equal ( 'Tray Example' )
60+ expect ( tray ) . toHaveAttribute ( 'aria-label' , 'Tray Example' )
8661 } )
8762
8863 it ( 'should support onOpen prop' , async ( ) => {
89- const onOpen = stub ( )
90- await mount (
64+ const onOpen = vi . fn ( )
65+ render (
9166 < Tray label = "Tray Example" open onOpen = { onOpen } >
92- Hello World
67+ Hello Tray
9368 </ Tray >
9469 )
9570
96- await wait ( ( ) => {
97- expect ( onOpen ) . to . have . been . called ( )
71+ await waitFor ( ( ) => {
72+ expect ( onOpen ) . toHaveBeenCalled ( )
9873 } )
9974 } )
10075
10176 it ( 'should support onClose prop' , async ( ) => {
102- const onClose = stub ( )
77+ const onClose = vi . fn ( )
10378
104- const subject = await mount (
79+ const { rerender } = render (
10580 < Tray label = "Tray Example" open onClose = { onClose } >
106- Hello World
81+ Hello Tray
10782 </ Tray >
10883 )
10984
110- await subject . setProps ( { open : false } )
85+ // Set prop: open
86+ rerender (
87+ < Tray label = "Tray Example" open = { false } onClose = { onClose } >
88+ Hello Tray
89+ </ Tray >
90+ )
11191
112- await wait ( ( ) => {
113- expect ( onClose ) . to . have . been . called ( )
92+ await waitFor ( ( ) => {
93+ expect ( onClose ) . toHaveBeenCalled ( )
11494 } )
11595 } )
11696
11797 it ( 'should take a prop for finding default focus' , async ( ) => {
118- await mount (
98+ render (
11999 < Tray
120100 label = "Tray Example"
121101 open
122102 defaultFocusElement = { ( ) => document . getElementById ( 'my-input' ) }
123103 >
124- Hello World
104+ Hello Tray
125105 < input type = "text" />
126- < input type = "text" id = "my-input" />
106+ < input type = "text" id = "my-input" aria-label = "my-input-label" />
127107 </ Tray >
128108 )
109+ const input = screen . getByLabelText ( 'my-input-label' )
129110
130- const tray = await TrayLocator . find ( )
131- const input = await tray . find ( '#my-input' )
132-
133- await wait ( ( ) => {
134- expect ( input . focused ( ) ) . to . be . true ( )
135- } )
136- } )
137-
138- it ( 'should call onDismiss prop when Esc key pressed' , async ( ) => {
139- const onDismiss = stub ( )
140- await mount (
141- < Tray
142- open
143- label = "Tray Example"
144- shouldCloseOnDocumentClick
145- onDismiss = { onDismiss }
146- >
147- Hello World
148- < input type = "text" />
149- < input type = "text" id = "my-input" />
150- </ Tray >
151- )
152-
153- const tray = await TrayLocator . find ( )
154-
155- await wait ( ( ) => {
156- expect ( tray . containsFocus ( ) ) . to . be . true ( )
157- } )
158-
159- await wrapQueryResult ( tray . getOwnerDocument ( ) . documentElement ) . keyUp (
160- 'escape' ,
161- {
162- defaultPrevented : false ,
163- bubbles : true ,
164- button : 0
165- } ,
166- { focusable : false }
167- )
168-
169- await wait ( ( ) => {
170- expect ( onDismiss ) . to . have . been . called ( )
111+ await waitFor ( ( ) => {
112+ expect ( document . activeElement ) . toBe ( input )
171113 } )
172114 } )
173115
@@ -204,10 +146,10 @@ describe('<Tray />', async () => {
204146 for ( const placement in placements [ dir ] . enteringPlacements ) {
205147 const val = placements [ dir ] . enteringPlacements [ placement ]
206148 it ( `returns ${ val } for ${ placement } when entering` , async ( ) => {
207- const onEntered = stub ( )
149+ const onEntered = vi . fn ( )
208150 document . documentElement . setAttribute ( 'dir' , dir )
209151
210- await mount (
152+ render (
211153 < Tray
212154 open
213155 label = "Tray Example"
@@ -217,20 +159,19 @@ describe('<Tray />', async () => {
217159 Hello
218160 </ Tray >
219161 )
220-
221- await wait ( ( ) => {
222- expect ( onEntered ) . to . have . been . called ( )
162+ await waitFor ( ( ) => {
163+ expect ( onEntered ) . toHaveBeenCalled ( )
223164 } )
224165 } )
225166 }
226167
227168 for ( const placement in placements [ dir ] . exitingPlacements ) {
228169 const val = placements [ dir ] . exitingPlacements [ placement ]
229170 it ( `returns ${ val } for ${ placement } when exiting` , async ( ) => {
230- const onExited = stub ( )
171+ const onExited = vi . fn ( )
231172 document . documentElement . setAttribute ( 'dir' , dir )
232173
233- const subject = await mount (
174+ const { rerender } = render (
234175 < Tray
235176 open
236177 label = "Tray Example"
@@ -241,10 +182,19 @@ describe('<Tray />', async () => {
241182 </ Tray >
242183 )
243184
244- await subject . setProps ( { open : false } )
245-
246- await wait ( ( ) => {
247- expect ( onExited ) . to . have . been . called ( )
185+ // Set prop: open
186+ rerender (
187+ < Tray
188+ open = { false }
189+ label = "Tray Example"
190+ placement = { placement as TrayProps [ 'placement' ] }
191+ onExited = { onExited }
192+ >
193+ Hello
194+ </ Tray >
195+ )
196+ await waitFor ( ( ) => {
197+ expect ( onExited ) . toHaveBeenCalled ( )
248198 } )
249199 } )
250200 }
0 commit comments