Skip to content

Commit

Permalink
create test for useRemoveFromFavoritesSingleRecordAction
Browse files Browse the repository at this point in the history
  • Loading branch information
bosiraphael committed Dec 13, 2024
1 parent d809f39 commit a4813be
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,25 @@ const personMockObjectMetadataItem = generatedMockObjectMetadataItems.find(

const peopleMock = getPeopleMock();

const favoritesMock = [
{
id: '1',
recordId: peopleMock[0].id,
position: 0,
avatarType: 'rounded',
avatarUrl: '',
labelIdentifier: ' ',
link: `/object/${personMockObjectMetadataItem.nameSingular}/${peopleMock[0].id}`,
objectNameSingular: personMockObjectMetadataItem.nameSingular,
workspaceMemberId: '1',
favoriteFolderId: undefined,
},
];

jest.mock('@/favorites/hooks/useFavorites', () => ({
useFavorites: () => ({
favorites: [
{
id: '1',
recordId: peopleMock[0].id,
position: 0,
avatarType: 'rounded',
avatarUrl: '',
labelIdentifier: ' ',
link: `/object/${personMockObjectMetadataItem.nameSingular}/${peopleMock[0].id}`,
objectNameSingular: personMockObjectMetadataItem.nameSingular,
workspaceMemberId: '1',
favoriteFolderId: undefined,
},
],

sortedFavorites: [
{
id: '1',
recordId: peopleMock[0].id,
position: 0,
avatarType: 'rounded',
avatarUrl: '',
labelIdentifier: ' ',
link: `/object/${personMockObjectMetadataItem.nameSingular}/${peopleMock[0].id}`,
objectNameSingular: personMockObjectMetadataItem.nameSingular,
workspaceMemberId: '1',
favoriteFolderId: undefined,
},
],
favorites: favoritesMock,
sortedFavorites: favoritesMock,
}),
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { renderHook } from '@testing-library/react';
import { act } from 'react';
import {
GetJestMetadataAndApolloMocksAndActionMenuWrapperProps,
getJestMetadataAndApolloMocksAndActionMenuWrapper,
} from '~/testing/jest/getJestMetadataAndApolloMocksAndContextStoreWrapper';
import { generatedMockObjectMetadataItems } from '~/testing/mock-data/generatedMockObjectMetadataItems';
import { getPeopleMock } from '~/testing/mock-data/people';
import { useRemoveFromFavoritesSingleRecordAction } from '../useRemoveFromFavoritesSingleRecordAction';

const personMockObjectMetadataItem = generatedMockObjectMetadataItems.find(
(item) => item.nameSingular === 'person',
)!;

const peopleMock = getPeopleMock();

const favoritesMock = [
{
id: '1',
recordId: peopleMock[0].id,
position: 0,
avatarType: 'rounded',
avatarUrl: '',
labelIdentifier: ' ',
link: `/object/${personMockObjectMetadataItem.nameSingular}/${peopleMock[0].id}`,
objectNameSingular: personMockObjectMetadataItem.nameSingular,
workspaceMemberId: '1',
favoriteFolderId: undefined,
},
];

jest.mock('@/favorites/hooks/useFavorites', () => ({
useFavorites: () => ({
favorites: favoritesMock,
sortedFavorites: favoritesMock,
}),
}));

const deleteFavoriteMock = jest.fn();

jest.mock('@/favorites/hooks/useDeleteFavorite', () => ({
useDeleteFavorite: () => ({
deleteFavorite: deleteFavoriteMock,
}),
}));

const wrapperConfigWithSelectedRecordAsFavorite: GetJestMetadataAndApolloMocksAndActionMenuWrapperProps =
{
apolloMocks: [],
componentInstanceId: '1',
contextStoreCurrentObjectMetadataNameSingular:
personMockObjectMetadataItem.nameSingular,
contextStoreTargetedRecordsRule: {
mode: 'selection',
selectedRecordIds: [peopleMock[0].id],
},
onInitializeRecoilSnapshot: (snapshot) => {
snapshot.set(recordStoreFamilyState(peopleMock[0].id), peopleMock[0]);
snapshot.set(recordStoreFamilyState(peopleMock[1].id), peopleMock[1]);
},
};

const wrapperConfigWithSelectedRecordNotAsFavorite: GetJestMetadataAndApolloMocksAndActionMenuWrapperProps =
{
...wrapperConfigWithSelectedRecordAsFavorite,
contextStoreTargetedRecordsRule: {
mode: 'selection',
selectedRecordIds: [peopleMock[1].id],
},
};

const wrapperWithSelectedRecordAsFavorite =
getJestMetadataAndApolloMocksAndActionMenuWrapper(
wrapperConfigWithSelectedRecordAsFavorite,
);

const wrapperWithSelectedRecordNotAsFavorite =
getJestMetadataAndApolloMocksAndActionMenuWrapper(
wrapperConfigWithSelectedRecordNotAsFavorite,
);

describe('useRemoveFromFavoritesSingleRecordAction', () => {
it('should be registered when the record is a favorite', () => {
const { result } = renderHook(
() =>
useRemoveFromFavoritesSingleRecordAction({
recordId: peopleMock[0].id,
objectMetadataItem: personMockObjectMetadataItem,
}),
{
wrapper: wrapperWithSelectedRecordAsFavorite,
},
);

expect(result.current.shouldBeRegistered).toBe(true);
});

it('should not be registered when the record is not a favorite', () => {
const { result } = renderHook(
() =>
useRemoveFromFavoritesSingleRecordAction({
recordId: peopleMock[1].id,
objectMetadataItem: personMockObjectMetadataItem,
}),
{
wrapper: wrapperWithSelectedRecordNotAsFavorite,
},
);

expect(result.current.shouldBeRegistered).toBe(false);
});

it('should call deleteFavorite on click', () => {
const { result } = renderHook(
() =>
useRemoveFromFavoritesSingleRecordAction({
recordId: peopleMock[0].id,
objectMetadataItem: personMockObjectMetadataItem,
}),
{
wrapper: wrapperWithSelectedRecordAsFavorite,
},
);

act(() => {
result.current.onClick();
});

expect(deleteFavoriteMock).toHaveBeenCalledWith(favoritesMock[0].id);
});
});

0 comments on commit a4813be

Please sign in to comment.