From 0bf5a91091501b73c5bb1745e8b9f326239247da Mon Sep 17 00:00:00 2001 From: esvyridov Date: Tue, 23 Nov 2021 19:12:09 +0100 Subject: [PATCH] Add tests for updated getUserComments func --- frontend/app/common/api.test.ts | 31 +++++++++++++++++++++++++++++++ frontend/app/common/api.ts | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 frontend/app/common/api.test.ts diff --git a/frontend/app/common/api.test.ts b/frontend/app/common/api.test.ts new file mode 100644 index 00000000..3c9f6c99 --- /dev/null +++ b/frontend/app/common/api.test.ts @@ -0,0 +1,31 @@ +import { getUserComments } from './api'; +import { apiFetcher } from './fetcher'; + +describe('getUserComments', () => { + it('should call apiFetcher.get with /comments endpoint and default skip and limit query params', () => { + const apiFetcherSpy = jest.spyOn(apiFetcher, 'get'); + const userId = '1'; + + getUserComments(userId); + expect(apiFetcherSpy).toHaveBeenCalledWith('/comments', { + user: userId, + skip: 0, + limit: 10, + }); + }); + + it('should call apiFetcher.get with /comments endpoint and provided query params', () => { + const apiFetcherSpy = jest.spyOn(apiFetcher, 'get'); + const userId = '1'; + const config = { + skip: 10, + limit: 20, + }; + + getUserComments(userId, config); + expect(apiFetcherSpy).toHaveBeenCalledWith('/comments', { + user: userId, + ...config, + }); + }); +}); diff --git a/frontend/app/common/api.ts b/frontend/app/common/api.ts index 771809f8..8bbf0886 100644 --- a/frontend/app/common/api.ts +++ b/frontend/app/common/api.ts @@ -13,7 +13,7 @@ export const getComment = (id: Comment['id']): Promise => apiFetcher.ge export const getUserComments = ( userId: User['id'], - config: { skip?: number; limit?: number } = { skip: 0, limit: 10 } + config: { limit: number; skip?: number } = { limit: 10, skip: 0 } ): Promise<{ comments: Comment[]; count: number }> => apiFetcher.get('/comments', { user: userId, ...config }); export const putCommentVote = ({ id, value }: { id: Comment['id']; value: number }): Promise =>