1
1
import { composeStories } from "@storybook/react" ;
2
2
import * as stories from "./FeedbackContainer.stories" ;
3
- import { act , render , screen } from "@testing-library/react" ;
3
+ import { render , screen , waitFor } from "@testing-library/react" ;
4
4
import * as React from "react" ;
5
5
import "@testing-library/jest-dom" ;
6
6
import { prepareStoryContextForTest } from "@nova/react-test-utils" ;
7
+ import { executePlayFunction } from "../testing-utils/executePlayFunction" ;
7
8
8
9
const { AutoGeneratedDataOnly, Primary, Liked, Like, LikeFailure, Loading } =
9
10
composeStories ( stories ) ;
@@ -29,17 +30,16 @@ describe("FeedbackContainer", () => {
29
30
30
31
it ( "should show unlike button after clicking like button" , async ( ) => {
31
32
const { container } = render ( < Like /> ) ;
32
- await act ( async ( ) => Like . play ( { canvasElement : container } ) ) ;
33
+ await executePlayFunction ( Like , { canvasElement : container } ) ;
33
34
const button = await screen . findByRole ( "button" , { name : "Unlike" } ) ;
34
35
expect ( button ) . toBeInTheDocument ( ) ;
35
36
} ) ;
36
37
37
38
it ( "should show an error if the like button fails" , async ( ) => {
38
39
const { container } = render ( < LikeFailure /> ) ;
39
- // This needs to be wrapped in act as play function for this story
40
- // relies on mock client to resolve queries which updates component state
41
- await act ( async ( ) =>
42
- LikeFailure . play ( prepareStoryContextForTest ( LikeFailure , container ) ) ,
40
+ await executePlayFunction (
41
+ LikeFailure ,
42
+ prepareStoryContextForTest ( LikeFailure , container ) ,
43
43
) ;
44
44
const error = await screen . findByText ( "Something went wrong" ) ;
45
45
expect ( error ) . toBeInTheDocument ( ) ;
@@ -55,8 +55,14 @@ describe("FeedbackContainer", () => {
55
55
render ( < Primary /> ) ;
56
56
render ( < Liked /> ) ;
57
57
render ( < Primary /> ) ;
58
- const texts = await screen . findAllByText ( "Feedback: Feedback title" ) ;
59
- expect ( texts ) . toHaveLength ( 3 ) ;
60
- expect ( texts [ 2 ] ) . toBeInTheDocument ( ) ;
58
+ const text = "Feedback: Feedback title" ;
59
+ await waitFor (
60
+ ( ) => {
61
+ const texts = screen . getAllByText ( text ) ;
62
+ expect ( texts ) . toHaveLength ( 3 ) ;
63
+ } ,
64
+ { timeout : 3000 } ,
65
+ ) ;
66
+ expect ( screen . getAllByText ( text ) [ 2 ] ) . toBeInTheDocument ( ) ;
61
67
} ) ;
62
68
} ) ;
0 commit comments