Skip to content

Commit

Permalink
basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 29, 2024
1 parent f09ed72 commit 51fce8f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/test/__tests__/common/hooks/useContainerEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { renderHook } from '@testing-library/react';
import { useRecoilValue } from 'recoil';
import { useContainerEvents } from '@common/hooks/useContainerEvents';
import * as domHelper from '@common/helpers/dom.helper';
import { useStatusStore } from '@src/store';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Expand All @@ -19,8 +20,8 @@ jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: true }
describe('useContainerEvents', () => {
const renderUseContainerEventsHook = (isEdited: boolean) => {
(domHelper.dispatchEventWrapper as jest.Mock) = mockDispatchEventWrapper;
(useStatusStore as jest.Mock).mockReturnValueOnce({ isEditedRecord: isEdited });
(useRecoilValue as jest.Mock).mockReturnValueOnce(false);
(useRecoilValue as jest.Mock).mockReturnValueOnce(isEdited);
(useRecoilValue as jest.Mock).mockReturnValueOnce(mockEvents);

renderHook(() => useContainerEvents({ watchEditedState: true }));
Expand Down
11 changes: 6 additions & 5 deletions src/test/__tests__/common/hooks/useProfileSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '@src/test/__mocks__/common/hooks/useServicesContext.mock';
import { schemaWithDuplicatesService } from '@src/test/__mocks__/common/hooks/useServicesContext.mock';
import { useProfileSchema } from '@common/hooks/useProfileSchema';
import { renderHook } from '@testing-library/react';
import { useSetRecoilState, useRecoilState } from 'recoil';
import { useStatusStore } from '@src/store';

jest.mock('recoil');

Expand All @@ -15,8 +15,9 @@ describe('useProfileSchema', () => {
};

beforeEach(() => {
(useRecoilState as jest.Mock).mockReturnValue([new Set(), jest.fn()]);
(useSetRecoilState as jest.Mock).mockImplementation(jest.fn);
(useRecoilState as jest.Mock).mockReturnValueOnce([new Set(), jest.fn()]);
(useStatusStore as jest.Mock).mockReturnValue({ setIsEditedRecord: jest.fn });
});

test('get schema with copied entries', () => {
Expand All @@ -25,13 +26,13 @@ describe('useProfileSchema', () => {
result.current.getSchemaWithCopiedEntries(entry, []);

expect(schemaWithDuplicatesService.duplicateEntry).toHaveBeenCalled();
})
});

test('get schema with deleted entries', () => {
const { result } = renderHook(() => useProfileSchema());

result.current.getSchemaWithDeletedEntries(entry);

expect(schemaWithDuplicatesService.deleteEntry).toHaveBeenCalled();
})
})
});
});
7 changes: 4 additions & 3 deletions src/test/__tests__/common/hooks/useRecordStatus.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { renderHook } from '@testing-library/react';
import { useRecoilValue } from 'recoil';
import { useStatusStore } from '@src/store';
import { useRecordStatus } from '@common/hooks/useRecordStatus';
import { useParams } from 'react-router-dom';

const mockResourceId = 'mockResourceId';

jest.mock('recoil');
jest.mock('react-router-dom');

describe('useRecordStatus', () => {
const renderUseRecordStatusHook = (lastSavedIdEqual = false) => {
(useParams as jest.Mock).mockReturnValueOnce({ resourceId: mockResourceId });
(useRecoilValue as jest.Mock).mockReturnValueOnce(lastSavedIdEqual ? mockResourceId : 'anotherId');
(useStatusStore as jest.Mock).mockReturnValueOnce({
lastSavedRecordId: lastSavedIdEqual ? mockResourceId : 'anotherId',
});

const {
result: { current },
Expand Down
12 changes: 5 additions & 7 deletions src/test/__tests__/components/SaveRecord.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import { RecoilRoot } from 'recoil';
import { render, screen, fireEvent } from '@testing-library/react';
import { saveRecord } from '@src/test/__mocks__/common/hooks/useRecordControls.mock';
import { SaveRecord } from '@components/SaveRecord';
import state from '@state';
import { BrowserRouter } from 'react-router-dom';
import { useStatusStore } from '@src/store';

describe('SaveRecord', () => {
function renderSaveRecordComponent(recordIsEdited = true) {
function renderSaveRecordComponent(isEditedRecord = true) {
(useStatusStore as jest.Mock).mockReturnValue({ isEditedRecord });

render(
<RecoilRoot
initializeState={snapshot => {
snapshot.set(state.status.recordIsEdited, recordIsEdited);
}}
>
<RecoilRoot>
<BrowserRouter>
<SaveRecord primary />
</BrowserRouter>
Expand Down

0 comments on commit 51fce8f

Please sign in to comment.