Skip to content

Commit

Permalink
fix test and add return types for hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
standeren committed Nov 27, 2024
1 parent 5a023ee commit 3ea2e89
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { screen } from '@testing-library/react';

import { textMock } from '@studio/testing/mocks/i18nMock';
import { CodeListsActionsBar } from './CodeListsActionsBar';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -43,44 +42,54 @@ describe('CodeListsActionsBar', () => {
it('does not call onUploadCodeList when uploading a file with existing file name', async () => {
const user = userEvent.setup();
renderCodeListsActionsBar();
const fileUploaderButton = screen.getByLabelText(
textMock('app_content_library.code_lists.upload_code_list'),
);
const file = new File(['test'], `${codeListName1}.json`, { type: 'application/json' });
await user.upload(fileUploaderButton, file);
await uploadFileWithExistingFileName(user);
expect(onUploadCodeListMock).not.toHaveBeenCalled();
});

it('renders correct toast error message when uploading a file with existing file name', async () => {
const user = userEvent.setup();
renderCodeListsActionsBar();
const fileUploaderButton = screen.getByLabelText(
textMock('app_content_library.code_lists.upload_code_list'),
);
const file = new File(['test'], `${codeListName1}.json`, { type: 'application/json' });
await user.upload(fileUploaderButton, file);
await uploadFileWithExistingFileName(user);
const toastErrorText = screen.getByText(
textMock('validation_errors.upload_file_name_occupied'),
);
expect(toastErrorText).toBeInTheDocument();
});

it('renders correct toast error message when uploading a file with empty name', async () => {
it('does not call onUploadCodeList when uploading a file with empty name', async () => {
const user = userEvent.setup();
renderCodeListsActionsBar();
const fileUploaderButton = screen.getByLabelText(
textMock('app_content_library.code_lists.upload_code_list'),
);
const file = new File(['test'], '.json', { type: 'application/json' });
await user.upload(fileUploaderButton, file);
await uploadFileWithEmptyFileName(user);
expect(onUploadCodeListMock).not.toHaveBeenCalled();
});

it('renders correct toast error message when uploading a file with empty name', async () => {
const user = userEvent.setup();
renderCodeListsActionsBar();
await uploadFileWithEmptyFileName(user);
const toastErrorText = screen.getByText(
textMock('validation_errors.upload_file_name_required'),
);
expect(toastErrorText).toBeInTheDocument();
});
});

const uploadFileWithExistingFileName = async (user: UserEvent) => {

Check failure on line 77 in frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeList/CodeListsActionsBar/CodeListsActionsBar.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

Cannot find name 'UserEvent'. Did you mean 'MouseEvent'?
const fileUploaderButton = screen.getByLabelText(
textMock('app_content_library.code_lists.upload_code_list'),
);
const file = new File(['test'], `${codeListName1}.json`, { type: 'application/json' });
await user.upload(fileUploaderButton, file);
};

const uploadFileWithEmptyFileName = async (user: UserEvent) => {

Check failure on line 85 in frontend/libs/studio-content-library/src/ContentLibrary/LibraryBody/pages/CodeList/CodeListsActionsBar/CodeListsActionsBar.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

Cannot find name 'UserEvent'. Did you mean 'MouseEvent'?
const fileUploaderButton = screen.getByLabelText(
textMock('app_content_library.code_lists.upload_code_list'),
);
const file = new File(['test'], '.json', { type: 'application/json' });
await user.upload(fileUploaderButton, file);
};

const renderCodeListsActionsBar = () => {
renderWithProviders(
<CodeListsActionsBar
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FileNameErrorResult } from '@studio/pure-functions';
import { useTranslation } from 'react-i18next';

export function useInputCodeListNameErrorMessage() {
export function useInputCodeListNameErrorMessage(): (fileNameError: FileNameErrorResult) => string {
const { t } = useTranslation();

type FileNameInputErrorResult = Exclude<FileNameErrorResult, FileNameErrorResult.NoRegExMatch>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FileNameErrorResult } from '@studio/pure-functions';
import { useTranslation } from 'react-i18next';

export function useUploadCodeListNameErrorMessage() {
export function useUploadCodeListNameErrorMessage(): (
fileNameError: FileNameErrorResult,
) => string {
const { t } = useTranslation();

type FileNameUploadErrorResult = Exclude<FileNameErrorResult, FileNameErrorResult.NoRegExMatch>;
Expand Down

0 comments on commit 3ea2e89

Please sign in to comment.