Skip to content

Commit

Permalink
chore: fixed specs
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan committed Mar 2, 2023
1 parent d5996f2 commit de5b5d9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export default async (req: Request, res: Response) => {
* parent_category_id:
* type: string
* description: The ID of the parent product category
* position:
* type: number
* description: The position of the category in the tree node (starting from 0)
*/
// eslint-disable-next-line max-len
export class AdminPostProductCategoriesCategoryReq extends AdminProductCategoriesReqBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const productCategoryRepositoryMock = {
if (query.where.id === IdMap.getId(validProdCategoryIdWithChildren)) {
return Promise.resolve({
id: IdMap.getId(validProdCategoryIdWithChildren),
parent_category_id: null,
category_children: [{
id: IdMap.getId(validProdCategoryId),
}]
Expand All @@ -53,6 +54,7 @@ export const productCategoryRepositoryMock = {

return Promise.resolve({
id: IdMap.getId(validProdCategoryId),
parent_category_id: null,
category_children: []
})
},
Expand All @@ -74,10 +76,12 @@ export const productCategoryRepositoryMock = {

return Promise.resolve([{
id: IdMap.getId(validProdCategoryWithSiblings),
parent_category_id: null,
category_children: [],
position: 0
}, {
id: IdMap.getId(validProdCategoryId),
parent_category_id: null,
category_children: [],
position: 1
}])
Expand Down
20 changes: 17 additions & 3 deletions packages/medusa/src/services/__tests__/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ describe("ProductCategoryService", () => {
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith(
{
order: {
created_at: "DESC",
position: "ASC",
},
skip: 0,
take: 100,
where: {},
},
validID,
{}
{},
false,
)
})

Expand All @@ -89,7 +90,19 @@ describe("ProductCategoryService", () => {

expect(result[0].id).toEqual(validID)
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledTimes(1)
expect(productCategoryRepository.findDescendantsTree).toHaveBeenCalledTimes(1)
expect(productCategoryRepository.getFreeTextSearchResultsAndCount).toHaveBeenCalledWith(
{
order: {
position: "ASC",
},
skip: 0,
take: 100,
where: {},
},
undefined,
{},
true,
)
})
})

Expand Down Expand Up @@ -168,6 +181,7 @@ describe("ProductCategoryService", () => {
expect(productCategoryRepository.save).toBeCalledWith({
id: IdMap.getId(validProdCategoryId),
category_children: [],
parent_category_id: null,
position: 0,
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ProductCategoryService extends TransactionBaseService {
config: FindConfig<ProductCategory> = {
skip: 0,
take: 100,
order: { created_at: "DESC" },
order: { position: "ASC" },
},
treeSelector: QuerySelector<ProductCategory> = {}
): Promise<[ProductCategory[], number]> {
Expand Down Expand Up @@ -304,7 +304,7 @@ class ProductCategoryService extends TransactionBaseService {
shouldDeleteElement = false
): ReorderConditions {
const originalParentId = productCategory.parent_category_id
const targetParentId = input.parent_category_id || null
const targetParentId = input.parent_category_id
const originalPosition = productCategory.position
const targetPosition = input.position
const shouldChangeParent =
Expand Down Expand Up @@ -436,7 +436,7 @@ class ProductCategoryService extends TransactionBaseService {
// Depending on the condition, we could also have the targetCategory
// in the siblings list, we skip shifting the target until all other siblings
// have been shifted.
if (targetCategoryId === sibling.id) {
if (sibling.id === targetCategoryId) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/types/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ProductBatchProductCategory {
export type ReorderConditions = {
targetCategoryId: string
originalParentId: string | null
targetParentId: string | null
targetParentId: string | null | undefined
originalPosition: number
targetPosition: number | undefined
shouldChangeParent: boolean
Expand Down

0 comments on commit de5b5d9

Please sign in to comment.