Skip to content

Commit 71ec417

Browse files
committed
refactor: Clean up option list typing
1 parent 15bd10a commit 71ec417

24 files changed

+117
-118
lines changed

frontend/app-development/features/appContentLibrary/AppContentLibrary.test.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import type { UserEvent } from '@testing-library/user-event';
1111
import userEvent from '@testing-library/user-event';
1212
import type { CodeList } from '@studio/components';
1313
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
14-
import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
14+
import type { OptionListData } from 'app-shared/types/OptionList';
1515

1616
const uploadCodeListButtonTextMock = 'Upload Code List';
1717
const updateCodeListButtonTextMock = 'Update Code List';
1818
const updateCodeListIdButtonTextMock = 'Update Code List Id';
1919
const codeListNameMock = 'codeListNameMock';
2020
const newCodeListNameMock = 'newCodeListNameMock';
2121
const codeListMock: CodeList = [{ value: '', label: '' }];
22-
const optionListsDataMock: OptionsListsResponse = [{ title: codeListNameMock, data: codeListMock }];
22+
const optionListsDataMock: OptionListData[] = [{ title: codeListNameMock, data: codeListMock }];
2323
jest.mock(
2424
'../../../libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage',
2525
() => ({
@@ -147,7 +147,7 @@ const goToLibraryPage = async (user: UserEvent, libraryPage: string) => {
147147
type renderAppContentLibraryProps = {
148148
queries?: Partial<ServicesContextProps>;
149149
shouldPutDataOnCache?: boolean;
150-
optionListsData?: OptionsListsResponse;
150+
optionListsData?: OptionListData[];
151151
};
152152

153153
const renderAppContentLibrary = ({

frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { CodeListData } from '@studio/content-library';
22
import { convertOptionsListsDataToCodeListsData } from './convertOptionsListsDataToCodeListsData';
3-
import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
3+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
44

55
describe('convertOptionsListsDataToCodeListsData', () => {
66
it('converts option lists data to code lists data correctly', () => {
77
const optionListId: string = 'optionListId';
8-
const optionListsData: OptionsListsResponse = [
8+
const optionListsData: OptionListsResponse = [
99
{
1010
title: optionListId,
1111
data: [
@@ -30,7 +30,7 @@ describe('convertOptionsListsDataToCodeListsData', () => {
3030

3131
it('sets hasError to true in result when optionListsResponse returns an option list with error', () => {
3232
const optionListId: string = 'optionListId';
33-
const optionListsData: OptionsListsResponse = [
33+
const optionListsData: OptionListsResponse = [
3434
{
3535
title: optionListId,
3636
data: null,
@@ -42,7 +42,7 @@ describe('convertOptionsListsDataToCodeListsData', () => {
4242
});
4343

4444
it('returns a result with empty code list data array when the input option list data is empty', () => {
45-
const optionListsData: OptionsListsResponse = [];
45+
const optionListsData: OptionListsResponse = [];
4646
const result: CodeListData[] = convertOptionsListsDataToCodeListsData(optionListsData);
4747
expect(result).toEqual([]);
4848
});

frontend/app-development/features/appContentLibrary/utils/convertOptionsListsDataToCodeListsData.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { OptionsListData, OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
1+
import type { OptionListData } from 'app-shared/types/OptionList';
2+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
23
import type { CodeListData } from '@studio/content-library';
34

4-
export const convertOptionsListsDataToCodeListsData = (optionListsData: OptionsListsResponse) => {
5+
export const convertOptionsListsDataToCodeListsData = (optionListsData: OptionListsResponse) => {
56
const codeListsData = [];
67
optionListsData.map((optionListData) => {
78
const codeListData = convertOptionsListDataToCodeListData(optionListData);
@@ -10,7 +11,7 @@ export const convertOptionsListsDataToCodeListsData = (optionListsData: OptionsL
1011
return codeListsData;
1112
};
1213

13-
const convertOptionsListDataToCodeListData = (optionListData: OptionsListData) => {
14+
const convertOptionsListDataToCodeListData = (optionListData: OptionListData) => {
1415
const codeListData: CodeListData = {
1516
title: optionListData.title,
1617
data: optionListData.data,

frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { CodeListIdSource } from '@studio/content-library';
22
import { mapToCodeListsUsage } from './mapToCodeListsUsage';
3-
import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists';
3+
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
44

55
const optionListId: string = 'optionListId';
66
const optionListIdSources: CodeListIdSource[] = [
@@ -10,7 +10,7 @@ const optionListIdSources: CodeListIdSource[] = [
1010
componentIds: ['componentId1', 'componentId2'],
1111
},
1212
];
13-
const optionListsUsages: OptionListsReferences = [
13+
const optionListsUsages: OptionListReferences = [
1414
{
1515
optionListId,
1616
optionListIdSources,

frontend/app-development/features/appContentLibrary/utils/mapToCodeListsUsage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists';
1+
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
22
import type { CodeListReference } from '@studio/content-library';
33

44
type MapToCodeListsUsageProps = {
5-
optionListsUsages: OptionListsReferences;
5+
optionListsUsages: OptionListReferences;
66
};
77

88
export const mapToCodeListsUsage = ({

frontend/packages/shared/src/api/queries.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ import type { Policy } from 'app-shared/types/Policy';
9191
import type { RepoDiffResponse } from 'app-shared/types/api/RepoDiffResponse';
9292
import type { ExternalImageUrlValidationResponse } from 'app-shared/types/api/ExternalImageUrlValidationResponse';
9393
import type { MaskinportenScopes } from 'app-shared/types/MaskinportenScope';
94-
import type { OptionListsReferences, OptionsList, OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
94+
import type { OptionList } from 'app-shared/types/OptionList';
95+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
96+
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
9597
import type { LayoutSetsModel } from '../types/api/dto/LayoutSetsModel';
9698
import type { AccessPackageResource, PolicyAccessPackageAreaGroup } from 'app-shared/types/PolicyAccessPackages';
9799
import type { DataType } from '../types/DataType';
@@ -120,9 +122,9 @@ export const getImageFileNames = (owner: string, app: string) => get<string[]>(g
120122
export const getLayoutNames = (owner: string, app: string) => get<string[]>(layoutNamesPath(owner, app));
121123
export const getLayoutSets = (owner: string, app: string) => get<LayoutSets>(layoutSetsPath(owner, app));
122124
export const getLayoutSetsExtended = (owner: string, app: string) => get<LayoutSetsModel>(layoutSetsPath(owner, app) + '/extended');
123-
export const getOptionList = (owner: string, app: string, optionsListId: string) => get<OptionsList>(optionListPath(owner, app, optionsListId));
124-
export const getOptionLists = (owner: string, app: string) => get<OptionsListsResponse>(optionListsPath(owner, app));
125-
export const getOptionListsReferences = (owner: string, app: string) => get<OptionListsReferences>(optionListReferencesPath(owner, app));
125+
export const getOptionList = (owner: string, app: string, optionsListId: string) => get<OptionList>(optionListPath(owner, app, optionsListId));
126+
export const getOptionLists = (owner: string, app: string) => get<OptionListsResponse>(optionListsPath(owner, app));
127+
export const getOptionListsReferences = (owner: string, app: string) => get<OptionListReferences>(optionListReferencesPath(owner, app));
126128
export const getOptionListIds = (owner: string, app: string) => get<string[]>(optionListIdsPath(owner, app));
127129
export const getOrgList = () => get<OrgList>(orgListUrl());
128130
export const getOrganizations = () => get<Organization[]>(orgsListPath());

frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useUpdateOptionListIdMutation } from './useUpdateOptionListIdMutation';
66
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
77
import { QueryKey } from 'app-shared/types/QueryKey';
88
import type { Option } from 'app-shared/types/Option';
9-
import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
9+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
1010

1111
// Test data:
1212
const optionListId: string = 'optionListId';
@@ -38,7 +38,7 @@ describe('useUpdateOptionListIdMutation', () => {
3838
const optionListC = 'optionListC';
3939
const optionListZ = 'optionListZ';
4040
const queryClient = createQueryClientMock();
41-
const oldData: OptionsListsResponse = [
41+
const oldData: OptionListsResponse = [
4242
{ title: optionListA, data: optionListMock },
4343
{ title: optionListB, data: optionListMock },
4444
{ title: optionListZ, data: optionListMock },
@@ -52,7 +52,7 @@ describe('useUpdateOptionListIdMutation', () => {
5252
optionListId: optionListA,
5353
newOptionListId: optionListC,
5454
});
55-
const cacheData: OptionsListsResponse = queryClient.getQueryData([
55+
const cacheData: OptionListsResponse = queryClient.getQueryData([
5656
QueryKey.OptionLists,
5757
org,
5858
app,
@@ -65,7 +65,7 @@ describe('useUpdateOptionListIdMutation', () => {
6565
test('Invalidates the optionListIds query cache', async () => {
6666
const queryClient = createQueryClientMock();
6767
const invalidateQueriesSpy = jest.spyOn(queryClient, 'invalidateQueries');
68-
const oldData: OptionsListsResponse = [
68+
const oldData: OptionListsResponse = [
6969
{ title: 'firstOptionList', data: optionListMock },
7070
{ title: 'optionListId', data: optionListMock },
7171
{ title: 'lastOptionList', data: optionListMock },

frontend/packages/shared/src/hooks/mutations/useUpdateOptionListIdMutation.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { QueryKey } from 'app-shared/types/QueryKey';
2-
import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
2+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
33
import { useQueryClient, useMutation } from '@tanstack/react-query';
44
import { useServicesContext } from 'app-shared/contexts/ServicesContext';
55
import { ArrayUtils } from '@studio/pure-functions';
@@ -21,7 +21,7 @@ export const useUpdateOptionListIdMutation = (org: string, app: string) => {
2121
}));
2222
},
2323
onSuccess: ({ optionListId, newOptionListId }) => {
24-
const oldData: OptionsListsResponse = queryClient.getQueryData([
24+
const oldData: OptionListsResponse = queryClient.getQueryData([
2525
QueryKey.OptionLists,
2626
org,
2727
app,
@@ -38,10 +38,10 @@ export const useUpdateOptionListIdMutation = (org: string, app: string) => {
3838
const changeIdAndSortCacheData = (
3939
oldId: string,
4040
newId: string,
41-
oldData: OptionsListsResponse,
42-
): OptionsListsResponse => {
41+
oldData: OptionListsResponse,
42+
): OptionListsResponse => {
4343
const oldOptionList = oldData.find((optionList) => optionList.title === oldId);
44-
const newOptionLists: OptionsListsResponse = ArrayUtils.replaceByPredicate(
44+
const newOptionLists: OptionListsResponse = ArrayUtils.replaceByPredicate(
4545
oldData,
4646
(optionList) => optionList.title === oldId,
4747
{ ...oldOptionList, title: newId },

frontend/packages/shared/src/hooks/mutations/useUpdateOptionListMutation.ts

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import type { MutationMeta } from '@tanstack/react-query';
22
import { QueryKey } from 'app-shared/types/QueryKey';
33
import type { Option } from 'app-shared/types/Option';
4-
import type {
5-
OptionsList,
6-
OptionsListData,
7-
OptionsListsResponse,
8-
} from 'app-shared/types/api/OptionsLists';
4+
import type { OptionList, OptionListData } from 'app-shared/types/OptionList';
5+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
96
import { useQueryClient, useMutation } from '@tanstack/react-query';
107
import { useServicesContext } from 'app-shared/contexts/ServicesContext';
118
import { ArrayUtils } from '@studio/pure-functions';
@@ -24,7 +21,7 @@ export const useUpdateOptionListMutation = (org: string, app: string, meta?: Mut
2421
return updateOptionList(org, app, optionListId, optionsList);
2522
},
2623
onSuccess: (updatedOptionsList: Option[], { optionListId }) => {
27-
const oldData: OptionsListsResponse = queryClient.getQueryData([
24+
const oldData: OptionListsResponse = queryClient.getQueryData([
2825
QueryKey.OptionLists,
2926
org,
3027
app,
@@ -40,14 +37,14 @@ export const useUpdateOptionListMutation = (org: string, app: string, meta?: Mut
4037
});
4138
};
4239

43-
const isOptionsListInOptionListsCache = (data: OptionsListsResponse | null): boolean => !!data;
40+
const isOptionsListInOptionListsCache = (data: OptionListsResponse | null): boolean => !!data;
4441

4542
const updateListInOptionsListsData = (
4643
optionsListId: string,
47-
updatedOptionsList: OptionsList,
48-
oldData: OptionsListsResponse,
49-
): OptionsListsResponse => {
50-
const [oldOptionsListData, optionsListExists]: [OptionsListData | undefined, boolean] =
44+
updatedOptionsList: OptionList,
45+
oldData: OptionListsResponse,
46+
): OptionListsResponse => {
47+
const [oldOptionsListData, optionsListExists]: [OptionListData | undefined, boolean] =
5148
getOldOptionsListData(oldData, optionsListId);
5249
if (optionsListExists) {
5350
return updateExistingOptionsList(oldData, oldOptionsListData, updatedOptionsList);
@@ -56,19 +53,19 @@ const updateListInOptionsListsData = (
5653
};
5754

5855
const getOldOptionsListData = (
59-
oldData: OptionsListsResponse,
56+
oldData: OptionListsResponse,
6057
optionsListId: string,
61-
): [OptionsListData | undefined, boolean] => {
58+
): [OptionListData | undefined, boolean] => {
6259
const oldOptionsListData = oldData.find(
6360
(optionsListData) => optionsListData.title === optionsListId,
6461
);
6562
return [oldOptionsListData, !!oldOptionsListData];
6663
};
6764

6865
const updateExistingOptionsList = (
69-
oldData: OptionsListsResponse,
70-
oldOptionsListData: OptionsListData,
71-
newOptionsList: OptionsList,
66+
oldData: OptionListsResponse,
67+
oldOptionsListData: OptionListData,
68+
newOptionsList: OptionList,
7269
) => {
7370
return ArrayUtils.replaceByPredicate(
7471
oldData,
@@ -81,9 +78,9 @@ const updateExistingOptionsList = (
8178
};
8279

8380
const addNewOptionsList = (
84-
oldData: OptionsListsResponse,
81+
oldData: OptionListsResponse,
8582
optionsListTitle: string,
86-
newOptionsList: OptionsList,
83+
newOptionsList: OptionList,
8784
) => {
8885
return ArrayUtils.prepend(oldData, { title: optionsListTitle, data: newOptionsList });
8986
};

frontend/packages/shared/src/hooks/queries/useOptionListQuery.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { renderHookWithProviders } from 'app-shared/mocks/renderHookWithProvider
44
import { useOptionListQuery } from 'app-shared/hooks/queries/useOptionListQuery';
55
import { waitFor } from '@testing-library/react';
66
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
7-
import type { OptionsList } from 'app-shared/types/api/OptionsLists';
7+
import type { OptionList } from 'app-shared/types/OptionList';
88
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
99

1010
const optionsListId = 'optionsListId';
@@ -16,7 +16,7 @@ describe('useOptionListQuery', () => {
1616
});
1717

1818
it('getOptionList returns optionList as is', async () => {
19-
const optionsList: OptionsList = [{ value: 'value', label: 'label' }];
19+
const optionsList: OptionList = [{ value: 'value', label: 'label' }];
2020
const getOptionList = jest.fn().mockImplementation(() => Promise.resolve(optionsList));
2121
const { current: currentResult } = await render({ getOptionList });
2222
expect(currentResult.data).toBe(optionsList);

frontend/packages/shared/src/hooks/queries/useOptionListQuery.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext';
22
import { QueryKey } from 'app-shared/types/QueryKey';
33
import type { UseQueryResult } from '@tanstack/react-query';
44
import { useQuery } from '@tanstack/react-query';
5-
import type { OptionsList } from 'app-shared/types/api/OptionsLists';
5+
import type { OptionList } from 'app-shared/types/OptionList';
66

77
export const useOptionListQuery = (
88
org: string,
99
app: string,
1010
optionListId: string,
11-
): UseQueryResult<OptionsList> => {
11+
): UseQueryResult<OptionList> => {
1212
const { getOptionList } = useServicesContext();
13-
return useQuery<OptionsList>({
13+
return useQuery<OptionList>({
1414
queryKey: [QueryKey.OptionList, org, app, optionListId],
1515
queryFn: () => getOptionList(org, app, optionListId),
1616
});

frontend/packages/shared/src/hooks/queries/useOptionListsQuery.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext';
22
import { QueryKey } from 'app-shared/types/QueryKey';
33
import type { UseQueryResult } from '@tanstack/react-query';
44
import { useQuery } from '@tanstack/react-query';
5-
import type { OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
5+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
66

77
export const useOptionListsQuery = (
88
org: string,
99
app: string,
10-
): UseQueryResult<OptionsListsResponse> => {
10+
): UseQueryResult<OptionListsResponse> => {
1111
const { getOptionLists } = useServicesContext();
12-
return useQuery<OptionsListsResponse>({
12+
return useQuery<OptionListsResponse>({
1313
queryKey: [QueryKey.OptionLists, org, app],
1414
queryFn: () => getOptionLists(org, app),
1515
});

frontend/packages/shared/src/hooks/queries/useOptionListsReferencesQuery.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { useServicesContext } from 'app-shared/contexts/ServicesContext';
22
import { QueryKey } from 'app-shared/types/QueryKey';
33
import type { UseQueryResult } from '@tanstack/react-query';
44
import { useQuery } from '@tanstack/react-query';
5-
import type { OptionListsReferences } from 'app-shared/types/api/OptionsLists';
5+
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
66

77
export const useOptionListsReferencesQuery = (
88
org: string,
99
app: string,
10-
): UseQueryResult<OptionListsReferences> => {
10+
): UseQueryResult<OptionListReferences> => {
1111
const { getOptionListsReferences } = useServicesContext();
12-
return useQuery<OptionListsReferences>({
12+
return useQuery<OptionListReferences>({
1313
queryKey: [QueryKey.OptionListsUsage, org, app],
1414
queryFn: () => getOptionListsReferences(org, app),
1515
});

frontend/packages/shared/src/mocks/queriesMock.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,11 @@ import type { DeploymentsResponse } from 'app-shared/types/api/DeploymentsRespon
6969
import type { RepoDiffResponse } from 'app-shared/types/api/RepoDiffResponse';
7070
import type { ExternalImageUrlValidationResponse } from 'app-shared/types/api/ExternalImageUrlValidationResponse';
7171
import type { MaskinportenScope } from 'app-shared/types/MaskinportenScope';
72-
import type {
73-
OptionListsReferences,
74-
OptionsList,
75-
OptionsListsResponse,
76-
} from 'app-shared/types/api/OptionsLists';
72+
import type { OptionList } from 'app-shared/types/OptionList';
73+
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
7774
import type { LayoutSetsModel } from '../types/api/dto/LayoutSetsModel';
7875
import { layoutSetsExtendedMock } from '@altinn/ux-editor/testing/layoutSetsMock';
76+
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
7977

8078
export const queriesMock: ServicesContextProps = {
8179
// Queries
@@ -113,11 +111,11 @@ export const queriesMock: ServicesContextProps = {
113111
.fn()
114112
.mockImplementation(() => Promise.resolve<LayoutSetsModel>(layoutSetsExtendedMock)),
115113
getOptionListIds: jest.fn().mockImplementation(() => Promise.resolve<string[]>([])),
116-
getOptionList: jest.fn().mockImplementation(() => Promise.resolve<OptionsList>([])),
117-
getOptionLists: jest.fn().mockImplementation(() => Promise.resolve<OptionsListsResponse>([])),
114+
getOptionList: jest.fn().mockImplementation(() => Promise.resolve<OptionList>([])),
115+
getOptionLists: jest.fn().mockImplementation(() => Promise.resolve<OptionListsResponse>([])),
118116
getOptionListsReferences: jest
119117
.fn()
120-
.mockImplementation(() => Promise.resolve<OptionListsReferences>([])),
118+
.mockImplementation(() => Promise.resolve<OptionListReferences>([])),
121119
getOrgList: jest.fn().mockImplementation(() => Promise.resolve<OrgList>(orgList)),
122120
getOrganizations: jest.fn().mockImplementation(() => Promise.resolve<Organization[]>([])),
123121
getRepoMetadata: jest.fn().mockImplementation(() => Promise.resolve<Repository>(repository)),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { Option } from 'app-shared/types/Option';
2+
3+
export type OptionList = Option[];
4+
5+
export type OptionListData = {
6+
title: string;
7+
data?: OptionList;
8+
hasError?: boolean;
9+
};

0 commit comments

Comments
 (0)