1
- import { beforeEach , describe , expect , test } from 'vitest'
1
+ import { setTimeout } from 'node:timers/promises'
2
+
3
+ import { act , render } from '@testing-library/svelte'
4
+ import { describe , expect , test } from 'vitest'
2
5
3
- import { act , fireEvent , render as stlRender } from '..'
4
6
import Comp from './fixtures/Comp.svelte'
5
7
6
8
describe ( 'act' , ( ) => {
7
- let props
8
-
9
- const render = ( ) => {
10
- return stlRender ( Comp , {
11
- props
12
- } )
13
- }
14
-
15
- beforeEach ( ( ) => {
16
- props = {
17
- name : 'World'
18
- }
19
- } )
20
-
21
9
test ( 'state updates are flushed' , async ( ) => {
22
- const { getByText } = render ( )
10
+ const { getByText } = render ( Comp )
23
11
const button = getByText ( 'Button' )
24
12
25
13
expect ( button ) . toHaveTextContent ( 'Button' )
@@ -31,24 +19,13 @@ describe('act', () => {
31
19
expect ( button ) . toHaveTextContent ( 'Button Clicked' )
32
20
} )
33
21
34
- test ( 'findByTestId returns the element' , async ( ) => {
35
- const { findByTestId } = render ( )
36
-
37
- expect ( await findByTestId ( 'test' ) ) . toHaveTextContent ( `Hello ${ props . name } !` )
38
- } )
39
-
40
22
test ( 'accepts async functions' , async ( ) => {
41
- const sleep = ( ms ) =>
42
- new Promise ( ( resolve ) => {
43
- setTimeout ( ( ) => resolve ( ) , ms )
44
- } )
45
-
46
- const { getByText } = render ( )
23
+ const { getByText } = render ( Comp )
47
24
const button = getByText ( 'Button' )
48
25
49
26
await act ( async ( ) => {
50
- await sleep ( 100 )
51
- await fireEvent . click ( button )
27
+ await setTimeout ( 100 )
28
+ button . click ( )
52
29
} )
53
30
54
31
expect ( button ) . toHaveTextContent ( 'Button Clicked' )
0 commit comments