Skip to content

Commit

Permalink
tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 29, 2024
1 parent 8615609 commit 88577c6
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 36 deletions.
7 changes: 7 additions & 0 deletions src/test/__mocks__/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { StoreApi } from 'zustand';

export const setInitialState = <T>(store: StoreApi<T>, initialState: any) =>
store.setState({
...store.getInitialState(),
...initialState,
});
52 changes: 52 additions & 0 deletions src/test/__mocks__/zustand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { act } from '@testing-library/react';
import type * as ZustandExportedTypes from 'zustand';
export * from 'zustand';

const { create: actualCreate, createStore: actualCreateStore } =
jest.requireActual<typeof ZustandExportedTypes>('zustand');

// a variable to hold reset functions for all stores declared in the app
export const storeResetFns = new Set<() => void>();

const createUncurried = <T>(stateCreator: ZustandExportedTypes.StateCreator<T>) => {
const store = actualCreate(stateCreator);
const initialState = store.getInitialState();
storeResetFns.add(() => {
store.setState(initialState, true);
});
return store;
};

// when creating a store, we get its initial state, create a reset function and add it in the set
export const create = (<T>(stateCreator: ZustandExportedTypes.StateCreator<T>) => {
console.log('zustand create mock');

// to support curried version of create
return typeof stateCreator === 'function' ? createUncurried(stateCreator) : createUncurried;
}) as typeof ZustandExportedTypes.create;

const createStoreUncurried = <T>(stateCreator: ZustandExportedTypes.StateCreator<T>) => {
const store = actualCreateStore(stateCreator);
const initialState = store.getInitialState();
storeResetFns.add(() => {
store.setState(initialState, true);
});
return store;
};

// when creating a store, we get its initial state, create a reset function and add it in the set
export const createStore = (<T>(stateCreator: ZustandExportedTypes.StateCreator<T>) => {
console.log('zustand createStore mock');

// to support curried version of createStore
return typeof stateCreator === 'function' ? createStoreUncurried(stateCreator) : createStoreUncurried;
}) as typeof ZustandExportedTypes.createStore;

// reset all stores after each test run
afterEach(() => {
act(() => {
storeResetFns.forEach(resetFn => {
resetFn();
});
});
});
8 changes: 2 additions & 6 deletions src/test/__tests__/common/hooks/useContainerEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRecoilValue } from 'recoil';
import { useContainerEvents } from '@common/hooks/useContainerEvents';
import * as domHelper from '@common/helpers/dom.helper';
import { useStatusStore } from '@src/store';
import { setInitialState } from '@src/test/__mocks__/store';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
Expand All @@ -18,17 +19,12 @@ jest.mock('recoil');
jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: true }));

describe('useContainerEvents', () => {
const initialStatusStoreStates = useStatusStore.getState();

const renderUseContainerEventsHook = (isEditedRecord: boolean) => {
(domHelper.dispatchEventWrapper as jest.Mock) = mockDispatchEventWrapper;
(useRecoilValue as jest.Mock).mockReturnValueOnce(false);
(useRecoilValue as jest.Mock).mockReturnValueOnce(mockEvents);

useStatusStore.setState({
...initialStatusStoreStates,
isEditedRecord,
});
setInitialState(useStatusStore, { isEditedRecord });

renderHook(() => useContainerEvents({ watchEditedState: true }));
};
Expand Down
8 changes: 2 additions & 6 deletions src/test/__tests__/common/hooks/useRecordStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ import { renderHook } from '@testing-library/react';
import { useStatusStore } from '@src/store';
import { useRecordStatus } from '@common/hooks/useRecordStatus';
import { useParams } from 'react-router-dom';
import { setInitialState } from '@src/test/__mocks__/store';

const mockResourceId = 'mockResourceId';

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

describe('useRecordStatus', () => {
const initialStatusStoreStates = useStatusStore.getState();

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

const {
result: { current },
Expand Down
8 changes: 2 additions & 6 deletions src/test/__tests__/components/CommonStatus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { CommonStatus } from '@components/CommonStatus';
import { StatusType } from '@common/constants/status.constants';
import { useStatusStore } from '@src/store';
import { setInitialState } from '@src/test/__mocks__/store';

describe('CommonStatus', () => {
const initialStatusStoreStates = useStatusStore.getState();

const renderComponent = (commonMessagesState: StatusEntry[] = []) => {
useStatusStore.setState({
...initialStatusStoreStates,
statusMessages: commonMessagesState,
});
setInitialState(useStatusStore, { statusMessages: commonMessagesState });

return render(<CommonStatus />);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RecoilRoot } from 'recoil';
import { useSearchContext } from '@common/hooks/useSearchContext';
import { MarcPreviewComplexLookup } from '@components/ComplexLookupField/MarcPreviewComplexLookup';
import { useMarcPreviewStore } from '@src/store';
import { setInitialState } from '@src/test/__mocks__/store';
import state from '@state';

jest.mock('@common/hooks/useSearchContext');
Expand All @@ -22,7 +23,6 @@ const marcPreviewMetadata = {
} as MarcPreviewMetadata;

describe('MarcPreviewComplexLookup', () => {
const initialMarcPreviewState = useMarcPreviewStore.getState();
const onClose = jest.fn();
const onAssignRecord = jest.fn();

Expand All @@ -35,11 +35,7 @@ describe('MarcPreviewComplexLookup', () => {
marcPreviewData: MarcDTO,
marcPreviewMetadata: MarcPreviewMetadata,
) => {
useMarcPreviewStore.setState({
...initialMarcPreviewState,
data: marcPreviewData,
metaData: marcPreviewMetadata,
});
setInitialState(useMarcPreviewStore, { data: marcPreviewData, metaData: marcPreviewMetadata });

return render(
<RecoilRoot
Expand Down
8 changes: 2 additions & 6 deletions src/test/__tests__/components/SaveRecord.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import { saveRecord } from '@src/test/__mocks__/common/hooks/useRecordControls.m
import { SaveRecord } from '@components/SaveRecord';
import { BrowserRouter } from 'react-router-dom';
import { useStatusStore } from '@src/store';
import { setInitialState } from '@src/test/__mocks__/store';

describe('SaveRecord', () => {
const initialStatusStoreStates = useStatusStore.getState();

function renderSaveRecordComponent(isEditedRecord = true) {
useStatusStore.setState({
...initialStatusStoreStates,
isEditedRecord,
});
setInitialState(useStatusStore, { isEditedRecord });

render(
<RecoilRoot>
Expand Down
8 changes: 2 additions & 6 deletions src/test/__tests__/components/ViewMarcModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ViewMarcModal } from '@components/ViewMarcModal';
import { useMarcPreviewStore } from '@src/store';
import { setInitialState } from '@src/test/__mocks__/store';
import { render } from '@testing-library/react';

const { leader, subfieldContent } = {
Expand Down Expand Up @@ -33,13 +34,8 @@ const mockMarcPreview = {
};

describe('ViewMarcModal', () => {
const initialMarcPreviewState = useMarcPreviewStore.getState();

test('renders modal and its contents', async () => {
useMarcPreviewStore.setState({
...initialMarcPreviewState,
value: mockMarcPreview,
});
setInitialState(useMarcPreviewStore, { value: mockMarcPreview });

const { findByText } = render(<ViewMarcModal />);

Expand Down

0 comments on commit 88577c6

Please sign in to comment.