fix: storefront product filtering (#1189)

* fix: allow multiple ids in list + expand, fields param

* fix: add filtering by title

* fix: adds integration test

* fix: adds integration test of product variant filtering

* fix: integration tests

* fix: unit tests

* fix: refactor query param parsing
This commit is contained in:
Sebastian Rindom
2022-03-17 23:28:15 +01:00
committed by GitHub
parent 4be991c156
commit e3655b53f7
12 changed files with 324 additions and 28 deletions
@@ -111,3 +111,60 @@ Object {
],
}
`;
exports[`/store/variants lists by title 1`] = `
Object {
"variants": Array [
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": null,
"height": null,
"hs_code": null,
"id": Any<String>,
"inventory_quantity": 12,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"options": Array [
Object {
"created_at": Any<String>,
"deleted_at": null,
"id": Any<String>,
"metadata": null,
"option_id": Any<String>,
"updated_at": Any<String>,
"value": "Handcrafted",
"variant_id": Any<String>,
},
],
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": Any<String>,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": Any<String>,
},
],
"product": Any<Object>,
"product_id": Any<String>,
"sku": null,
"title": "test2",
"upc": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
}
`;
@@ -280,3 +280,56 @@ Object {
},
}
`;
exports[`/store/products list params works with expand and fields 1`] = `
Object {
"count": 2,
"limit": 1,
"offset": 0,
"products": Array [
Object {
"id": Any<String>,
"title": "testprod",
"variants": Array [
Object {
"allow_backorder": false,
"barcode": null,
"created_at": Any<String>,
"deleted_at": null,
"ean": null,
"height": null,
"hs_code": null,
"id": Any<String>,
"inventory_quantity": 10,
"length": null,
"manage_inventory": true,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"prices": Array [
Object {
"amount": 100,
"created_at": Any<String>,
"currency_code": "usd",
"deleted_at": null,
"id": Any<String>,
"region_id": null,
"sale_amount": null,
"updated_at": Any<String>,
"variant_id": Any<String>,
},
],
"product_id": Any<String>,
"sku": null,
"title": "test-variant",
"upc": null,
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
},
],
}
`;
@@ -2,6 +2,7 @@ const path = require("path")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const { simpleProductFactory } = require("../../factories")
const productSeeder = require("../../helpers/product-seeder")
jest.setTimeout(30000)
@@ -24,6 +25,24 @@ describe("/store/variants", () => {
beforeEach(async () => {
try {
await productSeeder(dbConnection)
await simpleProductFactory(
dbConnection,
{
title: "prod",
variants: [
{
title: "test1",
inventory_quantity: 10,
},
{
title: "test2",
inventory_quantity: 12,
},
],
},
100
)
} catch (err) {
console.log(err)
throw err
@@ -93,6 +112,43 @@ describe("/store/variants", () => {
})
})
it("lists by title", async () => {
const api = useApi()
const response = await api.get(
"/store/variants?title[]=test1&title[]=test2&inventory_quantity[gt]=10"
)
expect(response.data).toMatchSnapshot({
variants: [
{
id: expect.any(String),
title: "test2",
created_at: expect.any(String),
updated_at: expect.any(String),
options: [
{
created_at: expect.any(String),
updated_at: expect.any(String),
id: expect.any(String),
option_id: expect.any(String),
variant_id: expect.any(String),
},
],
prices: [
{
id: expect.any(String),
variant_id: expect.any(String),
created_at: expect.any(String),
updated_at: expect.any(String),
},
],
product: expect.any(Object),
product_id: expect.any(String),
},
],
})
})
it("/test-variant", async () => {
const api = useApi()
@@ -4,6 +4,7 @@ const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const { simpleProductFactory } = require("../../factories")
const productSeeder = require("../../helpers/store-product-seeder")
const adminSeeder = require("../../helpers/admin-seeder")
jest.setTimeout(30000)
@@ -222,6 +223,74 @@ describe("/store/products", () => {
})
})
describe("list params", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
const p1 = await simpleProductFactory(
dbConnection,
{
title: "testprod",
status: "published",
variants: [{ title: "test-variant" }],
},
11
)
const p2 = await simpleProductFactory(
dbConnection,
{
title: "testprod3",
status: "published",
variants: [{ title: "test-variant1" }],
},
12
)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("works with expand and fields", async () => {
const api = useApi()
const response = await api.get(
"/store/products?expand=variants,variants.prices&fields=id,title&limit=1"
)
expect(response.data).toMatchSnapshot({
products: [
{
id: expect.any(String),
variants: [
{
created_at: expect.any(String),
updated_at: expect.any(String),
id: expect.any(String),
product_id: expect.any(String),
prices: [
{
created_at: expect.any(String),
updated_at: expect.any(String),
id: expect.any(String),
variant_id: expect.any(String),
},
],
},
],
},
],
})
})
})
describe("/store/products/:id", () => {
beforeEach(async () => {
try {
@@ -16,6 +16,7 @@ import {
export type ProductFactoryData = {
id?: string
is_giftcard?: boolean
status?: string
title?: string
type?: string
options?: { id: string; title: string }[]
@@ -54,6 +55,7 @@ export const simpleProductFactory = async (
const toSave = manager.create(Product, {
id: prodId,
type_id: typeId,
status: data.status,
title: data.title || faker.commerce.productName(),
is_giftcard: data.is_giftcard || false,
discountable: !data.is_giftcard,