feat: Add collection and category endpoints to store (#7155)
* feat: Add collection endpoints to store * feat: Add category store endpoints
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`/store/collections /store/collections lists collections 1`] = `
|
||||
Object {
|
||||
"collections": Array [
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection2",
|
||||
"id": "test-collection2",
|
||||
"metadata": null,
|
||||
"title": "Test collection 2",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection1",
|
||||
"id": "test-collection1",
|
||||
"metadata": null,
|
||||
"title": "Test collection 1",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection",
|
||||
"id": "test-collection",
|
||||
"metadata": null,
|
||||
"title": "Test collection",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
],
|
||||
"count": 3,
|
||||
"limit": 10,
|
||||
"offset": 0,
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`/store/collections /store/collections/:id gets collection 1`] = `
|
||||
Object {
|
||||
"collection": Object {
|
||||
"created_at": Any<String>,
|
||||
"deleted_at": null,
|
||||
"handle": "test-collection",
|
||||
"id": "test-collection",
|
||||
"metadata": null,
|
||||
"title": "Test collection",
|
||||
"updated_at": Any<String>,
|
||||
},
|
||||
}
|
||||
`;
|
||||
@@ -1,90 +0,0 @@
|
||||
const { ProductCollection } = require("@medusajs/medusa")
|
||||
const path = require("path")
|
||||
const setupServer = require("../../../environment-helpers/setup-server")
|
||||
const { useApi } = require("../../../environment-helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../environment-helpers/use-db")
|
||||
|
||||
const productSeeder = require("../../../helpers/product-seeder")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
describe("/store/collections", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
dbConnection = await initDb({ cwd })
|
||||
medusaProcess = await setupServer({ cwd })
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
describe("/store/collections/:id", () => {
|
||||
beforeEach(async () => {
|
||||
await productSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("gets collection", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/collections/test-collection")
|
||||
|
||||
expect(response.data).toMatchSnapshot({
|
||||
collection: {
|
||||
id: "test-collection",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("/store/collections", () => {
|
||||
beforeEach(async () => {
|
||||
await productSeeder(dbConnection)
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("lists collections", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/collections")
|
||||
|
||||
expect(response.data).toMatchSnapshot({
|
||||
collections: [
|
||||
{
|
||||
id: "test-collection2",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
},
|
||||
{
|
||||
id: "test-collection1",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
},
|
||||
{
|
||||
id: "test-collection",
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
},
|
||||
],
|
||||
count: 3,
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,92 @@
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import {
|
||||
createAdminUser,
|
||||
adminHeaders,
|
||||
} from "../../../helpers/create-admin-user"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
medusaIntegrationTestRunner({
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
let baseCollection
|
||||
let baseCollection1
|
||||
let baseCollection2
|
||||
|
||||
beforeEach(async () => {
|
||||
const container = getContainer()
|
||||
await createAdminUser(dbConnection, adminHeaders, container)
|
||||
|
||||
baseCollection = (
|
||||
await api.post(
|
||||
"/admin/collections",
|
||||
{ title: "test-collection" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.collection
|
||||
|
||||
baseCollection1 = (
|
||||
await api.post(
|
||||
"/admin/collections",
|
||||
{ title: "test-collection1" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.collection
|
||||
|
||||
baseCollection2 = (
|
||||
await api.post(
|
||||
"/admin/collections",
|
||||
{ title: "test-collection2" },
|
||||
adminHeaders
|
||||
)
|
||||
).data.collection
|
||||
})
|
||||
|
||||
describe("/store/collections", () => {
|
||||
describe("/store/collections/:id", () => {
|
||||
it("gets collection", async () => {
|
||||
const response = await api.get(
|
||||
`/store/collections/${baseCollection.id}`
|
||||
)
|
||||
|
||||
expect(response.data.collection).toEqual(
|
||||
expect.objectContaining({
|
||||
id: baseCollection.id,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
})
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("/store/collections", () => {
|
||||
it("lists collections", async () => {
|
||||
const response = await api.get("/store/collections")
|
||||
|
||||
expect(response.data).toEqual({
|
||||
collections: [
|
||||
expect.objectContaining({
|
||||
id: baseCollection2.id,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: baseCollection1.id,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: baseCollection.id,
|
||||
created_at: expect.any(String),
|
||||
updated_at: expect.any(String),
|
||||
}),
|
||||
],
|
||||
count: 3,
|
||||
limit: 10,
|
||||
offset: 0,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -1,358 +1,368 @@
|
||||
import {ProductCategory} from "@medusajs/medusa"
|
||||
import path from "path"
|
||||
|
||||
import startServerWithEnvironment
|
||||
from "../../../environment-helpers/start-server-with-environment"
|
||||
import {useApi} from "../../../environment-helpers/use-api"
|
||||
import {useDb} from "../../../environment-helpers/use-db"
|
||||
import {simpleProductCategoryFactory} from "../../../factories"
|
||||
import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
||||
import {
|
||||
adminHeaders,
|
||||
createAdminUser,
|
||||
} from "../../../helpers/create-admin-user"
|
||||
import { breaking } from "../../../helpers/breaking"
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/store/product-categories", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
let productCategory!: ProductCategory
|
||||
let productCategory2!: ProductCategory
|
||||
let productCategoryChild!: ProductCategory
|
||||
let productCategoryParent!: ProductCategory
|
||||
let productCategoryChild2!: ProductCategory
|
||||
let productCategoryChild3!: ProductCategory
|
||||
let productCategoryChild4!: ProductCategory
|
||||
medusaIntegrationTestRunner({
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||
testSuite: ({ dbConnection, getContainer, api }) => {
|
||||
let productCategoryParent
|
||||
let productCategory
|
||||
let productCategoryChild
|
||||
let productCategoryChild2
|
||||
let productCategoryChild3
|
||||
let productCategoryChild4
|
||||
|
||||
beforeAll(async () => {
|
||||
const cwd = path.resolve(path.join(__dirname, "..", ".."))
|
||||
const [process, connection] = await startServerWithEnvironment({
|
||||
cwd,
|
||||
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
|
||||
})
|
||||
dbConnection = connection
|
||||
medusaProcess = process
|
||||
})
|
||||
beforeEach(async () => {
|
||||
const container = getContainer()
|
||||
await createAdminUser(dbConnection, adminHeaders, container)
|
||||
|
||||
afterAll(async () => {
|
||||
const db = useDb()
|
||||
await db.shutdown()
|
||||
|
||||
medusaProcess.kill()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
productCategoryParent = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category parent",
|
||||
description: "test description",
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
productCategory = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category",
|
||||
parent_category: productCategoryParent,
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
productCategoryChild = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category child",
|
||||
parent_category: productCategory,
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
rank: 3,
|
||||
})
|
||||
|
||||
productCategoryChild2 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category child 2",
|
||||
parent_category: productCategory,
|
||||
is_internal: true,
|
||||
is_active: true,
|
||||
rank: 0,
|
||||
})
|
||||
|
||||
productCategoryChild3 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category child 3",
|
||||
parent_category: productCategory,
|
||||
is_active: false,
|
||||
is_internal: false,
|
||||
rank: 1,
|
||||
})
|
||||
|
||||
productCategoryChild4 = await simpleProductCategoryFactory(dbConnection, {
|
||||
name: "category child 4",
|
||||
parent_category: productCategory,
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
rank: 2,
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /store/product-categories/:id", () => {
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
})
|
||||
|
||||
it("gets product category with children tree and parent", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/store/product-categories/${productCategory.id}?fields=handle,name,description`
|
||||
)
|
||||
|
||||
expect(response.data.product_category).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
handle: productCategory.handle,
|
||||
name: productCategory.name,
|
||||
description: "",
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
handle: productCategoryParent.handle,
|
||||
name: productCategoryParent.name,
|
||||
productCategoryParent = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category parent",
|
||||
description: "test description",
|
||||
}),
|
||||
category_children: [
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategory = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category",
|
||||
parent_category_id: productCategoryParent.id,
|
||||
is_active: true,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
// The order in which the children are created is intentional as in v1 there was no way to explicitly set the rank.
|
||||
productCategoryChild2 = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category child 2",
|
||||
parent_category_id: productCategory.id,
|
||||
is_internal: true,
|
||||
is_active: true,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategoryChild3 = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category child 3",
|
||||
parent_category_id: productCategory.id,
|
||||
is_internal: false,
|
||||
is_active: false,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategoryChild4 = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category child 4",
|
||||
parent_category_id: productCategory.id,
|
||||
is_internal: false,
|
||||
is_active: true,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
|
||||
productCategoryChild = (
|
||||
await api.post(
|
||||
"/admin/product-categories",
|
||||
{
|
||||
name: "category child",
|
||||
parent_category_id: productCategory.id,
|
||||
is_active: true,
|
||||
is_internal: false,
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
).data.product_category
|
||||
})
|
||||
|
||||
describe("/store/product-categories", () => {
|
||||
describe("GET /store/product-categories/:id", () => {
|
||||
it("gets product category with children tree and parent", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories/${productCategory.id}?${breaking(
|
||||
() => "fields=handle,name,description",
|
||||
() => "include_ancestors_tree=true&include_descendants_tree=true"
|
||||
)}`
|
||||
)
|
||||
|
||||
expect(response.data.product_category).toEqual(
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
handle: productCategoryChild4.handle,
|
||||
name: productCategoryChild4.name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
handle: productCategoryChild.handle,
|
||||
name: productCategoryChild.name,
|
||||
}),
|
||||
],
|
||||
id: productCategory.id,
|
||||
handle: productCategory.handle,
|
||||
name: productCategory.name,
|
||||
description: "",
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
handle: productCategoryParent.handle,
|
||||
name: productCategoryParent.name,
|
||||
description: "test description",
|
||||
}),
|
||||
category_children: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
handle: productCategoryChild4.handle,
|
||||
name: productCategoryChild4.name,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
handle: productCategoryChild.handle,
|
||||
name: productCategoryChild.name,
|
||||
}),
|
||||
]),
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
})
|
||||
// TODO: This one is failing since we don't validate allowed fields currently. We should add that as part of our validators
|
||||
it("throws error on querying not allowed fields", async () => {
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategory.id}?fields=mpath`)
|
||||
.catch((e) => e)
|
||||
|
||||
it("throws error on querying not allowed fields", async () => {
|
||||
const api = useApi()
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"Requested fields [mpath] are not valid"
|
||||
)
|
||||
})
|
||||
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategory.id}?fields=mpath`)
|
||||
.catch((e) => e)
|
||||
it("throws error on querying for internal product category", async () => {
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategoryChild2.id}`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"Requested fields [mpath] are not valid"
|
||||
)
|
||||
})
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
breaking(
|
||||
() =>
|
||||
`ProductCategory with id: ${productCategoryChild2.id}, is_internal: false, is_active: true was not found`,
|
||||
() =>
|
||||
`Product category with id: ${productCategoryChild2.id} was not found`
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error on querying for internal product category", async () => {
|
||||
const api = useApi()
|
||||
it("throws error on querying for inactive product category", async () => {
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategoryChild3.id}`)
|
||||
.catch((e) => e)
|
||||
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategoryChild2.id}`)
|
||||
.catch((e) => e)
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
breaking(
|
||||
() =>
|
||||
`ProductCategory with id: ${productCategoryChild3.id}, is_internal: false, is_active: true was not found`,
|
||||
() =>
|
||||
`Product category with id: ${productCategoryChild3.id} was not found`
|
||||
)
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
`ProductCategory with id: ${productCategoryChild2.id}, is_internal: false, is_active: true was not found`
|
||||
)
|
||||
})
|
||||
describe("GET /store/product-categories", () => {
|
||||
//TODO: The listing results in V2 are unexpected and differ from v1, we need to investigate where the issue is
|
||||
it("gets list of product category with immediate children and parents", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories?limit=10${breaking(
|
||||
() => "",
|
||||
() => "&include_ancestors_tree=true&include_descendants_tree=true"
|
||||
)}`
|
||||
)
|
||||
|
||||
it("throws error on querying for inactive product category", async () => {
|
||||
const api = useApi()
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(4)
|
||||
expect(response.data.offset).toEqual(0)
|
||||
expect(response.data.limit).toEqual(10)
|
||||
|
||||
const error = await api
|
||||
.get(`/store/product-categories/${productCategoryChild3.id}`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(404)
|
||||
expect(error.response.data.type).toEqual("not_found")
|
||||
expect(error.response.data.message).toEqual(
|
||||
`ProductCategory with id: ${productCategoryChild3.id}, is_internal: false, is_active: true was not found`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("GET /store/product-categories", () => {
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
return await db.teardown()
|
||||
})
|
||||
|
||||
it("gets list of product category with immediate children and parents", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(`/store/product-categories?limit=10`)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(4)
|
||||
expect(response.data.offset).toEqual(0)
|
||||
expect(response.data.limit).toEqual(10)
|
||||
|
||||
expect(response.data.product_categories).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
rank: 0,
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
}),
|
||||
category_children: [
|
||||
expect(response.data.product_categories).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
rank: 0,
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
}),
|
||||
category_children: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
rank: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
rank: 3,
|
||||
}),
|
||||
]),
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
parent_category: null,
|
||||
rank: 0,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
rank: 2,
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
category_children: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
rank: 3,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
parent_category: null,
|
||||
rank: 0,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
rank: 2,
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
category_children: [],
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
rank: 3,
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
category_children: [],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
it("gets list of product category with all childrens when include_descendants_tree=true", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/store/product-categories?parent_category_id=null&include_descendants_tree=true&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
parent_category: null,
|
||||
rank: 0,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
parent_category_id: productCategoryParent.id,
|
||||
}),
|
||||
category_children: [],
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
||||
// TODO: It seems filtering using null doesn't work.
|
||||
it("gets list of product category with all childrens when include_descendants_tree=true", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories?parent_category_id=null&include_descendants_tree=true&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
id: productCategoryParent.id,
|
||||
parent_category: null,
|
||||
rank: 0,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [],
|
||||
rank: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [],
|
||||
rank: 3,
|
||||
id: productCategory.id,
|
||||
parent_category_id: productCategoryParent.id,
|
||||
rank: 0,
|
||||
category_children: [
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [],
|
||||
rank: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
parent_category_id: productCategory.id,
|
||||
category_children: [],
|
||||
rank: 3,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
}),
|
||||
])
|
||||
)
|
||||
])
|
||||
)
|
||||
})
|
||||
|
||||
it("throws error when querying not allowed fields", async () => {
|
||||
const error = await api
|
||||
.get(`/store/product-categories?is_internal=true&limit=10`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"property is_internal should not exist"
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on free text on name and handle columns", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories?q=category-parent&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories[0].id).toEqual(
|
||||
productCategoryParent.id
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on handle attribute of the data model", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories?handle=${productCategory.handle}&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories[0].id).toEqual(
|
||||
productCategory.id
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on parent category", async () => {
|
||||
const response = await api.get(
|
||||
`/store/product-categories?parent_category_id=${productCategory.id}&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.product_categories).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
category_children: [],
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
rank: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
category_children: [],
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
rank: 3,
|
||||
}),
|
||||
])
|
||||
|
||||
const nullCategoryResponse = await api
|
||||
.get(`/store/product-categories?parent_category_id=null`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(nullCategoryResponse.status).toEqual(200)
|
||||
expect(nullCategoryResponse.data.count).toEqual(1)
|
||||
expect(nullCategoryResponse.data.product_categories[0].id).toEqual(
|
||||
productCategoryParent.id
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("throws error when querying not allowed fields", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const error = await api
|
||||
.get(`/store/product-categories?is_internal=true&limit=10`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(error.response.status).toEqual(400)
|
||||
expect(error.response.data.type).toEqual("invalid_data")
|
||||
expect(error.response.data.message).toEqual(
|
||||
"property is_internal should not exist"
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on free text on name and handle columns", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/store/product-categories?q=category-parent&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories[0].id).toEqual(
|
||||
productCategoryParent.id
|
||||
)
|
||||
})
|
||||
|
||||
it("filters based on handle attribute of the data model", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/store/product-categories?handle=${productCategory.handle}&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(1)
|
||||
expect(response.data.product_categories[0].id).toEqual(productCategory.id)
|
||||
})
|
||||
|
||||
it("filters based on parent category", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get(
|
||||
`/store/product-categories?parent_category_id=${productCategory.id}&limit=10`
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.product_categories).toEqual([
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild4.id,
|
||||
category_children: [],
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
rank: 2,
|
||||
}),
|
||||
expect.objectContaining({
|
||||
id: productCategoryChild.id,
|
||||
category_children: [],
|
||||
parent_category: expect.objectContaining({
|
||||
id: productCategory.id,
|
||||
}),
|
||||
rank: 3,
|
||||
}),
|
||||
])
|
||||
|
||||
const nullCategoryResponse = await api
|
||||
.get(`/store/product-categories?parent_category_id=null`)
|
||||
.catch((e) => e)
|
||||
|
||||
expect(nullCategoryResponse.status).toEqual(200)
|
||||
expect(nullCategoryResponse.data.count).toEqual(1)
|
||||
expect(nullCategoryResponse.data.product_categories[0].id).toEqual(
|
||||
productCategoryParent.id
|
||||
)
|
||||
})
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user