1
1
import React from 'react' ;
2
- import { render , fireEvent } from "@testing-library/react" ;
2
+ import {
3
+ render ,
4
+ fireEvent ,
5
+ queryByAttribute ,
6
+ queryAllByAttribute
7
+ } from "@testing-library/react" ;
3
8
import { IStep } from '../stepper-component/types' ;
4
9
import Stepper from "../stepper-component/stepperComponent" ;
5
10
11
+ const getById = queryByAttribute . bind ( null , 'id' ) ;
12
+ const getAllById = queryAllByAttribute . bind ( null , 'id' ) ;
6
13
test ( "Stepper Component - Label and description" , async ( ) => {
7
14
const steps : IStep [ ] = [ {
8
15
label : 'Step 1' ,
9
16
description : 'Demo description' ,
10
17
status : 'completed'
11
18
} ]
12
- const { findByTestId } = render ( < Stepper steps = { steps } /> )
13
- const label = await findByTestId ( "stepper-label-0" ) ;
19
+ const dom = render ( < Stepper steps = { steps } /> )
20
+ const label = await getById ( dom . container , "stepper-label-0" ) ;
14
21
expect ( label . innerHTML ) . toBe ( 'Step 1' ) ;
15
- const description = await findByTestId ( "stepper-desc-0" ) ;
22
+ const description = await getById ( dom . container , "stepper-desc-0" ) ;
16
23
expect ( description . innerHTML ) . toBe ( 'Demo description' ) ;
17
24
} ) ;
18
25
@@ -21,9 +28,10 @@ test("Stepper Component - No description", async () => {
21
28
label : 'Step 1' ,
22
29
status : 'completed'
23
30
} ] ;
24
- const { findByTestId } = render ( < Stepper steps = { steps } /> )
31
+ const dom = render ( < Stepper steps = { steps } /> )
25
32
try {
26
- await findByTestId ( "stepper-desc-0" ) ;
33
+ const val = await getById ( dom . container , "stepper-desc-0" ) ;
34
+ if ( val === null ) throw Error ( ) ;
27
35
} catch ( err ) {
28
36
return ;
29
37
}
@@ -38,8 +46,8 @@ test("Stepper Component - Number of steps", async () => {
38
46
label : 'Step 2' ,
39
47
status : 'visited'
40
48
} ] ;
41
- const { findAllByTestId } = render ( < Stepper steps = { steps } /> ) ;
42
- const elements = await findAllByTestId ( "stepper-steps" ) ;
49
+ const dom = render ( < Stepper steps = { steps } /> ) ;
50
+ const elements = await getAllById ( dom . container , "stepper-steps" ) ;
43
51
expect ( elements ?. length ) . toBe ( 2 ) ;
44
52
} )
45
53
@@ -50,13 +58,13 @@ test("Stepper Component - On Click function", async () => {
50
58
status : 'completed'
51
59
} ] ;
52
60
const onClick = jest . fn ( ) ;
53
- const { findByTestId } = render (
61
+ const dom = render (
54
62
< Stepper
55
63
steps = { steps }
56
64
onStepClick = { onClick }
57
65
/>
58
66
)
59
- const bubble = await findByTestId ( "stepper-bubble" ) ;
67
+ const bubble = await getById ( dom . container , "stepper-bubble" ) ;
60
68
fireEvent . click ( bubble ) ;
61
69
expect ( onClick ) . toBeCalled ( ) ;
62
70
} )
0 commit comments