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: 14508/delete code list for org #14630

Merged
merged 4 commits into from
Feb 13, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,44 @@ import { OrgContentLibrary } from './OrgContentLibrary';
import { screen } from '@testing-library/react';
import { textMock } from '@studio/testing/mocks/i18nMock';
import { renderWithProviders } from '../../testing/mocks';
import userEvent, { type UserEvent } from '@testing-library/user-event';
import type { ServicesContextProps } from 'app-shared/contexts/ServicesContext';
import type { QueryClient } from '@tanstack/react-query';
import { createQueryClientMock } from 'app-shared/mocks/queryClientMock';
import { QueryKey } from 'app-shared/types/QueryKey';
import { org } from '@studio/testing/testids';
import type { CodeListData } from '@studio/content-library';
import type { CodeList } from '@studio/components';
import { queriesMock } from 'app-shared/mocks/queriesMock';

const deleteCodeListButtonTextMock = 'Delete Code List';
const codeListNameMock = 'codeListNameMock';
const codeListMock: CodeList = [{ value: '', label: '' }];
const codeListsDataMock: CodeListData[] = [{ title: codeListNameMock, data: codeListMock }];

jest.mock(
'../../../libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeListPage',
() => ({
CodeListPage: ({ onDeleteCodeList }: any) => (
<div>
<button onClick={() => onDeleteCodeList(codeListsDataMock[0].title)}>
{deleteCodeListButtonTextMock}
</button>
</div>
),
}),
);

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
selectedContext: 'testOrg',
}),
}));

describe('OrgContentLibrary', () => {
afterEach(jest.clearAllMocks);

it('renders the library title', () => {
renderWithProviders(<OrgContentLibrary />);
const libraryTitle = screen.getByRole('heading', {
Expand Down Expand Up @@ -32,4 +68,52 @@ describe('OrgContentLibrary', () => {
});
expect(codeListMenuElement).toBeInTheDocument();
});

it('calls deleteCodeListForOrg when onDeleteCodeList is triggered', async () => {
const user = userEvent.setup();
renderOrgContentLibraryWithCodeLists();
await goToLibraryPage(user, 'code_lists');
const deleteCodeListButton = screen.getByRole('button', { name: deleteCodeListButtonTextMock });
await user.click(deleteCodeListButton);
expect(queriesMock.deleteCodeListForOrg).toHaveBeenCalledTimes(1);
expect(queriesMock.deleteCodeListForOrg).toHaveBeenCalledWith(org, codeListsDataMock[0].title);
});
ErlingHauan marked this conversation as resolved.
Show resolved Hide resolved
});

const getLibraryPageTile = (libraryPage: string) =>
screen.getByText(textMock(`app_content_library.${libraryPage}.page_name`));

const goToLibraryPage = async (user: UserEvent, libraryPage: string) => {
const libraryPageNavTile = getLibraryPageTile(libraryPage);
await user.click(libraryPageNavTile);
};

type RenderOrgContentLibraryProps = {
queries?: Partial<ServicesContextProps>;
queryClient?: QueryClient;
};

const renderAppContentLibrary = ({
queries = {},
queryClient = createQueryClientMock(),
}: RenderOrgContentLibraryProps = {}): void => {
renderWithProviders(<OrgContentLibrary />, {
queries,
queryClient,
});
};

function renderOrgContentLibraryWithCodeLists(
props?: Omit<RenderOrgContentLibraryProps, 'queryClient'>,
): void {
const queryClient = createQueryClientWithOptionsDataList(codeListsDataMock);
renderAppContentLibrary({ ...props, queryClient });
}

function createQueryClientWithOptionsDataList(
codeListDataList: CodeListData[] | undefined,
): QueryClient {
const queryClient = createQueryClientMock();
queryClient.setQueryData([QueryKey.OrgCodeLists, org], codeListDataList);
return queryClient;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import type { ReactElement } from 'react';
import React from 'react';
import { ResourceContentLibraryImpl } from '@studio/content-library';
import { useSelectedContext } from '../../hooks/useSelectedContext';
import { useDeleteOrgCodeListMutation } from 'app-shared/hooks/mutations/useDeleteOrgCodeListMutation';

export function OrgContentLibrary(): ReactElement {
const org = useSelectedContext();
const { mutate: deleteCodeList } = useDeleteOrgCodeListMutation(org);

const { getContentResourceLibrary } = new ResourceContentLibraryImpl({
pages: {
codeList: {
props: {
codeListsData: [],
onDeleteCodeList: () => {},
onDeleteCodeList: deleteCodeList,
onUpdateCodeListId: () => {},
onUpdateCodeList: () => {},
onUploadCodeList: () => {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('useDeleteOrgCodeListMutation', () => {

it('Calls deleteCodeListForOrg with correct parameters', async () => {
const { result } = renderHookWithProviders(() => useDeleteOrgCodeListMutation(org));
await result.current.mutateAsync({ title });
await result.current.mutateAsync(title);
expect(queriesMock.deleteCodeListForOrg).toHaveBeenCalledTimes(1);
expect(queriesMock.deleteCodeListForOrg).toHaveBeenCalledWith(org, title);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
import { useServicesContext } from '../../contexts/ServicesContext';
import { QueryKey } from '../../types/QueryKey';
import type { CodeListsResponse } from '../../types/api/CodeListsResponse';
import type { CodeListData } from '../../types/CodeListData';

type DeleteOrgCodeListMutationArgs = Pick<CodeListData, 'title'>;

export const useDeleteOrgCodeListMutation = (org: string) => {
const queryClient = useQueryClient();
const { deleteCodeListForOrg } = useServicesContext();

const mutationFn = ({ title }: DeleteOrgCodeListMutationArgs) => deleteCodeListForOrg(org, title);
const mutationFn = (title: string) => deleteCodeListForOrg(org, title);

return useMutation({
mutationFn,
Expand Down