1
- import React from 'react' ;
1
+ import { render , screen } from '@testing-library/ react' ;
2
2
3
- import { ErrorPage } from '@edx/frontend-platform/react' ;
4
- import { ModalDialog } from '@openedx/paragon' ;
5
- import { shallow } from '@edx/react-unit-test-utils' ;
6
-
7
- import PageLoading from '@src/generic/PageLoading' ;
8
-
9
- import { ContentIFrameLoaderSlot } from '@src/plugin-slots/ContentIFrameLoaderSlot' ;
10
3
import * as hooks from './hooks' ;
11
- import ContentIFrame , { IFRAME_FEATURE_POLICY , testIDs } from './ContentIFrame' ;
4
+ import ContentIFrame , { IFRAME_FEATURE_POLICY } from './ContentIFrame' ;
12
5
13
- jest . mock ( '@edx/frontend-platform/react' , ( ) => ( { ErrorPage : ' ErrorPage' } ) ) ;
6
+ jest . mock ( '@edx/frontend-platform/react' , ( ) => ( { ErrorPage : ( ) => < div > ErrorPage</ div > } ) ) ;
14
7
15
- jest . mock ( '@openedx/paragon' , ( ) => jest . requireActual ( '@edx/react-unit-test-utils' )
16
- . mockComponents ( {
17
- ModalDialog : {
18
- Body : 'ModalDialog.Body' ,
19
- } ,
20
- } ) ) ;
21
-
22
- jest . mock ( '@src/generic/PageLoading' , ( ) => 'PageLoading' ) ;
8
+ jest . mock ( '@src/generic/PageLoading' , ( ) => jest . fn ( ( ) => < div > PageLoading</ div > ) ) ;
23
9
24
10
jest . mock ( './hooks' , ( ) => ( {
25
11
useIFrameBehavior : jest . fn ( ) ,
@@ -67,14 +53,13 @@ const props = {
67
53
title : 'test-title' ,
68
54
} ;
69
55
70
- let el ;
71
56
describe ( 'ContentIFrame Component' , ( ) => {
72
57
beforeEach ( ( ) => {
73
58
jest . clearAllMocks ( ) ;
74
59
} ) ;
75
60
describe ( 'behavior' , ( ) => {
76
61
beforeEach ( ( ) => {
77
- el = shallow ( < ContentIFrame { ...props } /> ) ;
62
+ render ( < ContentIFrame { ...props } /> ) ;
78
63
} ) ;
79
64
it ( 'initializes iframe behavior hook' , ( ) => {
80
65
expect ( hooks . useIFrameBehavior ) . toHaveBeenCalledWith ( {
@@ -89,61 +74,61 @@ describe('ContentIFrame Component', () => {
89
74
} ) ;
90
75
} ) ;
91
76
describe ( 'output' , ( ) => {
92
- let component ;
93
77
describe ( 'if shouldShowContent' , ( ) => {
94
78
describe ( 'if not hasLoaded' , ( ) => {
95
79
it ( 'displays errorPage if showError' , ( ) => {
96
80
hooks . useIFrameBehavior . mockReturnValueOnce ( { ...iframeBehavior , showError : true } ) ;
97
- el = shallow ( < ContentIFrame { ...props } /> ) ;
98
- expect ( el . instance . findByType ( ErrorPage ) . length ) . toEqual ( 1 ) ;
81
+ render ( < ContentIFrame { ...props } /> ) ;
82
+ const errorPage = screen . getByText ( 'ErrorPage' ) ;
83
+ expect ( errorPage ) . toBeInTheDocument ( ) ;
99
84
} ) ;
100
85
it ( 'displays PageLoading component if not showError' , ( ) => {
101
- el = shallow ( < ContentIFrame { ...props } /> ) ;
102
- [ component ] = el . instance . findByType ( ContentIFrameLoaderSlot ) ;
103
- expect ( component . props . loadingMessage ) . toEqual ( props . loadingMessage ) ;
86
+ render ( < ContentIFrame { ...props } /> ) ;
87
+ const pageLoading = screen . getByText ( 'PageLoading' ) ;
88
+ expect ( pageLoading ) . toBeInTheDocument ( ) ;
104
89
} ) ;
105
90
} ) ;
106
91
describe ( 'hasLoaded' , ( ) => {
107
92
it ( 'does not display PageLoading or ErrorPage' , ( ) => {
108
93
hooks . useIFrameBehavior . mockReturnValueOnce ( { ...iframeBehavior , hasLoaded : true } ) ;
109
- el = shallow ( < ContentIFrame { ...props } /> ) ;
110
- expect ( el . instance . findByType ( PageLoading ) . length ) . toEqual ( 0 ) ;
111
- expect ( el . instance . findByType ( ErrorPage ) . length ) . toEqual ( 0 ) ;
94
+ render ( < ContentIFrame { ...props } /> ) ;
95
+ const pageLoading = screen . queryByText ( 'PageLoading' ) ;
96
+ expect ( pageLoading ) . toBeNull ( ) ;
97
+ const errorPage = screen . queryByText ( 'ErrorPage' ) ;
98
+ expect ( errorPage ) . toBeNull ( ) ;
112
99
} ) ;
113
100
} ) ;
114
101
it ( 'display iframe with props from hooks' , ( ) => {
115
- el = shallow ( < ContentIFrame { ...props } /> ) ;
116
- [ component ] = el . instance . findByTestId ( testIDs . contentIFrame ) ;
117
- expect ( component . props ) . toEqual ( {
118
- allow : IFRAME_FEATURE_POLICY ,
119
- allowFullScreen : true ,
120
- scrolling : 'no' ,
121
- referrerPolicy : 'origin' ,
122
- title : props . title ,
123
- id : props . elementId ,
124
- src : props . iframeUrl ,
125
- height : iframeBehavior . iframeHeight ,
126
- onLoad : iframeBehavior . handleIFrameLoad ,
127
- 'data-testid' : testIDs . contentIFrame ,
128
- } ) ;
102
+ render ( < ContentIFrame { ...props } /> ) ;
103
+ const iframe = screen . getByTitle ( props . title ) ;
104
+ expect ( iframe ) . toBeInTheDocument ( ) ;
105
+ expect ( iframe ) . toHaveAttribute ( 'id' , props . elementId ) ;
106
+ expect ( iframe ) . toHaveAttribute ( 'src' , props . iframeUrl ) ;
107
+ expect ( iframe ) . toHaveAttribute ( 'allow' , IFRAME_FEATURE_POLICY ) ;
108
+ expect ( iframe ) . toHaveAttribute ( 'allowfullscreen' , '' ) ;
109
+ expect ( iframe ) . toHaveAttribute ( 'scrolling' , 'no' ) ;
110
+ expect ( iframe ) . toHaveAttribute ( 'referrerpolicy' , 'origin' ) ;
129
111
} ) ;
130
112
} ) ;
131
113
describe ( 'if not shouldShowContent' , ( ) => {
132
114
it ( 'does not show PageLoading, ErrorPage, or unit-iframe-wrapper' , ( ) => {
133
- el = shallow ( < ContentIFrame { ...{ ...props , shouldShowContent : false } } /> ) ;
134
- expect ( el . instance . findByType ( PageLoading ) . length ) . toEqual ( 0 ) ;
135
- expect ( el . instance . findByType ( ErrorPage ) . length ) . toEqual ( 0 ) ;
136
- expect ( el . instance . findByTestId ( testIDs . contentIFrame ) . length ) . toEqual ( 0 ) ;
115
+ render ( < ContentIFrame { ...{ ...props , shouldShowContent : false } } /> ) ;
116
+ expect ( screen . queryByText ( ' PageLoading' ) ) . toBeNull ( ) ;
117
+ expect ( screen . queryByText ( ' ErrorPage' ) ) . toBeNull ( ) ;
118
+ expect ( screen . queryByTitle ( props . title ) ) . toBeNull ( ) ;
137
119
} ) ;
138
120
} ) ;
139
121
it ( 'does not display modal if modalOptions returns isOpen: false' , ( ) => {
140
- el = shallow ( < ContentIFrame { ...props } /> ) ;
141
- expect ( el . instance . findByType ( ModalDialog ) . length ) . toEqual ( 0 ) ;
122
+ render ( < ContentIFrame { ...props } /> ) ;
123
+ const modal = screen . queryByRole ( 'dialog' ) ;
124
+ expect ( modal ) . toBeNull ( ) ;
142
125
} ) ;
143
126
describe ( 'if modalOptions.isOpen' , ( ) => {
144
127
const testModalOpenAndHandleClose = ( ) => {
145
- test ( 'Modal component isOpen, with handleModalClose from hook' , ( ) => {
146
- expect ( component . props . onClose ) . toEqual ( modalIFrameData . handleModalClose ) ;
128
+ it ( 'closes modal on close button click' , ( ) => {
129
+ const closeButton = screen . getByTestId ( 'modal-backdrop' ) ;
130
+ closeButton . click ( ) ;
131
+ expect ( modalIFrameData . handleModalClose ) . toHaveBeenCalled ( ) ;
147
132
} ) ;
148
133
} ;
149
134
describe ( 'fullscreen modal' , ( ) => {
@@ -153,14 +138,13 @@ describe('ContentIFrame Component', () => {
153
138
...modalIFrameData ,
154
139
modalOptions : { ...modalOptions . withBody , isFullscreen : true } ,
155
140
} ) ;
156
- el = shallow ( < ContentIFrame { ...props } /> ) ;
157
- [ component ] = el . instance . findByType ( ModalDialog ) ;
141
+ render ( < ContentIFrame { ...props } /> ) ;
158
142
} ) ;
159
143
it ( 'displays Modal with div wrapping provided body content if modal.body is provided' , ( ) => {
160
- const content = component . findByType ( ModalDialog . Body ) [ 0 ] . children [ 0 ] ;
161
- expect ( content . matches ( shallow (
162
- < div className = "unit-modal" > { modalOptions . withBody . body } </ div > ,
163
- ) ) ) . toEqual ( true ) ;
144
+ const dialog = screen . getByRole ( 'dialog' ) ;
145
+ expect ( dialog ) . toBeInTheDocument ( ) ;
146
+ const modalBody = screen . getByText ( modalOptions . withBody . body ) ;
147
+ expect ( modalBody ) . toBeInTheDocument ( ) ;
164
148
} ) ;
165
149
testModalOpenAndHandleClose ( ) ;
166
150
} ) ;
@@ -171,55 +155,42 @@ describe('ContentIFrame Component', () => {
171
155
...modalIFrameData ,
172
156
modalOptions : { ...modalOptions . withUrl , isFullscreen : true } ,
173
157
} ) ;
174
- el = shallow ( < ContentIFrame { ...props } /> ) ;
175
- [ component ] = el . instance . findByType ( ModalDialog ) ;
158
+ render ( < ContentIFrame { ...props } /> ) ;
176
159
} ) ;
177
- testModalOpenAndHandleClose ( ) ;
178
160
it ( 'displays Modal with iframe to provided url if modal.body is not provided' , ( ) => {
179
- const content = component . findByType ( ModalDialog . Body ) [ 0 ] . children [ 0 ] ;
180
- expect ( content . matches ( shallow (
181
- < iframe
182
- title = { modalOptions . withUrl . title }
183
- allow = { IFRAME_FEATURE_POLICY }
184
- frameBorder = "0"
185
- src = { modalOptions . withUrl . url }
186
- style = { { width : '100%' , height : modalOptions . withUrl . height } }
187
- /> ,
188
- ) ) ) . toEqual ( true ) ;
161
+ const iframe = screen . getByTitle ( modalOptions . withUrl . title ) ;
162
+ expect ( iframe ) . toBeInTheDocument ( ) ;
163
+ expect ( iframe ) . toHaveAttribute ( 'allow' , IFRAME_FEATURE_POLICY ) ;
164
+ expect ( iframe ) . toHaveAttribute ( 'src' , modalOptions . withUrl . url ) ;
189
165
} ) ;
166
+ testModalOpenAndHandleClose ( ) ;
190
167
} ) ;
191
168
} ) ;
192
169
describe ( 'body modal' , ( ) => {
193
170
beforeEach ( ( ) => {
194
171
hooks . useModalIFrameData . mockReturnValueOnce ( { ...modalIFrameData , modalOptions : modalOptions . withBody } ) ;
195
- el = shallow ( < ContentIFrame { ...props } /> ) ;
196
- [ component ] = el . instance . findByType ( ModalDialog ) ;
172
+ render ( < ContentIFrame { ...props } /> ) ;
197
173
} ) ;
198
174
it ( 'displays Modal with div wrapping provided body content if modal.body is provided' , ( ) => {
199
- const content = component . findByType ( ModalDialog . Body ) [ 0 ] . children [ 0 ] ;
200
- expect ( content . matches ( shallow ( < div className = "unit-modal" > { modalOptions . withBody . body } </ div > ) ) ) . toEqual ( true ) ;
175
+ const dialog = screen . getByRole ( 'dialog' ) ;
176
+ expect ( dialog ) . toBeInTheDocument ( ) ;
177
+ const modalBody = screen . getByText ( modalOptions . withBody . body ) ;
178
+ expect ( modalBody ) . toBeInTheDocument ( ) ;
201
179
} ) ;
202
180
testModalOpenAndHandleClose ( ) ;
203
181
} ) ;
204
182
describe ( 'url modal' , ( ) => {
205
183
beforeEach ( ( ) => {
206
184
hooks . useModalIFrameData . mockReturnValueOnce ( { ...modalIFrameData , modalOptions : modalOptions . withUrl } ) ;
207
- el = shallow ( < ContentIFrame { ...props } /> ) ;
208
- [ component ] = el . instance . findByType ( ModalDialog ) ;
185
+ render ( < ContentIFrame { ...props } /> ) ;
209
186
} ) ;
210
- testModalOpenAndHandleClose ( ) ;
211
187
it ( 'displays Modal with iframe to provided url if modal.body is not provided' , ( ) => {
212
- const content = component . findByType ( ModalDialog . Body ) [ 0 ] . children [ 0 ] ;
213
- expect ( content . matches ( shallow (
214
- < iframe
215
- title = { modalOptions . withUrl . title }
216
- allow = { IFRAME_FEATURE_POLICY }
217
- frameBorder = "0"
218
- src = { modalOptions . withUrl . url }
219
- style = { { width : '100%' , height : modalOptions . withUrl . height } }
220
- /> ,
221
- ) ) ) . toEqual ( true ) ;
188
+ const iframe = screen . getByTitle ( modalOptions . withUrl . title ) ;
189
+ expect ( iframe ) . toBeInTheDocument ( ) ;
190
+ expect ( iframe ) . toHaveAttribute ( 'allow' , IFRAME_FEATURE_POLICY ) ;
191
+ expect ( iframe ) . toHaveAttribute ( 'src' , modalOptions . withUrl . url ) ;
222
192
} ) ;
193
+ testModalOpenAndHandleClose ( ) ;
223
194
} ) ;
224
195
} ) ;
225
196
} ) ;
0 commit comments