Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: frontend mock endpoint to edit org codelist #14522

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions frontend/packages/shared/src/api/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import type { FormLayoutRequest } from 'app-shared/types/api/FormLayoutRequest';
import type { Option } from 'app-shared/types/Option';
import type { MaskinportenScopes } from 'app-shared/types/MaskinportenScope';
import type { DataType } from '../types/DataType';
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';

const headers = {
Accept: 'application/json',
Expand Down Expand Up @@ -167,3 +168,12 @@ export const updateProcessDataTypes = (org: string, app: string, dataTypesChange

// Maskinporten
export const updateSelectedMaskinportenScopes = (org: string, app: string, appScopesUpsertRequest: MaskinportenScopes) => put(selectedMaskinportenScopesPath(org, app), appScopesUpsertRequest);

// Org level code lists - TODO: Replace mocks with correct paths.
export const editOrgLevelCodeList = async (codeListItem: OptionListsResponse): Promise<void> =>
new Promise((resolve) => {
setTimeout(() => {
console.log('Code list edited:', codeListItem);
resolve();
}, 1000);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { queriesMock } from 'app-shared/mocks/queriesMock';
import { renderHookWithProviders } from 'app-development/test/mocks';
import { useEditOrgCodeListMutation } from './useEditOrgCodeListMutation';
import { waitFor } from '@testing-library/react';
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
import { QueryKey } from 'app-shared/types/QueryKey';
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';

const codeListToEdit: OptionListsResponse = [
{
title: 'title',
data: [
{ label: 'label1', value: 'value1' },
{ label: 'label2', value: 'value2' },
],
hasError: false,
},
];

describe('useEditOrgCodeListMutation', () => {
it('Calls editOrgLevelCodeList with correct arguments and payload', async () => {
const result = renderHookWithProviders()(() => useEditOrgCodeListMutation()).renderHookResult
.result;

result.current.mutate(codeListToEdit);
await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(queriesMock.editOrgLevelCodeList).toHaveBeenCalledTimes(1);
expect(queriesMock.editOrgLevelCodeList).toHaveBeenCalledWith(codeListToEdit);
});

it('Invalidates query key', async () => {
const client = createQueryClientMock();
const invalidateQueriesSpy = jest.spyOn(client, 'invalidateQueries');
const result = renderHookWithProviders({}, client)(() => useEditOrgCodeListMutation())
.renderHookResult.result;

result.current.mutate(codeListToEdit);
await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(invalidateQueriesSpy).toHaveBeenCalledTimes(1);
expect(invalidateQueriesSpy).toHaveBeenCalledWith({
queryKey: [QueryKey.OrgLevelCodeLists],
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useServicesContext } from 'app-shared/contexts/ServicesContext';
import type { OptionListsResponse } from 'app-shared/types/api/OptionListsResponse';
import { QueryKey } from 'app-shared/types/QueryKey';

export const useEditOrgCodeListMutation = () => {
const q = useQueryClient();
const { editOrgLevelCodeList } = useServicesContext();
return useMutation({
mutationFn: (codeListItem: OptionListsResponse) => editOrgLevelCodeList(codeListItem),
onSuccess: () => Promise.all([q.invalidateQueries({ queryKey: [QueryKey.OrgLevelCodeLists] })]),
});
};
1 change: 1 addition & 0 deletions frontend/packages/shared/src/mocks/queriesMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export const queriesMock: ServicesContextProps = {
.mockImplementation(() => Promise.resolve({ belongsToOrg: true })),

// Mutations
editOrgLevelCodeList: jest.fn().mockImplementation(() => Promise.resolve()),
addAppAttachmentMetadata: jest.fn().mockImplementation(() => Promise.resolve()),
addDataTypeToAppMetadata: jest.fn().mockImplementation(() => Promise.resolve()),
addImage: jest.fn().mockImplementation(() => Promise.resolve()),
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/shared/src/types/QueryKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export enum QueryKey {
IsLoggedInWithAnsattporten = 'IsLoggedInWithAnsattporten',
AppScopes = 'AppScopes',
SelectedAppScopes = 'SelectedAppScopes',
UserOrgPermissions = 'UserOrgPermissions',
DataType = 'DataType',
OrgLevelCodeLists = 'OrgLevelCodeLists',

// Resourceadm
ResourceList = 'ResourceList',
Expand Down
Loading