generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hook for download & item like
fix: changes by review
- Loading branch information
1 parent
af2487b
commit ce86263
Showing
18 changed files
with
537 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { QueryClientConfig, UUID } from '../types'; | ||
import configureAxios, { verifyAuthentication } from './axios'; | ||
import { buildDownloadItemRoute } from './routes'; | ||
|
||
const axios = configureAxios(); | ||
|
||
/* eslint-disable import/prefer-default-export */ | ||
export const downloadItem = async (id: UUID, { API_HOST }: QueryClientConfig) => | ||
verifyAuthentication(() => | ||
axios({ | ||
url: `${API_HOST}/${buildDownloadItemRoute(id)}`, | ||
method: 'GET', | ||
responseType: 'blob', | ||
}).then(({ data }) => data), | ||
); |
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,49 @@ | ||
import { List } from 'immutable'; | ||
import { | ||
buildDeleteItemLikeRoute, | ||
buildGetLikeCountRoute, | ||
buildGetLikedItemsRoute, | ||
buildPostItemLikeRoute, | ||
} from './routes'; | ||
import { QueryClientConfig, UUID } from '../types'; | ||
import configureAxios, { verifyAuthentication } from './axios'; | ||
|
||
const axios = configureAxios(); | ||
|
||
export const getLikedItems = async ( | ||
memberId: UUID, | ||
{ API_HOST }: QueryClientConfig, | ||
) => | ||
verifyAuthentication(() => | ||
axios | ||
.get(`${API_HOST}/${buildGetLikedItemsRoute(memberId)}`) | ||
.then(({ data }) => List(data)), | ||
); | ||
|
||
// TODO: make a public one | ||
export const getLikeCount = async (id: UUID, { API_HOST }: QueryClientConfig) => | ||
verifyAuthentication(() => | ||
axios | ||
.get(`${API_HOST}/${buildGetLikeCountRoute(id)}`) | ||
.then(({ data }) => data), | ||
); | ||
|
||
export const postItemLike = async ( | ||
itemId: UUID, | ||
{ API_HOST }: QueryClientConfig, | ||
) => | ||
verifyAuthentication(() => | ||
axios | ||
.post(`${API_HOST}/${buildPostItemLikeRoute(itemId)}`) | ||
.then(({ data }) => data), | ||
); | ||
|
||
export const deleteItemLike = async ( | ||
id: UUID, | ||
{ API_HOST }: QueryClientConfig, | ||
) => | ||
verifyAuthentication(() => | ||
axios | ||
.delete(`${API_HOST}/${buildDeleteItemLikeRoute(id)}`) | ||
.then(({ data }) => data), | ||
); |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import nock from 'nock'; | ||
import Cookies from 'js-cookie'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { List } from 'immutable'; | ||
import { buildGetLikeCountRoute, buildGetLikedItemsRoute } from '../api/routes'; | ||
import { mockHook, setUpTest } from '../../test/utils'; | ||
import { ITEMS, LIKE_COUNT, ITEM_LIKES, UNAUTHORIZED_RESPONSE } from '../../test/constants'; | ||
import { buildGetLikeCountKey, buildGetLikedItemsKey } from '../config/keys'; | ||
|
||
const { hooks, wrapper, queryClient } = setUpTest(); | ||
jest.spyOn(Cookies, 'get').mockReturnValue({ session: 'somesession' }); | ||
|
||
describe('Item Like Hooks', () => { | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
queryClient.clear(); | ||
}); | ||
|
||
describe('useLikedItems', () => { | ||
const memberId = 'member-id'; | ||
const route = `/${buildGetLikedItemsRoute(memberId)}`; | ||
const key = buildGetLikedItemsKey(memberId); | ||
|
||
const hook = () => hooks.useLikedItems(memberId); | ||
|
||
it(`Receive item likes`, async () => { | ||
const response = ITEM_LIKES; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as List<typeof ITEM_LIKES[0]>).toJS()).toEqual(response); | ||
|
||
// verify cache keys | ||
expect(queryClient.getQueryData(key)).toEqual(List(response)); | ||
}); | ||
|
||
it(`Unauthorized`, async () => { | ||
const endpoints = [ | ||
{ | ||
route, | ||
response: UNAUTHORIZED_RESPONSE, | ||
statusCode: StatusCodes.UNAUTHORIZED, | ||
}, | ||
]; | ||
const { data, isError } = await mockHook({ | ||
hook, | ||
wrapper, | ||
endpoints, | ||
}); | ||
|
||
expect(data).toBeFalsy(); | ||
expect(isError).toBeTruthy(); | ||
// verify cache keys | ||
expect(queryClient.getQueryData(key)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('useLikeCount', () => { | ||
const itemId = ITEMS[0].id; | ||
const route = `/${buildGetLikeCountRoute(itemId)}`; | ||
const key = buildGetLikeCountKey(itemId); | ||
|
||
const hook = () => hooks.useLikeCount(itemId); | ||
|
||
it(`Receive item like count`, async () => { | ||
const response = LIKE_COUNT; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect(data as typeof LIKE_COUNT).toEqual(response); | ||
|
||
// verify cache keys | ||
expect(queryClient.getQueryData(key)).toEqual(response); | ||
}); | ||
|
||
it(`Unauthorized`, async () => { | ||
const endpoints = [ | ||
{ | ||
route, | ||
response: UNAUTHORIZED_RESPONSE, | ||
statusCode: StatusCodes.UNAUTHORIZED, | ||
}, | ||
]; | ||
const { data, isError } = await mockHook({ | ||
hook, | ||
wrapper, | ||
endpoints, | ||
}); | ||
|
||
expect(data).toBeFalsy(); | ||
expect(isError).toBeTruthy(); | ||
// verify cache keys | ||
expect(queryClient.getQueryData(key)).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
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,33 @@ | ||
import { useQuery } from 'react-query'; | ||
import { List } from 'immutable'; | ||
import { QueryClientConfig, UUID } from '../types'; | ||
import * as Api from '../api'; | ||
import { buildGetLikeCountKey, buildGetLikedItemsKey } from '../config/keys'; | ||
|
||
export default (queryConfig: QueryClientConfig) => { | ||
const { retry, cacheTime, staleTime } = queryConfig; | ||
const defaultOptions = { | ||
retry, | ||
cacheTime, | ||
staleTime, | ||
}; | ||
|
||
const useLikedItems = (memberId: UUID) => | ||
useQuery({ | ||
queryKey: buildGetLikedItemsKey(memberId), | ||
queryFn: () => | ||
Api.getLikedItems(memberId, queryConfig).then((data) => List(data)), | ||
...defaultOptions, | ||
enabled: Boolean(memberId), | ||
}); | ||
|
||
const useLikeCount = (itemId: UUID) => | ||
useQuery({ | ||
queryKey: buildGetLikeCountKey(itemId), | ||
queryFn: () => Api.getLikeCount(itemId, queryConfig).then((data) => data), | ||
...defaultOptions, | ||
enabled: Boolean(itemId), | ||
}); | ||
|
||
return { useLikeCount, useLikedItems }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import nock from 'nock'; | ||
import Cookies from 'js-cookie'; | ||
import { act } from 'react-test-renderer'; | ||
import { mockMutation, setUpTest, waitForMutation } from '../../test/utils'; | ||
import { REQUEST_METHODS } from '../api/utils'; | ||
import { MUTATION_KEYS } from '../config/keys'; | ||
import { buildDownloadItemRoute } from '../api/routes'; | ||
import { downloadItemRoutine } from '../routines'; | ||
|
||
const mockedNotifier = jest.fn(); | ||
const { wrapper, queryClient, useMutation } = setUpTest({ | ||
notifier: mockedNotifier, | ||
}); | ||
|
||
jest.spyOn(Cookies, 'get').mockReturnValue({ session: 'somesession' }); | ||
|
||
describe('Download Item', () => { | ||
afterEach(() => { | ||
queryClient.clear(); | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe(MUTATION_KEYS.EXPORT_ZIP, () => { | ||
const itemId = 'item-id'; | ||
const route = `/${buildDownloadItemRoute(itemId)}`; | ||
const mutation = () => useMutation(MUTATION_KEYS.EXPORT_ZIP); | ||
|
||
it('download item', async () => { | ||
const endpoints = [ | ||
{ | ||
response: { id: 'id', content: 'content' }, | ||
method: REQUEST_METHODS.GET, | ||
route, | ||
}, | ||
]; | ||
|
||
const mockedMutation = await mockMutation({ | ||
endpoints, | ||
mutation, | ||
wrapper, | ||
}); | ||
|
||
await act(async () => { | ||
await mockedMutation.mutate(itemId); | ||
await waitForMutation(); | ||
}); | ||
|
||
expect(mockedNotifier).toHaveBeenCalledWith({ | ||
type: downloadItemRoutine.SUCCESS, | ||
}); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { QueryClient } from 'react-query'; | ||
import * as Api from '../api'; | ||
import { MUTATION_KEYS } from '../config/keys'; | ||
import { downloadItemRoutine } from '../routines'; | ||
import { QueryClientConfig } from '../types'; | ||
|
||
export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => { | ||
const { notifier } = queryConfig; | ||
|
||
queryClient.setMutationDefaults(MUTATION_KEYS.EXPORT_ZIP, { | ||
mutationFn: (id) => | ||
Api.downloadItem(id, queryConfig).then((data) => data), | ||
onSuccess: () => { | ||
notifier?.({ type: downloadItemRoutine.SUCCESS }); | ||
}, | ||
onError: (error) => { | ||
notifier?.({ type: downloadItemRoutine.FAILURE, payload: { error } }); | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.