1
- import { fireEvent , render , screen } from "@testing-library/react" ;
1
+ import { fireEvent , render , screen , waitFor } from "@testing-library/react" ;
2
2
import "@testing-library/jest-dom" ;
3
3
import QuestionCategoryAutoComplete from "." ;
4
4
5
+ jest . mock ( "../../utils/api" , ( ) => ( {
6
+ questionClient : {
7
+ get : jest . fn ( ) . mockResolvedValue ( { data : { categories : [ "DFS" ] } } ) ,
8
+ } ,
9
+ } ) ) ;
10
+
5
11
describe ( "Question Category Auto Complete" , ( ) => {
6
12
const selectedCategories : string [ ] = [ "DFS" ] ;
7
13
const setSelectedCategories = jest . fn ( ) ;
8
14
9
- it ( "Question Category Auto Complete is rendered" , ( ) => {
15
+ it ( "Question Category Auto Complete is rendered" , async ( ) => {
10
16
render (
11
17
< QuestionCategoryAutoComplete
12
18
selectedCategories = { selectedCategories }
13
19
setSelectedCategories = { setSelectedCategories }
14
- /> ,
20
+ />
15
21
) ;
16
22
17
- const category = screen . getByText ( "DFS" ) ;
23
+ const category = await screen . findByText ( "DFS" ) ;
18
24
19
25
expect ( category ) . toBeInTheDocument ( ) ;
20
26
} ) ;
21
27
22
- it ( "Adding a new category from the category list" , ( ) => {
28
+ it ( "Adding a new category from the category list" , async ( ) => {
23
29
render (
24
30
< QuestionCategoryAutoComplete
25
31
selectedCategories = { selectedCategories }
26
32
setSelectedCategories = { setSelectedCategories }
27
- /> ,
33
+ />
28
34
) ;
29
35
30
36
const input = screen . getByLabelText ( "Category" ) ;
31
37
fireEvent . change ( input , { target : { value : "DFS" } } ) ;
32
38
33
- expect ( screen . getByText ( "DFS" ) ) . toBeInTheDocument ( ) ;
39
+ await waitFor ( ( ) => expect ( screen . getByText ( "DFS" ) ) . toBeInTheDocument ( ) ) ;
34
40
} ) ;
35
41
36
- it ( "Adding a new category not from the category list" , ( ) => {
42
+ it ( "Adding a new category not from the category list" , async ( ) => {
37
43
const { rerender } = render (
38
44
< QuestionCategoryAutoComplete
39
45
selectedCategories = { selectedCategories }
40
46
setSelectedCategories = { setSelectedCategories }
41
- /> ,
47
+ />
42
48
) ;
43
49
44
50
const input = screen . getByLabelText ( "Category" ) ;
45
51
fireEvent . change ( input , { target : { value : "New Category" } } ) ;
46
52
47
53
const valueAdded = 'Add: "New Category"' ;
48
- expect ( screen . getByText ( valueAdded ) ) . toBeInTheDocument ( ) ;
54
+ expect ( await screen . findByText ( valueAdded ) ) . toBeInTheDocument ( ) ;
49
55
50
56
fireEvent . click ( screen . getByText ( valueAdded ) ) ;
51
57
@@ -55,23 +61,23 @@ describe("Question Category Auto Complete", () => {
55
61
< QuestionCategoryAutoComplete
56
62
selectedCategories = { updatedCategories }
57
63
setSelectedCategories = { setSelectedCategories }
58
- /> ,
64
+ />
59
65
) ;
60
66
61
67
expect ( screen . getByText ( "New Category" ) ) . toBeInTheDocument ( ) ;
62
68
} ) ;
63
69
64
- it ( "Remove a category from selected categories" , ( ) => {
70
+ it ( "Remove a category from selected categories" , async ( ) => {
65
71
render (
66
72
< QuestionCategoryAutoComplete
67
73
selectedCategories = { selectedCategories }
68
74
setSelectedCategories = { setSelectedCategories }
69
- /> ,
75
+ />
70
76
) ;
71
77
72
78
const deleteButton = screen . getByTestId ( "CancelIcon" ) ;
73
79
fireEvent . click ( deleteButton ) ;
74
80
75
- expect ( setSelectedCategories ) . toHaveBeenCalledWith ( [ ] ) ;
81
+ await waitFor ( ( ) => expect ( setSelectedCategories ) . toHaveBeenCalledWith ( [ ] ) ) ;
76
82
} ) ;
77
83
} ) ;
0 commit comments