1
1
import React from 'react' ;
2
2
import { when , resetAllWhenMocks } from 'jest-when' ;
3
3
import {
4
- screen , render , waitFor ,
4
+ screen , render , waitFor , fireEvent , act ,
5
5
} from '@testing-library/react' ;
6
6
import { ModuleRegistry } from '@ag-grid-community/core' ;
7
7
import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model' ;
@@ -29,6 +29,7 @@ const mockReport = {
29
29
30
30
const withReportContext = ( Component ) => function ReportContextHOC ( props ) {
31
31
return (
32
+ // eslint-disable-next-line react/jsx-no-constructed-context-values
32
33
< ReportContext . Provider value = { { report : mockReport , setReport : ( ) => { } } } >
33
34
< Component { ...props } />
34
35
</ ReportContext . Provider >
@@ -52,30 +53,67 @@ describe('GeneViewer', () => {
52
53
const { asFragment } = render (
53
54
< Component
54
55
gene = { mockGene }
55
- isOpen
56
56
/> ,
57
57
) ;
58
58
59
59
expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
60
60
} ) ;
61
61
62
- test ( 'It calls onClose if the gene does not exist' , async ( ) => {
62
+ test ( 'It renders the gene name' , async ( ) => {
63
+ render (
64
+ < Component
65
+ gene = { mockGene }
66
+ /> ,
67
+ ) ;
68
+
69
+ expect ( await screen . findByText ( mockGene ) ) . toBeInTheDocument ( ) ;
70
+ } ) ;
71
+
72
+ test ( 'It renders the gene and the link button, if isLink prop is given' , async ( ) => {
73
+ render (
74
+ < Component
75
+ gene = { mockGene }
76
+ isLink
77
+ /> ,
78
+ ) ;
79
+
80
+ expect ( await screen . findByText ( mockGene ) ) . toBeInTheDocument ( ) ;
81
+ expect ( screen . getByRole ( 'button' ) ) . toBeInTheDocument ( ) ;
82
+ } ) ;
83
+
84
+ test ( 'It renders a modal when the gene name is clicked' , async ( ) => {
85
+ render (
86
+ < Component
87
+ gene = { mockGene }
88
+ isLink
89
+ /> ,
90
+ ) ;
91
+ act ( ( ) => {
92
+ fireEvent . click ( screen . getByRole ( 'button' ) ) ;
93
+ } ) ;
94
+ expect ( screen . getByRole ( 'dialog' ) ) . toBeInTheDocument ( ) ;
95
+ } ) ;
96
+
97
+ test ( 'It closes the dialog if gene does not exist' , async ( ) => {
63
98
when ( api . get as ( endpoint : string ) => Partial < ApiCall > )
64
99
. calledWith ( `/reports/${ mockReport . ident } /gene-viewer/${ mockErrorGene } ` )
65
100
. mockImplementation ( ( ) => ( { request : async ( ) => { throw new Error ( ) ; } } ) ) ;
66
101
67
- const mockOnClose = jest . fn ( ) ;
68
-
69
102
render (
70
103
< Component
71
104
gene = { mockErrorGene }
72
- isOpen
73
- onClose = { mockOnClose }
105
+ isLink
74
106
/> ,
75
107
) ;
76
-
77
- await waitFor ( ( ) => expect ( mockOnClose ) . toHaveBeenCalledTimes ( 1 ) ) ;
78
- await waitFor ( ( ) => expect ( mockOnClose ) . toHaveBeenCalledWith ( ) ) ;
108
+ const button = await screen . findByText ( mockErrorGene ) ;
109
+ act ( ( ) => {
110
+ fireEvent . click ( button ) ;
111
+ } ) ;
112
+ const dialog = screen . getByRole ( 'dialog' ) ;
113
+ expect ( dialog ) . toBeInTheDocument ( ) ;
114
+ await waitFor ( ( ) => {
115
+ expect ( dialog ) . not . toBeInTheDocument ( ) ;
116
+ } ) ;
79
117
} ) ;
80
118
81
119
test ( 'Tabs are all shown' , async ( ) => {
@@ -86,18 +124,20 @@ describe('GeneViewer', () => {
86
124
render (
87
125
< Component
88
126
gene = { mockGene }
89
- isOpen
90
- onClose = { ( ) => { } }
127
+ isLink
91
128
/> ,
92
129
) ;
93
130
94
- const promises = [ ] ;
131
+ const button = await screen . findByText ( mockGene ) ;
132
+
133
+ act ( ( ) => {
134
+ fireEvent . click ( button ) ;
135
+ } ) ;
136
+
137
+ await screen . getByRole ( 'dialog' ) ;
138
+
95
139
for ( const key of Object . keys ( mockGeneResults ) ) {
96
- promises . push ( screen . findByText ( key , { exact : false } ) ) ;
97
- }
98
- const resolved = await Promise . all ( promises ) ;
99
- for ( const elem of resolved ) {
100
- expect ( elem ) . toBeInTheDocument ( ) ;
140
+ screen . getByText ( key , { exact : false } ) ;
101
141
}
102
142
} ) ;
103
143
} ) ;
0 commit comments