-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create test for useRemoveFromFavoritesSingleRecordAction
- Loading branch information
1 parent
d809f39
commit a4813be
Showing
2 changed files
with
149 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...d-actions/single-record/hooks/__tests__/useRemoveFromFavoritesSingleRecordAction.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |