Skip to content

Commit

Permalink
refactor unit tests to apply multiple stores
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Dec 4, 2024
1 parent a8fd004 commit 9e58809
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 44 deletions.
34 changes: 26 additions & 8 deletions src/test/__mocks__/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { StoreApi } from 'zustand';

export const setInitialGlobalState = <T>(store: StoreApi<T>, initialState: any) =>
store.setState({
...store.getInitialState(),
...initialState,
type StoreState = Record<string, any>;

interface StoreWithState {
store: StoreApi<StoreState>;
state: StoreState;
}

interface StoreWithUpdatedState<T> {
store: StoreApi<T>;
updatedState: Partial<T>;
}

export const setInitialGlobalState = (storeWithStates: StoreWithState[]) => {
storeWithStates.forEach(({ store, state }) => {
store.setState({
...store.getInitialState(),
...state,
});
});
};

export const setUpdatedGlobalState = <T>(store: StoreApi<T>, updatedState: any) =>
store.setState({
...store.getState(),
...updatedState,
export const setUpdatedGlobalState = <T>(storeWithUpdatedStates: StoreWithUpdatedState<T>[]) => {
storeWithUpdatedStates.forEach(({ store, updatedState }) => {
store.setState({
...store.getState(),
...updatedState,
});
});
};
36 changes: 23 additions & 13 deletions src/test/__tests__/common/hooks/useConfig.hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ describe('useConfig', () => {
const setSelectedRecordBlocks = jest.fn();

const mockUseRecoilState = (profiles: ProfileEntry[] = []) => {
setUpdatedGlobalState(useProfileStore, { profiles, preparedFields: null });
setUpdatedGlobalState([
{
store: useProfileStore,
updatedState: { profiles, preparedFields: null },
},
]);
};

beforeEach(() => {
Expand All @@ -88,18 +93,23 @@ describe('useConfig', () => {
.mockReturnValueOnce(setPreviewContent)
.mockReturnValueOnce(setSelectedRecordBlocks);

setInitialGlobalState(useProfileStore, {
profiles: [],
setProfiles,
selectedProfile: {},
setSelectedProfile,
preparedFields: {},
setPreparedFields,
schema: {},
setSchema,
initialSchemaKey: '',
setInitialSchemaKey,
});
setInitialGlobalState([
{
store: useProfileStore,
state: {
profiles: [],
setProfiles,
selectedProfile: {},
setSelectedProfile,
preparedFields: {},
setPreparedFields,
schema: {},
setSchema,
initialSchemaKey: '',
setInitialSchemaKey,
},
},
]);

(SchemaService.SchemaService as jest.Mock).mockImplementation(() => ({ generate: jest.fn() }));
});
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/common/hooks/useContainerEvents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ describe('useContainerEvents', () => {
(useRecoilValue as jest.Mock).mockReturnValueOnce(false);
(useRecoilValue as jest.Mock).mockReturnValueOnce(mockEvents);

setInitialGlobalState(useStatusStore, { isEditedRecord });
setInitialGlobalState([
{
store: useStatusStore,
state: { isEditedRecord },
},
]);

renderHook(() => useContainerEvents({ watchEditedState: true }));
};
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/common/hooks/useLoadSearchResults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ describe('useLoadSearchResults', () => {
const setIsLoading = jest.fn();

beforeEach(() => {
setInitialGlobalState(useLoadingStateStore, { isLoading: false, setIsLoading });
setInitialGlobalState([
{
store: useLoadingStateStore,
state: { isLoading: false, setIsLoading },
},
]);
});

it('fetches data and updates state with query and searchBy', async () => {
Expand Down
17 changes: 15 additions & 2 deletions src/test/__tests__/common/hooks/useRecordGeneration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ describe('useRecordGeneration', () => {
const selectedEntries = 'mockSelectedEntries';
const initKey = 'mockInitialSchemaKey';

setInitialGlobalState(useProfileStore, { schema, initialSchemaKey: initKey });
setInitialGlobalState(useInputsStore, { userValues });
setInitialGlobalState([
{
store: useProfileStore,
state: {
schema,
initialSchemaKey: initKey,
},
},
{
store: useInputsStore,
state: {
userValues,
},
},
]);

(useRecoilValue as jest.Mock).mockReturnValueOnce(selectedEntries);

Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/common/hooks/useRecordStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ jest.mock('react-router-dom');
describe('useRecordStatus', () => {
const renderUseRecordStatusHook = (lastSavedIdEqual = false) => {
(useParams as jest.Mock).mockReturnValueOnce({ resourceId: mockResourceId });
setInitialGlobalState(useStatusStore, { lastSavedRecordId: lastSavedIdEqual ? mockResourceId : 'anotherId' });
setInitialGlobalState([
{
store: useStatusStore,
state: { lastSavedRecordId: lastSavedIdEqual ? mockResourceId : 'anotherId' },
},
]);

const {
result: { current },
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/common/hooks/useSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ describe('useSearch hook', () => {
};

beforeEach(() => {
setInitialGlobalState(useLoadingStateStore, { isLoading: false, setIsLoading });
setInitialGlobalState([
{
store: useLoadingStateStore,
state: { isLoading: false, setIsLoading },
},
]);
(useSearchContext as jest.Mock).mockReturnValue(mockUseSearchContext);
(useSearchParams as jest.Mock).mockReturnValue([null, setSearchParams]);

Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/components/CommonStatus.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { setInitialGlobalState } from '@src/test/__mocks__/store';

describe('CommonStatus', () => {
const renderComponent = (statusMessages: StatusEntry[] = []) => {
setInitialGlobalState(useStatusStore, { statusMessages });
setInitialGlobalState([
{
store: useStatusStore,
state: { statusMessages },
},
]);

return render(<CommonStatus />);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ describe('Complex Lookup Field', () => {
const { getByTestId, getAllByTestId, queryByTestId, queryAllByTestId, getByRole } = screen;

function renderComponent(entry: SchemaEntry = {} as SchemaEntry, value: UserValueContents[] = []) {
setInitialGlobalState(useProfileStore, { schema: {} as Schema });
setInitialGlobalState([
{
store: useProfileStore,
state: { schema: {} as Schema },
},
]);

render(
<RecoilRoot
Expand Down
22 changes: 14 additions & 8 deletions src/test/__tests__/components/EditSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,20 @@ jest.mock('@common/constants/build.constants', () => ({ IS_EMBEDDED_MODE: false

describe('EditSection', () => {
const renderScreen = () => {
setInitialGlobalState(useProfileStore, {
selectedProfile: monograph as unknown as ProfileEntry,
initialSchemaKey: 'uuid0',
schema: schema as Schema,
});
setInitialGlobalState(useInputsStore, {
userValues,
});
setInitialGlobalState([
{
store: useProfileStore,
state: {
selectedProfile: monograph as unknown as ProfileEntry,
initialSchemaKey: 'uuid0',
schema: schema as Schema,
},
},
{
store: useInputsStore,
state: { userValues },
},
]);

return render(
<RecoilRoot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ describe('MarcPreviewComplexLookup', () => {
marcPreviewData: MarcDTO,
marcPreviewMetadata: MarcPreviewMetadata,
) => {
setInitialGlobalState(useMarcPreviewStore, { complexValue: marcPreviewData, metaData: marcPreviewMetadata });
setInitialGlobalState([
{
store: useMarcPreviewStore,
state: { complexValue: marcPreviewData, metaData: marcPreviewMetadata },
},
]);

return render(
<RecoilRoot
Expand Down
12 changes: 10 additions & 2 deletions src/test/__tests__/components/Preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,16 @@ describe('Preview', () => {
const { getAllByTestId, getByText } = screen;

beforeEach(() => {
setInitialGlobalState(useProfileStore, { initialSchemaKey, schema });
setInitialGlobalState(useInputsStore, { userValues });
setInitialGlobalState([
{
store: useProfileStore,
state: { initialSchemaKey, schema },
},
{
store: useInputsStore,
state: { userValues },
},
]);

return render(
<RecoilRoot
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/components/SaveRecord.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { setInitialGlobalState } from '@src/test/__mocks__/store';

describe('SaveRecord', () => {
function renderSaveRecordComponent(isEditedRecord = true) {
setInitialGlobalState(useStatusStore, { isEditedRecord });
setInitialGlobalState([
{
store: useStatusStore,
state: { isEditedRecord },
},
]);

render(
<RecoilRoot>
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/components/ViewMarcModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ const mockMarcPreview = {

describe('ViewMarcModal', () => {
test('renders modal and its contents', async () => {
setInitialGlobalState(useMarcPreviewStore, { basicValue: mockMarcPreview });
setInitialGlobalState([
{
store: useMarcPreviewStore,
state: { basicValue: mockMarcPreview },
},
]);

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

Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/views/Edit.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ describe('Edit', () => {

const renderComponent = (recordState: ProfileEntry | null) =>
act(async () => {
setInitialGlobalState(useProfileStore, { selectedProfile: recordState });
setInitialGlobalState([
{
store: useProfileStore,
state: { selectedProfile: recordState },
},
]);

return render(
<RecoilRoot>
Expand Down
7 changes: 6 additions & 1 deletion src/test/__tests__/views/Root.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ useRoutePathPattern.mockImplementation(() => null);

describe('Root', () => {
function renderRootComponent(isLoading = false) {
setInitialGlobalState(useLoadingStateStore, { isLoading });
setInitialGlobalState([
{
store: useLoadingStateStore,
state: { isLoading },
},
]);

render(
<RecoilRoot>
Expand Down

0 comments on commit 9e58809

Please sign in to comment.