Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Dec 20, 2024
1 parent 22829f0 commit efca53c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const updateCodeListIdButtonTextMock = 'Update Code List Id';
const codeListNameMock = 'codeListNameMock';
const newCodeListNameMock = 'newCodeListNameMock';
const codeListMock: CodeList = [{ value: '', label: '' }];
const optionListsDataMock: OptionsListsResponse = [{ title: codeListNameMock, data: codeListMock }];
jest.mock(
'../../../libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage',
() => ({
Expand Down Expand Up @@ -119,7 +120,7 @@ describe('AppContentLibrary', () => {

it('calls onUpdateOptionListId when onUpdateCodeListId is triggered', async () => {
const user = userEvent.setup();
renderAppContentLibrary(optionListsMock);
renderAppContentLibrary();
await goToLibraryPage(user, 'code_lists');
const updateCodeListIdButton = screen.getByRole('button', {
name: updateCodeListIdButtonTextMock,
Expand Down Expand Up @@ -150,7 +151,7 @@ type renderAppContentLibraryProps = {

const renderAppContentLibrary = ({
queries = {},
optionListsData = [],
optionListsData = optionListsDataMock,
}: renderAppContentLibraryProps = {}) => {
const queryClientMock = createQueryClientMock();
if (optionListsData.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { UserEvent } from '@testing-library/user-event';
import { textMock } from '@studio/testing/mocks/i18nMock';
import type { CodeList as StudioComponentCodeList } from '@studio/components';
import { codeListsDataMock } from '../../../../../mocks/mockPagesConfig';
import { ArrayUtils } from '@studio/pure-functions';

const onUpdateCodeListIdMock = jest.fn();
const onUpdateCodeListMock = jest.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function EditCodeList({
};

const handleValidateCodeListId = (newCodeListId: string) => {
const invalidCodeListNames = ArrayUtils.removeItemByValue(codeListNames, codeList.title);
const invalidCodeListNames = ArrayUtils.removeItemByValue(codeListNames, codeListTitle);
const fileNameError = FileNameUtils.findFileNameError(newCodeListId, invalidCodeListNames);
return getInvalidInputFileNameErrorMessage(fileNameError);
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/src/api/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ import type { Policy } from 'app-shared/types/Policy';
import type { RepoDiffResponse } from 'app-shared/types/api/RepoDiffResponse';
import type { ExternalImageUrlValidationResponse } from 'app-shared/types/api/ExternalImageUrlValidationResponse';
import type { MaskinportenScopes } from 'app-shared/types/MaskinportenScope';
import type {OptionsList, OptionsListsResponse} from 'app-shared/types/api/OptionsLists';
import type { OptionsList, OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
import type { LayoutSetsModel } from '../types/api/dto/LayoutSetsModel';

export const getIsLoggedInWithAnsattporten = () => get<{ isLoggedIn: boolean }>(authStatusAnsattporten());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,23 @@ describe('useUpdateOptionListMutation', () => {
expect(queriesMock.updateOptionList).toHaveBeenCalledWith(org, app, optionListId, optionsList);
});

test('Sets the updated option list on the cache for all option lists', async () => {
test('Sets the updated option list on the cache for all option lists when cache contains the list', async () => {
const queryClient = createQueryClientMock();
queryClient.setQueryData(
[QueryKey.OptionLists, org, app],
[{ title: optionListId, data: optionsList }],
);
const renderUpdateOptionListMutationResult = renderHookWithProviders(
() => useUpdateOptionListMutation(org, app),
{ queries: { updateOptionList }, queryClient },
).result;
await renderUpdateOptionListMutationResult.current.mutateAsync(args);
expect(queryClient.getQueryData([QueryKey.OptionLists, org, app])).toEqual({
test: updatedOptionsList,
});
expect(queryClient.getQueryData([QueryKey.OptionLists, org, app])).toEqual([
{
title: optionListId,
data: updatedOptionsList,
},
]);
});

test('Sets the updated option list on the cache for the single option list', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { MutationMeta } from '@tanstack/react-query';
import { QueryKey } from 'app-shared/types/QueryKey';
import type { Option } from 'app-shared/types/Option';
import type { OptionsList, OptionsListsResponse } from 'app-shared/types/api/OptionsLists';
import type {
OptionsList,
OptionsListData,
OptionsListsResponse,
} from 'app-shared/types/api/OptionsLists';
import { useQueryClient, useMutation } from '@tanstack/react-query';
import { useServicesContext } from 'app-shared/contexts/ServicesContext';
import { ArrayUtils } from '@studio/pure-functions';
Expand All @@ -25,23 +29,29 @@ export const useUpdateOptionListMutation = (org: string, app: string, meta?: Mut
org,
app,
]);
const newData = updateListInOptionLists(optionListId, updatedOptionList, oldData);
queryClient.setQueryData([QueryKey.OptionLists, org, app], newData);
if (isOptionsListInOptionListsCache(oldData)) {
const newData = updateListInOptionListsData(optionListId, updatedOptionList, oldData);
queryClient.setQueryData([QueryKey.OptionLists, org, app], newData);
}
queryClient.setQueryData([QueryKey.OptionList, org, app, optionListId], updatedOptionList);
void queryClient.invalidateQueries({ queryKey: [QueryKey.OptionListIds, org, app] });
},
meta,
});
};

const updateListInOptionLists = (
const isOptionsListInOptionListsCache = (data: OptionsListsResponse | null): boolean => !!data;

const updateListInOptionListsData = (
optionListId: string,
updatedOptionList: OptionsList,
oldData: OptionsListsResponse,
): OptionsListsResponse => {
const oldOptionList = oldData.find((optionList) => optionList.title === optionListId);
const oldOptionsListData: OptionsListData = oldData.find(
(optionListData) => optionListData.title === optionListId,
);
return ArrayUtils.replaceByPredicate(oldData, (optionList) => optionList.title === optionListId, {
...oldOptionList,
...oldOptionsListData,
data: updatedOptionList,
});
};

0 comments on commit efca53c

Please sign in to comment.