-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify react-query hooks + fix types
- Loading branch information
1 parent
ec2c842
commit bdc8d30
Showing
6 changed files
with
78 additions
and
187 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
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
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
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
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 |
---|---|---|
@@ -1,121 +1,95 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useQuery, useMutation } from '@tanstack/react-query'; | ||
import { act } from '@testing-library/react'; | ||
import { | ||
useTaxonomyTagsDataResponse, | ||
useIsTaxonomyTagsDataLoaded, | ||
useContentTaxonomyTagsDataResponse, | ||
useIsContentTaxonomyTagsDataLoaded, | ||
useContentDataResponse, | ||
useIsContentDataLoaded, | ||
useTaxonomyTagsData, | ||
useContentTaxonomyTagsData, | ||
useContentData, | ||
useContentTaxonomyTagsMutation, | ||
} from './apiHooks'; | ||
|
||
import { updateContentTaxonomyTags } from './api'; | ||
|
||
jest.mock('@tanstack/react-query', () => ({ | ||
useQuery: jest.fn(), | ||
useMutation: jest.fn(), | ||
})); | ||
|
||
describe('useTaxonomyTagsDataResponse', () => { | ||
it('should return data when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success', data: { data: 'data' } }); | ||
const taxonomyId = '123'; | ||
const result = useTaxonomyTagsDataResponse(taxonomyId); | ||
|
||
expect(result).toEqual({ data: 'data' }); | ||
}); | ||
|
||
it('should return undefined when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
const taxonomyId = '123'; | ||
const result = useTaxonomyTagsDataResponse(taxonomyId); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
jest.mock('./api', () => ({ | ||
updateContentTaxonomyTags: jest.fn(), | ||
})); | ||
|
||
describe('useIsTaxonomyTagsDataLoaded', () => { | ||
it('should return true when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success' }); | ||
const taxonomyId = '123'; | ||
const result = useIsTaxonomyTagsDataLoaded(taxonomyId); | ||
describe('useTaxonomyTagsData', () => { | ||
it('should return success response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: true, data: 'data' }); | ||
const taxonomyId = 123; | ||
const result = useTaxonomyTagsData(taxonomyId); | ||
|
||
expect(result).toBe(true); | ||
expect(result).toEqual({ isSuccess: true, data: 'data' }); | ||
}); | ||
|
||
it('should return false when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
const taxonomyId = '123'; | ||
const result = useIsTaxonomyTagsDataLoaded(taxonomyId); | ||
it('should return failure response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: false }); | ||
const taxonomyId = 123; | ||
const result = useTaxonomyTagsData(taxonomyId); | ||
|
||
expect(result).toBe(false); | ||
expect(result).toEqual({ isSuccess: false }); | ||
}); | ||
}); | ||
|
||
describe('useContentTaxonomyTagsDataResponse', () => { | ||
it('should return data when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success', data: { data: 'data' } }); | ||
describe('useContentTaxonomyTagsData', () => { | ||
it('should return success response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: true, data: 'data' }); | ||
const contentId = '123'; | ||
const result = useContentTaxonomyTagsDataResponse(contentId); | ||
const result = useContentTaxonomyTagsData(contentId); | ||
|
||
expect(result).toEqual({ data: 'data' }); | ||
expect(result).toEqual({ isSuccess: true, data: 'data' }); | ||
}); | ||
|
||
it('should return undefined when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
it('should return failure response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: false }); | ||
const contentId = '123'; | ||
const result = useContentTaxonomyTagsDataResponse(contentId); | ||
const result = useContentTaxonomyTagsData(contentId); | ||
|
||
expect(result).toBeUndefined(); | ||
expect(result).toEqual({ isSuccess: false }); | ||
}); | ||
}); | ||
|
||
describe('useIsContentTaxonomyTagsDataLoaded', () => { | ||
it('should return true when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success' }); | ||
describe('useContentData', () => { | ||
it('should return success response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: true, data: 'data' }); | ||
const contentId = '123'; | ||
const result = useIsContentTaxonomyTagsDataLoaded(contentId); | ||
const result = useContentData(contentId); | ||
|
||
expect(result).toBe(true); | ||
expect(result).toEqual({ isSuccess: true, data: 'data' }); | ||
}); | ||
|
||
it('should return false when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
it('should return failure response', () => { | ||
useQuery.mockReturnValueOnce({ isSuccess: false }); | ||
const contentId = '123'; | ||
const result = useIsContentTaxonomyTagsDataLoaded(contentId); | ||
const result = useContentData(contentId); | ||
|
||
expect(result).toBe(false); | ||
expect(result).toEqual({ isSuccess: false }); | ||
}); | ||
}); | ||
|
||
describe('useContentDataResponse', () => { | ||
it('should return data when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success', data: { data: 'data' } }); | ||
const contentId = '123'; | ||
const result = useContentDataResponse(contentId); | ||
describe('useContentTaxonomyTagsMutation', () => { | ||
it('should call the update content taxonomy tags function', async () => { | ||
useMutation.mockReturnValueOnce({ mutate: jest.fn() }); | ||
|
||
expect(result).toEqual({ data: 'data' }); | ||
}); | ||
const mutation = useContentTaxonomyTagsMutation(); | ||
mutation.mutate(); | ||
|
||
it('should return undefined when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
const contentId = '123'; | ||
const result = useContentDataResponse(contentId); | ||
expect(useMutation).toBeCalled(); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('useIsContentDataLoaded', () => { | ||
it('should return true when status is success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'success' }); | ||
const contentId = '123'; | ||
const result = useIsContentDataLoaded(contentId); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false when status is not success', () => { | ||
useQuery.mockReturnValueOnce({ status: 'error' }); | ||
const contentId = '123'; | ||
const result = useIsContentDataLoaded(contentId); | ||
const [config] = useMutation.mock.calls[0]; | ||
const { mutationFn } = config; | ||
|
||
expect(result).toBe(false); | ||
await act(async () => { | ||
const contentId = 'testerContent'; | ||
const taxonomyId = 123; | ||
const tags = ['tag1', 'tag2']; | ||
await mutationFn({ contentId, taxonomyId, tags }); | ||
expect(updateContentTaxonomyTags).toBeCalledWith(contentId, taxonomyId, tags); | ||
}); | ||
}); | ||
}); |
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