@@ -2,14 +2,15 @@ import React from 'react';
2
2
3
3
import { beforeAll , expect , jest } from '@jest/globals' ;
4
4
import { screen , waitFor } from '@testing-library/react' ;
5
+ import { AxiosError } from 'axios' ;
5
6
import { v4 as uuidv4 } from 'uuid' ;
6
7
7
8
import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock' ;
8
9
import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock' ;
9
10
import { getLayoutSetsMock } from 'src/__mocks__/getLayoutSetsMock' ;
10
11
import { DataModelFetcher } from 'src/features/formData/FormDataReaders' ;
11
12
import { Lang } from 'src/features/language/Lang' ;
12
- import { fetchApplicationMetadata , fetchInstanceData } from 'src/queries/queries' ;
13
+ import { fetchApplicationMetadata , fetchFormData , fetchInstanceData } from 'src/queries/queries' ;
13
14
import { renderWithInstanceAndLayout } from 'src/test/renderWithProviders' ;
14
15
import type { IRawTextResource } from 'src/features/language/textResources' ;
15
16
import type { IData , IDataType } from 'src/types/shared' ;
@@ -57,6 +58,20 @@ async function render(props: TestProps) {
57
58
} ) ,
58
59
) ;
59
60
jest . mocked ( fetchInstanceData ) . mockImplementationOnce ( async ( ) => instanceData ) ;
61
+ jest . mocked ( fetchFormData ) . mockImplementation ( async ( url ) => {
62
+ const path = new URL ( url ) . pathname ;
63
+ const id = path . split ( '/' ) . pop ( ) ;
64
+ const modelName = idToNameMap [ id ! ] ;
65
+ const formData = props . dataModels [ modelName ] ;
66
+ if ( formData instanceof Error ) {
67
+ return Promise . reject ( formData ) ;
68
+ }
69
+ if ( ! formData ) {
70
+ throw new Error ( `No form data mocked for testing (modelName = ${ modelName } )` ) ;
71
+ }
72
+
73
+ return formData ;
74
+ } ) ;
60
75
61
76
function generateDataElements ( instanceId : string ) : IData [ ] {
62
77
return dataModelNames . map ( ( name ) => {
@@ -124,19 +139,6 @@ async function render(props: TestProps) {
124
139
resources : props . textResources ,
125
140
language : 'nb' ,
126
141
} ) ,
127
- fetchFormData : async ( url ) => {
128
- const path = new URL ( url ) . pathname ;
129
- const id = path . split ( '/' ) . pop ( ) ;
130
- const modelName = idToNameMap [ id ! ] ;
131
- const formData = props . dataModels [ modelName ] ;
132
- if ( formData instanceof Error ) {
133
- return Promise . reject ( formData ) ;
134
- }
135
- if ( ! formData ) {
136
- throw new Error ( `No form data mocked for testing (modelName = ${ modelName } )` ) ;
137
- }
138
- return formData ;
139
- } ,
140
142
} ,
141
143
} ) ;
142
144
@@ -162,7 +164,7 @@ describe('FormDataReaders', () => {
162
164
it . each < string > ( [ 'someModel' , 'someModel1.0' ] ) (
163
165
'simple, should render a resource with a variable lookup - %s' ,
164
166
async ( modelName : string ) => {
165
- const { queries , urlFor } = await render ( {
167
+ await render ( {
166
168
ids : [ 'test' ] ,
167
169
textResources : [
168
170
{
@@ -186,9 +188,6 @@ describe('FormDataReaders', () => {
186
188
187
189
await waitFor ( ( ) => expect ( screen . getByTestId ( 'test' ) ) . toHaveTextContent ( 'Hello World' ) ) ;
188
190
189
- expect ( queries . fetchFormData ) . toHaveBeenCalledTimes ( 1 ) ;
190
- expect ( queries . fetchFormData ) . toHaveBeenCalledWith ( urlFor ( modelName ) , { } ) ;
191
-
192
191
expect ( window . logError ) . not . toHaveBeenCalled ( ) ;
193
192
expect ( window . logErrorOnce ) . not . toHaveBeenCalled ( ) ;
194
193
} ,
@@ -197,13 +196,13 @@ describe('FormDataReaders', () => {
197
196
it ( 'advanced, should fetch data from multiple models, handle failures' , async ( ) => {
198
197
jest . useFakeTimers ( ) ;
199
198
const missingError = new Error ( 'This should fail when fetching' ) ;
200
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
201
- ( missingError as any ) . isAxiosError = true ;
199
+
200
+ ( missingError as AxiosError ) . isAxiosError = true ;
202
201
203
202
const model2Promise = new Promise ( ( resolve ) => {
204
203
setTimeout ( ( ) => resolve ( { name : 'Universe' } ) , 100 ) ;
205
204
} ) ;
206
- const { queries , urlFor } = await render ( {
205
+ await render ( {
207
206
ids : [ 'test1' , 'test2' , 'test3' , 'testDefault' , 'testMissing' , 'testMissingWithDefault' ] ,
208
207
textResources : [
209
208
{
@@ -310,11 +309,6 @@ describe('FormDataReaders', () => {
310
309
await waitFor ( ( ) => expect ( screen . getByTestId ( 'test2' ) ) . toHaveTextContent ( 'Hello Universe' ) ) ;
311
310
expect ( screen . getByTestId ( 'test3' ) ) . toHaveTextContent ( 'You are [missing] year(s) old' ) ;
312
311
313
- expect ( queries . fetchFormData ) . toHaveBeenCalledTimes ( 3 ) ;
314
- expect ( queries . fetchFormData ) . toHaveBeenCalledWith ( urlFor ( 'model1' ) , { } ) ;
315
- expect ( queries . fetchFormData ) . toHaveBeenCalledWith ( urlFor ( 'model2' ) , { } ) ;
316
- expect ( queries . fetchFormData ) . toHaveBeenCalledWith ( urlFor ( 'modelMissing' ) , { } ) ;
317
-
318
312
expect ( window . logError ) . toHaveBeenCalledTimes ( 1 ) ;
319
313
expect ( window . logError ) . toHaveBeenCalledWith ( 'Fetching form data failed:\n' , missingError ) ;
320
314
0 commit comments