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.
Merge pull request #90 from graasp/89/category
close ref #89
- Loading branch information
Showing
17 changed files
with
2,252 additions
and
1,683 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,5 @@ example/src/react-app-env.d.ts | |
!.yarn/releases | ||
!.yarn/sdks | ||
!.yarn/versions | ||
|
||
package-lock.json |
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,67 @@ | ||
import { QueryClientConfig, UUID} from '../types'; | ||
import { buildGetCategoriesRoute, buildGetCategoryInfoRoute, buildGetItemCategoriesRoute, | ||
buildGetItemsInCategoryRoute, buildPostItemCategoryRoute, buildDeleteItemCategoryRoute, GET_CATEGORY_TYPES_ROUTE } from './routes'; | ||
import { DEFAULT_DELETE, DEFAULT_GET, DEFAULT_POST, failOnError } from './utils'; | ||
|
||
export const getCategoryTypes = async ({ API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${GET_CATEGORY_TYPES_ROUTE}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const getCategories = async ({ API_HOST }: QueryClientConfig, typeIds?: UUID[], ) => { | ||
const res = await fetch(`${API_HOST}/${buildGetCategoriesRoute(typeIds)}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const getCategory = async (categoryId: UUID, { API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${buildGetCategoryInfoRoute(categoryId)}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const getItemCategories = async (itemId: UUID, { API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${buildGetItemCategoriesRoute(itemId)}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
return res.json(); | ||
}; | ||
|
||
export const getItemsForCategories = async (categoryIds: UUID[], { API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${buildGetItemsInCategoryRoute(categoryIds)}`, DEFAULT_GET).then( | ||
failOnError, | ||
); | ||
|
||
return res.json(); | ||
}; | ||
|
||
// payload: itemId, categoryId | ||
export const postItemCategory = async ( | ||
{ | ||
itemId, | ||
categoryId, | ||
}: { itemId: UUID; categoryId: UUID }, | ||
{ API_HOST }: QueryClientConfig, | ||
) => { | ||
const res = await fetch(`${API_HOST}/${buildPostItemCategoryRoute(itemId)}`, { | ||
...DEFAULT_POST, | ||
body: JSON.stringify({ itemId, categoryId }), | ||
}).then(failOnError); | ||
|
||
return res.json(); | ||
}; | ||
|
||
export const deleteItemCategory = async (entryId: UUID, { API_HOST }: QueryClientConfig) => { | ||
const res = await fetch(`${API_HOST}/${buildDeleteItemCategoryRoute(entryId)}`, { | ||
...DEFAULT_DELETE, | ||
}).then(failOnError); | ||
|
||
return res.json(); | ||
}; |
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,210 @@ | ||
import nock from 'nock'; | ||
import { List } from 'immutable'; | ||
import { mockHook, setUpTest } from '../../test/utils'; | ||
import { buildGetCategoriesRoute, buildGetCategoryInfoRoute, buildGetItemCategoriesRoute, buildGetItemsInCategoryRoute, GET_CATEGORY_TYPES_ROUTE } from '../api/routes'; | ||
import { buildCategoriesKey, buildCategoryKey, buildItemCategoriesKey, buildItemsByCategoriesKey, CATEGORY_TYPES_KEY } from '../config/keys'; | ||
import { CATEGORIES, CATEGORY_TYPES, ITEM_CATEGORIES, UNAUTHORIZED_RESPONSE } from '../../test/constants'; | ||
import { Category, CategoryType, ItemCategory } from '../types'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
const { hooks, wrapper, queryClient } = setUpTest(); | ||
|
||
type ItemId = { | ||
itemId: string, | ||
} | ||
|
||
describe('Category Hooks', () => { | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
queryClient.clear(); | ||
}); | ||
|
||
describe('useCategoryTypes', () => { | ||
const route = `/${GET_CATEGORY_TYPES_ROUTE}`; | ||
const key = CATEGORY_TYPES_KEY; | ||
|
||
const hook = () => hooks.useCategoryTypes(); | ||
|
||
it(`Receive category types`, async () => { | ||
const response = CATEGORY_TYPES; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as List<CategoryType>).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('useCategories', () => { | ||
const typeIds = ['type-id1', 'type-id2']; | ||
const route = `/${buildGetCategoriesRoute(typeIds)}`; | ||
const key = buildCategoriesKey(typeIds); | ||
|
||
const hook = () => hooks.useCategories(typeIds); | ||
|
||
it(`Receive category types`, async () => { | ||
const response = CATEGORIES; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as List<Category>).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('useCategory', () => { | ||
const categoryId = 'id'; | ||
const route = `/${buildGetCategoryInfoRoute(categoryId)}`; | ||
const key = buildCategoryKey(categoryId); | ||
|
||
const hook = () => hooks.useCategory(categoryId); | ||
|
||
it(`Receive category info`, async () => { | ||
const response = CATEGORIES[0]; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as Category)).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(); | ||
}); | ||
}); | ||
|
||
describe('useItemCategories', () => { | ||
const itemId = 'item-id'; | ||
const route = `/${buildGetItemCategoriesRoute(itemId)}`; | ||
const key = buildItemCategoriesKey(itemId); | ||
|
||
const hook = () => hooks.useItemCategories(itemId); | ||
|
||
it(`Receive item categories`, async () => { | ||
const response = ITEM_CATEGORIES; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as List<ItemCategory>).toJS()).toEqual(response); | ||
|
||
// verify cache keys | ||
expect((queryClient.getQueryData(key) as List<ItemCategory>).toJS()).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(); | ||
}); | ||
}); | ||
|
||
describe('useItemsInCategories', () => { | ||
const categoryIds = ['id1']; | ||
const route = `/${buildGetItemsInCategoryRoute(categoryIds)}`; | ||
const key = buildItemsByCategoriesKey(categoryIds); | ||
|
||
const hook = () => hooks.useItemsInCategories(categoryIds); | ||
|
||
it(`Receive items in categories`, async () => { | ||
const response = [{itemId: 'id1'}, {itemId: 'id2'}]; | ||
const endpoints = [{ route, response }]; | ||
const { data } = await mockHook({ endpoints, hook, wrapper }); | ||
|
||
expect((data as List<ItemId>).toJS()).toEqual(response); | ||
|
||
// verify cache keys | ||
expect((queryClient.getQueryData(key) as List<ItemId>)?.toJS()).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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.