@@ -13,6 +13,7 @@ import {
13
13
} from '../../../../test-data/codeListDataList' ;
14
14
import { ArrayUtils } from '@studio/pure-functions' ;
15
15
import { label1ResourceNb , textResources } from '../../../../test-data/textResources' ;
16
+ import type { TextResource } from '../../../../types/TextResource' ;
16
17
import type { TextResourceWithLanguage } from '../../../../types/TextResourceWithLanguage' ;
17
18
18
19
const onDeleteCodeList = jest . fn ( ) ;
@@ -182,6 +183,41 @@ describe('CodeListPage', () => {
182
183
expect ( onUpdateTextResource ) . toHaveBeenCalledTimes ( newLabel . length ) ;
183
184
expect ( onUpdateTextResource ) . toHaveBeenLastCalledWith ( expectedObject ) ;
184
185
} ) ;
186
+
187
+ it ( 'Renders with text resources in the input fields of the create dialog when given' , async ( ) => {
188
+ const user = userEvent . setup ( ) ;
189
+
190
+ renderCodeListPage ( { textResources } ) ;
191
+ const dialog = await openCreateDialog ( user ) ;
192
+ await addCodeListItem ( user , dialog ) ;
193
+ await openSearchModeForFirstLabel ( user , dialog ) ;
194
+ await openFirstLabelCombobox ( user , dialog ) ;
195
+
196
+ expect ( getTextResourceOption ( label1ResourceNb ) ) . toBeInTheDocument ( ) ;
197
+ } ) ;
198
+
199
+ it ( 'Calls onUpdateTextResource with the new text resource and the default language when a text resource is changed in the create dialog' , async ( ) => {
200
+ const user = userEvent . setup ( ) ;
201
+ const onUpdateTextResource = jest . fn ( ) ;
202
+ const newLabel = 'Ny ledetekst' ;
203
+
204
+ renderCodeListPage ( { textResources, onUpdateTextResource } ) ;
205
+ const dialog = await openCreateDialog ( user ) ;
206
+ await addCodeListItem ( user , dialog ) ;
207
+ await openSearchModeForFirstLabel ( user , dialog ) ;
208
+ await openFirstLabelCombobox ( user , dialog ) ;
209
+ await user . click ( getTextResourceOption ( label1ResourceNb ) ) ;
210
+ await openEditModeForFirstLabel ( user , dialog ) ;
211
+ await user . type ( getFirstLabelField ( dialog ) , newLabel ) ;
212
+
213
+ const expectedLanguage = 'nb' ;
214
+ const expectedObject : TextResourceWithLanguage = {
215
+ language : expectedLanguage ,
216
+ textResource : { ...label1ResourceNb , value : newLabel } ,
217
+ } ;
218
+ expect ( onUpdateTextResource ) . toHaveBeenCalledTimes ( newLabel . length ) ;
219
+ expect ( onUpdateTextResource ) . toHaveBeenLastCalledWith ( expectedObject ) ;
220
+ } ) ;
185
221
} ) ;
186
222
187
223
const uploadCodeList = async ( user : UserEvent , fileName : string ) : Promise < void > => {
@@ -198,8 +234,12 @@ const openAndGetFirstLabelField = async (
198
234
) : Promise < HTMLElement > => {
199
235
await user . click ( getCodeListHeading ( codeListTitle ) ) ;
200
236
const accordion = getCodeListAccordion ( codeListTitle ) ;
237
+ return getFirstLabelField ( accordion ) ;
238
+ } ;
239
+
240
+ const getFirstLabelField = ( area : HTMLElement ) : HTMLElement => {
201
241
const labelFieldLabel = textMock ( 'code_list_editor.text_resource.label.value' , { number : 1 } ) ;
202
- return within ( accordion ) . getByRole ( 'textbox' , { name : labelFieldLabel } ) ;
242
+ return within ( area ) . getByRole ( 'textbox' , { name : labelFieldLabel } ) ;
203
243
} ;
204
244
205
245
const getCodeListAccordion = ( codeListTitle : string ) : HTMLElement =>
@@ -215,3 +255,37 @@ const queryCodeListHeading = (codeListTitle: string): HTMLElement =>
215
255
216
256
const renderCodeListPage = ( props : Partial < CodeListPageProps > = { } ) : RenderResult =>
217
257
render ( < CodeListPage { ...defaultCodeListPageProps } { ...props } /> ) ;
258
+
259
+ const openCreateDialog = async ( user : UserEvent ) : Promise < HTMLElement > => {
260
+ const createButtonLabel = textMock ( 'app_content_library.code_lists.create_new_code_list' ) ;
261
+ await user . click ( screen . getByRole ( 'button' , { name : createButtonLabel } ) ) ;
262
+ return screen . getByRole ( 'dialog' ) ;
263
+ } ;
264
+
265
+ const addCodeListItem = async ( user : UserEvent , area : HTMLElement ) : Promise < void > => {
266
+ const addButtonLabel = textMock ( 'code_list_editor.add_option' ) ;
267
+ await user . click ( within ( area ) . getByRole ( 'button' , { name : addButtonLabel } ) ) ;
268
+ } ;
269
+
270
+ const openSearchModeForFirstLabel = async ( user : UserEvent , area : HTMLElement ) : Promise < void > => {
271
+ const radioLabel = textMock ( 'code_list_editor.text_resource.label.search_mode' , { number : 1 } ) ;
272
+ const radio = within ( area ) . getByRole ( 'radio' , { name : radioLabel } ) ;
273
+ await user . click ( radio ) ;
274
+ } ;
275
+
276
+ const openEditModeForFirstLabel = async ( user : UserEvent , area : HTMLElement ) : Promise < void > => {
277
+ const radioLabel = textMock ( 'code_list_editor.text_resource.label.edit_mode' , { number : 1 } ) ;
278
+ const radio = await within ( area ) . findByRole ( 'radio' , { name : radioLabel } ) ;
279
+ await user . click ( radio ) ;
280
+ } ;
281
+
282
+ const openFirstLabelCombobox = async ( user : UserEvent , area : HTMLElement ) : Promise < void > => {
283
+ const comboboxLabel = textMock ( 'code_list_editor.text_resource.label.select' , { number : 1 } ) ;
284
+ const combobox = within ( area ) . getByRole ( 'combobox' , { name : comboboxLabel } ) ;
285
+ await user . click ( combobox ) ;
286
+ } ;
287
+
288
+ const getTextResourceOption = ( textResource : TextResource ) : HTMLElement =>
289
+ screen . getByRole ( 'option' , { name : retrieveOptionName ( textResource ) } ) ;
290
+
291
+ const retrieveOptionName = ( { value, id } : TextResource ) : string => `${ value } ${ id } ` ;
0 commit comments