This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(product): add sorting and pagination draft, fix(product): fixes …
…after CR
- Loading branch information
Maciej Kucmus
committed
Oct 15, 2019
1 parent
d86bad2
commit 5c4e7c0
Showing
3 changed files
with
140 additions
and
19 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/shopware-6-client/__tests__/services/productService.spec.ts
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,80 @@ | ||
import { ProductService } from "../../src/index"; | ||
|
||
describe("ProductService", () => { | ||
describe("getProductsIds", () => { | ||
it("should return array of products' ids (default amount of 10)", async () => { | ||
try { | ||
const result = await ProductService.getProductsIds(); | ||
expect(result.total).toEqual(60); | ||
expect(result.data).toHaveLength(10); | ||
} catch (e) { | ||
console.error("Connection problem", e); | ||
} | ||
}); | ||
}); | ||
describe("getProducts", () => { | ||
it("should return array of products (default amount of 10)", async () => { | ||
try { | ||
const result = await ProductService.getProducts(); | ||
expect(result.total).toEqual(60); | ||
expect(result.data).toHaveLength(10); | ||
} catch (e) { | ||
console.error("Connection problem", e); | ||
} | ||
}); | ||
it("should return array of products limited to 5", async () => { | ||
try { | ||
const pagination = { | ||
page: 1, | ||
limit: 5 | ||
}; | ||
const result = await ProductService.getProducts(pagination); | ||
expect(result.total).toEqual(60); | ||
expect(result.data).toHaveLength(5); | ||
} catch (e) { | ||
console.error("Connection problem", e); | ||
} | ||
}); | ||
it("should return a different array of products sorted by name", async () => { | ||
try { | ||
const pagination = { | ||
page: 1, | ||
limit: 1 | ||
}; | ||
|
||
/** get the products with descending order to compare */ | ||
const sort = { | ||
sort: `-name` | ||
}; | ||
const result = await ProductService.getProducts(pagination, sort); | ||
expect(result.data).toHaveLength(1); | ||
const nameDesc = result.data[0].name; | ||
|
||
/** get the products with ascending order to compare */ | ||
const sortAsc = { | ||
sort: `name` | ||
}; | ||
const resultAsc = await ProductService.getProducts(pagination, sortAsc); | ||
expect(resultAsc.data).toHaveLength(1); | ||
const nameAsc = resultAsc.data[0].name; | ||
|
||
/** compare first results's name from different sorting order */ | ||
expect(nameAsc).not.toBe(nameDesc); | ||
} catch (e) { | ||
console.error("Connection problem", e); | ||
} | ||
}); | ||
}); | ||
describe("getProduct", () => { | ||
it("should return chosen product", async () => { | ||
try { | ||
const productId = "044a190a54ab4f06803909c3ee8063ef"; | ||
const result = await ProductService.getProduct(productId); | ||
expect(result).toHaveProperty("id"); | ||
expect(result.id).toEqual(productId); | ||
} catch (e) { | ||
console.error("Connection problem", e); | ||
} | ||
}); | ||
}); | ||
}); |
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,38 @@ | ||
export interface Params { | ||
page?: number; | ||
limit?: number; | ||
sort?: string; | ||
filter?: any; | ||
} | ||
|
||
export interface ParamsConverter { | ||
getParams: (pagination?: any, sort?: any, filter?: any) => Params | null; | ||
} | ||
/** | ||
* @description Combines parameters into one object | ||
*/ | ||
const getParams = ( | ||
pagination?: any, | ||
sort?: any, | ||
filter?: any | ||
): Params | null => { | ||
let params = {}; | ||
|
||
if (pagination) { | ||
params = Object.assign(params, pagination); | ||
} | ||
|
||
if (sort) { | ||
params = Object.assign(params, sort); | ||
} | ||
|
||
if (filter) { | ||
params = Object.assign(params, filter); | ||
} | ||
|
||
return pagination || sort || filter ? params : null; | ||
}; | ||
|
||
export const ParamsConverter: ParamsConverter = { | ||
getParams | ||
}; |
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