1
- import { render , screen } from '@testing-library/react' ;
2
- import { routerRoutes } from './routes' ;
1
+ import { render , screen , waitFor } from '@testing-library/react' ;
2
+ import { routerRoutes , UiEditor } from './routes' ;
3
3
import { RoutePaths } from '../enums/RoutePaths' ;
4
4
import React from 'react' ;
5
5
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock' ;
@@ -13,6 +13,9 @@ import { PreviewContextProvider } from '../contexts/PreviewContext';
13
13
import { AppDevelopmentContextProvider } from '../contexts/AppDevelopmentContext' ;
14
14
import { textMock } from '@studio/testing/mocks/i18nMock' ;
15
15
import type { QueryClient } from '@tanstack/react-query' ;
16
+ import { LayoutContext } from 'app-development/contexts/LayoutContext/LayoutContext' ;
17
+ import { SubApp as UiEditorLatest } from '@altinn/ux-editor/SubApp' ;
18
+ import { FeatureFlag , shouldDisplayFeature } from 'app-shared/utils/featureToggleUtils' ;
16
19
17
20
// Mocks:
18
21
jest . mock ( '@altinn/ux-editor-v3/SubApp' , ( ) => ( {
@@ -22,8 +25,79 @@ jest.mock('@altinn/ux-editor/SubApp', () => ({
22
25
SubApp : ( ) => < div data-testid = 'latest version' /> ,
23
26
} ) ) ;
24
27
28
+ jest . mock ( '@altinn/ux-editor/SubApp' , ( ) => ( {
29
+ SubApp : ( { onLayoutSetNameChange } : { onLayoutSetNameChange : ( name : string ) => void } ) => {
30
+ React . useEffect ( ( ) => {
31
+ onLayoutSetNameChange ( 'test-layout' ) ;
32
+ } , [ onLayoutSetNameChange ] ) ;
33
+ return < div data-testid = 'latest version' /> ;
34
+ } ,
35
+ } ) ) ;
36
+
37
+ jest . mock ( '@altinn/ux-editor/containers/FormDesignNavigation' , ( ) => ( {
38
+ FormDesignerNavigation : ( ) => < div > Form Designer Navigation</ div > ,
39
+ } ) ) ;
40
+
41
+ jest . mock ( 'app-shared/utils/featureToggleUtils' , ( ) => ( {
42
+ ...jest . requireActual ( 'app-shared/utils/featureToggleUtils' ) ,
43
+ shouldDisplayFeature : jest . fn ( ) ,
44
+ } ) ) ;
45
+
46
+ const renderWithProviders = (
47
+ ui : React . ReactElement ,
48
+ queryClient : QueryClient ,
49
+ layoutContextValue ?: any ,
50
+ ) => {
51
+ return render (
52
+ < ServicesContextProvider { ...queriesMock } client = { queryClient } >
53
+ < SettingsModalContextProvider >
54
+ < PreviewContextProvider >
55
+ < AppDevelopmentContextProvider >
56
+ < LayoutContext . Provider value = { layoutContextValue } > { ui } </ LayoutContext . Provider >
57
+ </ AppDevelopmentContextProvider >
58
+ </ PreviewContextProvider >
59
+ </ SettingsModalContextProvider >
60
+ </ ServicesContextProvider > ,
61
+ ) ;
62
+ } ;
63
+
25
64
describe ( 'routes' , ( ) => {
26
65
describe ( RoutePaths . UIEditor , ( ) => {
66
+ afterEach ( ( ) => {
67
+ jest . restoreAllMocks ( ) ;
68
+ } ) ;
69
+ it ( 'calls setSelectedLayoutSetName when onLayoutSetNameChange is triggered' , ( ) => {
70
+ const setSelectedLayoutSetName = jest . fn ( ) ;
71
+ const queryClient = createQueryClientMock ( ) ;
72
+ const appVersion : AppVersion = {
73
+ frontendVersion : '4.0.0' ,
74
+ backendVersion : '7.0.0' ,
75
+ } ;
76
+ queryClient . setQueryData ( [ QueryKey . AppVersion , org , app ] , appVersion ) ;
77
+ const { rerender } = renderWithProviders ( < UiEditor /> , queryClient , {
78
+ setSelectedLayoutSetName,
79
+ } ) ;
80
+ const layoutSetName = 'test-layout' ;
81
+ const onLayoutSetNameChange = jest . fn ( ) ;
82
+ rerender (
83
+ < UiEditorLatest
84
+ shouldReloadPreview = { false }
85
+ previewHasLoaded = { undefined }
86
+ onLayoutSetNameChange = { onLayoutSetNameChange }
87
+ /> ,
88
+ ) ;
89
+ onLayoutSetNameChange ( layoutSetName ) ;
90
+ expect ( setSelectedLayoutSetName ) . toHaveBeenCalledWith ( layoutSetName ) ;
91
+ } ) ;
92
+
93
+ it ( 'Returns null when there is no AppVersion' , async ( ) => {
94
+ const queryClient = createQueryClientMock ( ) ;
95
+ queryClient . setQueryData ( [ QueryKey . AppVersion , org , app ] , null ) ;
96
+ renderUiEditor ( queryClient ) ;
97
+ expect ( screen . queryByTestId ( 'version 3' ) ) . not . toBeInTheDocument ( ) ;
98
+ expect ( screen . queryByTestId ( 'latest version' ) ) . not . toBeInTheDocument ( ) ;
99
+ } ) ;
100
+
27
101
type FrontendVersion = null | '3.0.0' | '4.0.0' ;
28
102
type PackageVersion = 'version 3' | 'latest version' ;
29
103
type TestCase = [ PackageVersion , FrontendVersion ] ;
@@ -36,23 +110,47 @@ describe('routes', () => {
36
110
37
111
it . each ( testCases ) (
38
112
'Renders the %s schema editor page when the app frontend version is %s' ,
39
- ( expectedPackage , frontendVersion ) => {
113
+ async ( expectedPackage , frontendVersion ) => {
40
114
const appVersion : AppVersion = {
41
115
frontendVersion,
42
116
backendVersion : '7.0.0' ,
43
117
} ;
44
118
const queryClient = createQueryClientMock ( ) ;
45
119
queryClient . setQueryData ( [ QueryKey . AppVersion , org , app ] , appVersion ) ;
46
120
renderUiEditor ( queryClient ) ;
47
- expect ( screen . getByTestId ( expectedPackage ) ) . toBeInTheDocument ( ) ;
121
+ expect ( await screen . findByTestId ( expectedPackage ) ) . toBeInTheDocument ( ) ;
48
122
} ,
49
123
) ;
50
124
125
+ it ( 'renders a loading spinner' , async ( ) => {
126
+ renderUiEditor ( ) ;
127
+ await waitFor ( ( ) => {
128
+ expect ( screen . getByTestId ( 'studio-spinner-test-id' ) ) . toBeInTheDocument ( ) ;
129
+ } ) ;
130
+ } ) ;
131
+
51
132
it ( 'renders a loading spinner while fetching frontend version' , ( ) => {
52
133
renderUiEditor ( ) ;
53
134
expect ( screen . getByText ( textMock ( 'ux_editor.loading_page' ) ) ) . toBeInTheDocument ( ) ;
54
135
} ) ;
55
136
137
+ it ( 'renders FormDesignerNavigation when task navigation is enabled and no layout set is selected' , ( ) => {
138
+ ( shouldDisplayFeature as jest . Mock ) . mockImplementation (
139
+ ( feature ) => feature === FeatureFlag . TaskNavigation ,
140
+ ) ;
141
+ const queryClient = createQueryClientMock ( ) ;
142
+ queryClient . setQueryData ( [ QueryKey . AppVersion , org , app ] , {
143
+ frontendVersion : '4.0.0' ,
144
+ backendVersion : '7.0.0' ,
145
+ } ) ;
146
+
147
+ renderWithProviders ( < UiEditor /> , queryClient , {
148
+ setSelectedLayoutSetName : jest . fn ( ) ,
149
+ selectedFormLayoutSetName : undefined ,
150
+ } ) ;
151
+ expect ( screen . getByText ( 'Form Designer Navigation' ) ) . toBeInTheDocument ( ) ;
152
+ } ) ;
153
+
56
154
const renderUiEditor = ( queryClient : QueryClient = createQueryClientMock ( ) ) =>
57
155
renderSubapp ( RoutePaths . UIEditor , queryClient ) ;
58
156
} ) ;
0 commit comments