@@ -2,14 +2,15 @@ import React from 'react';
22
33import { beforeAll , expect , jest } from '@jest/globals' ;
44import { screen , waitFor } from '@testing-library/react' ;
5+ import { AxiosError } from 'axios' ;
56import { v4 as uuidv4 } from 'uuid' ;
67
78import { getIncomingApplicationMetadataMock } from 'src/__mocks__/getApplicationMetadataMock' ;
89import { getInstanceDataMock } from 'src/__mocks__/getInstanceDataMock' ;
910import { getLayoutSetsMock } from 'src/__mocks__/getLayoutSetsMock' ;
1011import { DataModelFetcher } from 'src/features/formData/FormDataReaders' ;
1112import { Lang } from 'src/features/language/Lang' ;
12- import { fetchApplicationMetadata , fetchInstanceData } from 'src/queries/queries' ;
13+ import { fetchApplicationMetadata , fetchFormData , fetchInstanceData } from 'src/queries/queries' ;
1314import { renderWithInstanceAndLayout } from 'src/test/renderWithProviders' ;
1415import type { IRawTextResource } from 'src/features/language/textResources' ;
1516import type { IData , IDataType } from 'src/types/shared' ;
@@ -57,6 +58,20 @@ async function render(props: TestProps) {
5758 } ) ,
5859 ) ;
5960 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+ } ) ;
6075
6176 function generateDataElements ( instanceId : string ) : IData [ ] {
6277 return dataModelNames . map ( ( name ) => {
@@ -124,19 +139,6 @@ async function render(props: TestProps) {
124139 resources : props . textResources ,
125140 language : 'nb' ,
126141 } ) ,
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- } ,
140142 } ,
141143 } ) ;
142144
@@ -162,7 +164,7 @@ describe('FormDataReaders', () => {
162164 it . each < string > ( [ 'someModel' , 'someModel1.0' ] ) (
163165 'simple, should render a resource with a variable lookup - %s' ,
164166 async ( modelName : string ) => {
165- const { queries , urlFor } = await render ( {
167+ await render ( {
166168 ids : [ 'test' ] ,
167169 textResources : [
168170 {
@@ -186,9 +188,6 @@ describe('FormDataReaders', () => {
186188
187189 await waitFor ( ( ) => expect ( screen . getByTestId ( 'test' ) ) . toHaveTextContent ( 'Hello World' ) ) ;
188190
189- expect ( queries . fetchFormData ) . toHaveBeenCalledTimes ( 1 ) ;
190- expect ( queries . fetchFormData ) . toHaveBeenCalledWith ( urlFor ( modelName ) , { } ) ;
191-
192191 expect ( window . logError ) . not . toHaveBeenCalled ( ) ;
193192 expect ( window . logErrorOnce ) . not . toHaveBeenCalled ( ) ;
194193 } ,
@@ -197,13 +196,13 @@ describe('FormDataReaders', () => {
197196 it ( 'advanced, should fetch data from multiple models, handle failures' , async ( ) => {
198197 jest . useFakeTimers ( ) ;
199198 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 ;
202201
203202 const model2Promise = new Promise ( ( resolve ) => {
204203 setTimeout ( ( ) => resolve ( { name : 'Universe' } ) , 100 ) ;
205204 } ) ;
206- const { queries , urlFor } = await render ( {
205+ await render ( {
207206 ids : [ 'test1' , 'test2' , 'test3' , 'testDefault' , 'testMissing' , 'testMissingWithDefault' ] ,
208207 textResources : [
209208 {
@@ -310,11 +309,6 @@ describe('FormDataReaders', () => {
310309 await waitFor ( ( ) => expect ( screen . getByTestId ( 'test2' ) ) . toHaveTextContent ( 'Hello Universe' ) ) ;
311310 expect ( screen . getByTestId ( 'test3' ) ) . toHaveTextContent ( 'You are [missing] year(s) old' ) ;
312311
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-
318312 expect ( window . logError ) . toHaveBeenCalledTimes ( 1 ) ;
319313 expect ( window . logError ) . toHaveBeenCalledWith ( 'Fetching form data failed:\n' , missingError ) ;
320314
0 commit comments