1
1
import { NavFormioJs } from '@navikt/skjemadigitalisering-shared-components' ;
2
- import { render , screen , waitFor , within } from '@testing-library/react' ;
2
+ import { cleanup , render , screen , waitFor , within } from '@testing-library/react' ;
3
3
import userEvent from '@testing-library/user-event' ;
4
4
import createMockImplementation , { DEFAULT_PROJECT_URL } from '../../test/backendMockImplementation' ;
5
5
import NavFormBuilder from './NavFormBuilder' ;
@@ -24,12 +24,17 @@ const DEFAULT_FORM_BUILDER_OPTIONS = {
24
24
describe ( 'NavFormBuilder' , ( ) => {
25
25
beforeAll ( ( ) => {
26
26
new NavFormioJs . Formio ( DEFAULT_PROJECT_URL ) ;
27
+ } ) ;
28
+
29
+ beforeEach ( ( ) => {
27
30
vi . spyOn ( NavFormioJs . Formio , 'fetch' ) . mockImplementation ( createMockImplementation ( ) ) ;
28
31
} ) ;
29
32
30
- afterEach ( ( ) => {
33
+ afterEach ( async ( ) => {
31
34
fetchMock . resetMocks ( ) ;
32
- window . confirm = undefined ;
35
+ vi . restoreAllMocks ( ) ;
36
+ cleanup ( ) ;
37
+ await waitFor ( ( ) => expect ( Object . keys ( NavFormioJs . Formio . forms ) . length ) . toBe ( 0 ) ) ;
33
38
} ) ;
34
39
35
40
describe ( 'mounting' , ( ) => {
@@ -78,7 +83,7 @@ describe('NavFormBuilder', () => {
78
83
79
84
describe ( 'remove button' , ( ) => {
80
85
it ( 'removes a component which no other component depends on' , async ( ) => {
81
- const checkbox = screen . queryByLabelText ( 'Oppgi din favorittfarge' ) ;
86
+ const checkbox = await screen . findByLabelText ( 'Oppgi din favorittfarge' ) ;
82
87
expect ( checkbox ) . toBeInTheDocument ( ) ;
83
88
84
89
const builderComponent = findClosestWithAttribute ( checkbox , BUILDER_COMP_TESTID_ATTR ) ;
@@ -92,7 +97,7 @@ describe('NavFormBuilder', () => {
92
97
93
98
it ( 'prompts user when removing a component which other components depends on' , async ( ) => {
94
99
window . confirm = vi . fn ( ) . mockImplementation ( ( ) => true ) ;
95
- const fieldset = screen . queryByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
100
+ const fieldset = await screen . findByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
96
101
expect ( fieldset ) . toBeInTheDocument ( ) ;
97
102
98
103
const builderComponent = findClosestWithAttribute ( fieldset , BUILDER_COMP_TESTID_ATTR ) ;
@@ -106,15 +111,15 @@ describe('NavFormBuilder', () => {
106
111
107
112
it ( 'does not remove component if user declines prompt' , async ( ) => {
108
113
window . confirm = vi . fn ( ) . mockImplementation ( ( ) => false ) ;
109
- const fieldset = screen . queryByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
114
+ const fieldset = await screen . findByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
110
115
expect ( fieldset ) . toBeInTheDocument ( ) ;
111
116
112
117
const builderComponent = findClosestWithAttribute ( fieldset , BUILDER_COMP_TESTID_ATTR ) ;
113
118
114
119
const removeComponentButton = await within ( builderComponent ) . findByTitle ( 'Slett' ) ;
115
120
await userEvent . click ( removeComponentButton ) ;
116
121
117
- expect ( screen . queryByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ) . toBeInTheDocument ( ) ;
122
+ expect ( await screen . findByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ) . toBeInTheDocument ( ) ;
118
123
expect ( onChangeMock . mock . calls ) . toHaveLength ( 0 ) ;
119
124
120
125
expect ( window . confirm . mock . calls ) . toHaveLength ( 1 ) ;
@@ -125,7 +130,7 @@ describe('NavFormBuilder', () => {
125
130
126
131
it ( 'prompts user when removing component containing component which other components depends on' , async ( ) => {
127
132
window . confirm = vi . fn ( ) . mockImplementation ( ( ) => true ) ;
128
- const panel = screen . queryByText ( 'Tilbakemelding' ) ;
133
+ const panel = await screen . findByText ( 'Tilbakemelding' ) ;
129
134
expect ( panel ) . toBeInTheDocument ( ) ;
130
135
131
136
const builderComponent = findClosestWithAttribute ( panel , BUILDER_COMP_TESTID_ATTR ) ;
@@ -147,6 +152,7 @@ describe('NavFormBuilder', () => {
147
152
148
153
it ( 'prompts user when removing attachment panel' , async ( ) => {
149
154
window . confirm = vi . fn ( ) . mockImplementation ( ( ) => true ) ;
155
+ onReadyMock . mockReset ( ) ;
150
156
rerender (
151
157
< NavFormBuilder
152
158
form = { { ...testform , display : 'skjema' } }
@@ -155,12 +161,9 @@ describe('NavFormBuilder', () => {
155
161
formBuilderOptions = { DEFAULT_FORM_BUILDER_OPTIONS }
156
162
/> ,
157
163
) ;
164
+ await waitFor ( ( ) => expect ( onReadyMock . mock . calls ) . toHaveLength ( 1 ) ) ;
158
165
159
- const attachmentPanelLink = screen . queryByRole ( 'link' , { name : 'Vedlegg' } ) ;
160
- expect ( attachmentPanelLink ) . toBeInTheDocument ( ) ;
161
- await userEvent . click ( attachmentPanelLink ) ;
162
-
163
- const attachmentPanel = screen . queryByRole ( 'button' , { name : 'Vedlegg' } ) ;
166
+ const attachmentPanel = await screen . findByRole ( 'button' , { name : 'Vedlegg' } ) ;
164
167
expect ( attachmentPanel ) . toBeInTheDocument ( ) ;
165
168
const builderComponent = findClosestWithAttribute ( attachmentPanel , BUILDER_COMP_TESTID_ATTR ) ;
166
169
const removeComponentButtons = await within ( builderComponent ) . findAllByTitle ( 'Slett' ) ;
@@ -177,6 +180,7 @@ describe('NavFormBuilder', () => {
177
180
178
181
it ( 'does not remove attachment panel if user declines prompt' , async ( ) => {
179
182
window . confirm = vi . fn ( ) . mockImplementation ( ( ) => false ) ;
183
+ onReadyMock . mockReset ( ) ;
180
184
rerender (
181
185
< NavFormBuilder
182
186
form = { { ...testform , display : 'skjema' } }
@@ -185,12 +189,9 @@ describe('NavFormBuilder', () => {
185
189
formBuilderOptions = { DEFAULT_FORM_BUILDER_OPTIONS }
186
190
/> ,
187
191
) ;
192
+ await waitFor ( ( ) => expect ( onReadyMock . mock . calls ) . toHaveLength ( 1 ) ) ;
188
193
189
- const attachmentPanelLink = screen . queryByRole ( 'link' , { name : 'Vedlegg' } ) ;
190
- expect ( attachmentPanelLink ) . toBeInTheDocument ( ) ;
191
- await userEvent . click ( attachmentPanelLink ) ;
192
-
193
- const attachmentPanel = screen . queryByRole ( 'button' , { name : 'Vedlegg' } ) ;
194
+ const attachmentPanel = await screen . findByRole ( 'button' , { name : 'Vedlegg' } ) ;
194
195
expect ( attachmentPanel ) . toBeInTheDocument ( ) ;
195
196
const builderComponent = findClosestWithAttribute ( attachmentPanel , BUILDER_COMP_TESTID_ATTR ) ;
196
197
const removeComponentButtons = await within ( builderComponent ) . findAllByTitle ( 'Slett' ) ;
@@ -201,22 +202,22 @@ describe('NavFormBuilder', () => {
201
202
'Du forsøker nå å slette vedleggspanelet. For å gjenopprette må et predefinert vedleggspanel trekkes inn. Vil du fremdeles slette panelet?' ,
202
203
) ;
203
204
204
- const attachmentPanelAfterNoDelete = screen . queryByRole ( 'button' , { name : 'Vedlegg' } ) ;
205
+ const attachmentPanelAfterNoDelete = await screen . findByRole ( 'button' , { name : 'Vedlegg' } ) ;
205
206
expect ( attachmentPanelAfterNoDelete ) . toBeInTheDocument ( ) ;
206
207
} ) ;
207
208
} ) ;
208
209
209
210
describe ( 'conditional alert message in edit component' , ( ) => {
210
211
it ( 'is visible when component is depended upon by other components' , async ( ) => {
211
- const fieldset = screen . queryByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
212
+ const fieldset = await screen . findByRole ( 'group' , { name : / Y o u r f a v o r i t e t i m e o f t h e y e a r / } ) ;
212
213
expect ( fieldset ) . toBeInTheDocument ( ) ;
213
214
214
215
const builderComponent = findClosestWithAttribute ( fieldset , BUILDER_COMP_TESTID_ATTR ) ;
215
216
216
217
const editComponentButton = await within ( builderComponent ) . findByTitle ( 'Rediger' ) ;
217
218
await userEvent . click ( editComponentButton ) ;
218
219
219
- const conditionalAlert = screen . queryByRole ( 'list' , {
220
+ const conditionalAlert = await screen . findByRole ( 'list' , {
220
221
name : 'Følgende komponenter har avhengighet til denne:' ,
221
222
} ) ;
222
223
expect ( conditionalAlert ) . toBeInTheDocument ( ) ;
@@ -225,7 +226,7 @@ describe('NavFormBuilder', () => {
225
226
} ) ;
226
227
227
228
it ( 'is not visible when component is not depended upon by other components' , async ( ) => {
228
- const textInput = screen . queryByLabelText ( 'Oppgi din favorittfarge' ) ;
229
+ const textInput = await screen . findByLabelText ( 'Oppgi din favorittfarge' ) ;
229
230
expect ( textInput ) . toBeInTheDocument ( ) ;
230
231
231
232
const builderComponent = findClosestWithAttribute ( textInput , BUILDER_COMP_TESTID_ATTR ) ;
0 commit comments