chore: Start using medusa test runner on product API tests (#6609)

I want to get this merged where we only switch to the runner, so actual test changes are more obvious when reviewing.
This commit is contained in:
Stevche Radevski
2024-03-07 10:29:00 +00:00
committed by GitHub
parent 3d0da980cf
commit 7dee1a3fd3
+117 -233
View File
@@ -1,63 +1,65 @@
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 adminSeeder = require("../../../helpers/admin-seeder")
const productSeeder = require("../../../helpers/product-seeder")
const { const {
createAdminUser,
adminHeaders,
} = require("../../../helpers/create-admin-user")
const { breaking } = require("../../../helpers/breaking")
const { IdMap, medusaIntegrationTestRunner } = require("medusa-test-utils")
let productSeeder = undefined
let priceListSeeder = undefined
let {
ProductVariant, ProductVariant,
ProductOptionValue, ProductOptionValue,
MoneyAmount, MoneyAmount,
DiscountConditionType, DiscountConditionType,
DiscountConditionOperator, DiscountConditionOperator,
} = require("@medusajs/medusa") DiscountRuleType,
const priceListSeeder = require("../../../helpers/price-list-seeder") AllocationType,
const { } = {}
let {
simpleProductFactory, simpleProductFactory,
simpleDiscountFactory, simpleDiscountFactory,
simpleSalesChannelFactory, simpleSalesChannelFactory,
simpleRegionFactory, simpleRegionFactory,
simplePriceListFactory, simplePriceListFactory,
} = require("../../../factories") } = {}
const { DiscountRuleType, AllocationType } = require("@medusajs/medusa/dist")
const { IdMap } = require("medusa-test-utils")
jest.setTimeout(50000) jest.setTimeout(50000)
const adminHeaders = { medusaIntegrationTestRunner({
headers: {
"x-medusa-access-token": "test_token",
},
}
describe("/admin/products", () => {
let medusaProcess
let dbConnection
beforeAll(async () => {
const cwd = path.resolve(path.join(__dirname, "..", ".."))
dbConnection = await initDb({ cwd })
medusaProcess = await setupServer({
cwd,
env: { MEDUSA_FF_PRODUCT_CATEGORIES: true }, env: { MEDUSA_FF_PRODUCT_CATEGORIES: true },
}) testSuite: ({ dbConnection, getContainer, api }) => {
beforeAll(() => {
// Note: We have to lazily load everything because there are weird ordering issues when doing `require` of `@medusajs/medusa`
productSeeder = require("../../../helpers/product-seeder")
priceListSeeder = require("../../../helpers/price-list-seeder")
;({
ProductVariant,
ProductOptionValue,
MoneyAmount,
DiscountConditionType,
DiscountConditionOperator,
DiscountRuleType,
AllocationType,
} = require("@medusajs/medusa"))
;({
simpleProductFactory,
simpleDiscountFactory,
simpleSalesChannelFactory,
simpleRegionFactory,
simplePriceListFactory,
} = require("../../../factories"))
}) })
afterAll(async () => { beforeEach(async () => {
const db = useDb() const container = getContainer()
await db.shutdown() await createAdminUser(dbConnection, adminHeaders, container)
medusaProcess.kill()
}) })
describe("/admin/products", () => {
describe("GET /admin/products", () => { describe("GET /admin/products", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
id: "default-channel", id: "default-channel",
@@ -65,14 +67,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("returns a list of products with all statuses when no status or invalid status is provided", async () => { it("returns a list of products with all statuses when no status or invalid status is provided", async () => {
const api = useApi()
const res = await api const res = await api
.get("/admin/products", adminHeaders) .get("/admin/products", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -95,8 +90,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of all products when no query is provided", async () => { it("returns a list of all products when no query is provided", async () => {
const api = useApi()
const res = await api const res = await api
.get("/admin/products?q=", adminHeaders) .get("/admin/products?q=", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -119,8 +112,6 @@ describe("/admin/products", () => {
}) })
it("should return prices not in price list for list product endpoint", async () => { it("should return prices not in price list for list product endpoint", async () => {
const api = useApi()
await simplePriceListFactory(dbConnection, { await simplePriceListFactory(dbConnection, {
prices: [ prices: [
{ {
@@ -131,9 +122,14 @@ describe("/admin/products", () => {
], ],
}) })
const res = await api.get("/admin/products?id=test-product", adminHeaders) const res = await api.get(
"/admin/products?id=test-product",
adminHeaders
)
const prices = res.data.products[0].variants.map((v) => v.prices).flat() const prices = res.data.products[0].variants
.map((v) => v.prices)
.flat()
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(res.data.products).toEqual( expect(res.data.products).toEqual(
@@ -152,8 +148,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products where status is proposed", async () => { it("returns a list of products where status is proposed", async () => {
const api = useApi()
const payload = { const payload = {
status: "proposed", status: "proposed",
} }
@@ -183,8 +177,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products where status is proposed or published", async () => { it("returns a list of products where status is proposed or published", async () => {
const api = useApi()
const notExpected = [ const notExpected = [
expect.objectContaining({ status: "draft" }), expect.objectContaining({ status: "draft" }),
expect.objectContaining({ status: "rejected" }), expect.objectContaining({ status: "rejected" }),
@@ -222,8 +214,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products where type_id is test-type", async () => { it("returns a list of products where type_id is test-type", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?type_id[]=test-type", adminHeaders) .get("/admin/products?type_id[]=test-type", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -242,8 +232,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products filtered by discount condition id", async () => { it("returns a list of products filtered by discount condition id", async () => {
const api = useApi()
const resProd = await api.get("/admin/products", adminHeaders) const resProd = await api.get("/admin/products", adminHeaders)
const prod1 = resProd.data.products[0] const prod1 = resProd.data.products[0]
@@ -315,8 +303,6 @@ describe("/admin/products", () => {
}) })
it("doesn't expand collection and types", async () => { it("doesn't expand collection and types", async () => {
const api = useApi()
const notExpected = [ const notExpected = [
expect.objectContaining({ expect.objectContaining({
collection: expect.any(Object), collection: expect.any(Object),
@@ -356,10 +342,11 @@ describe("/admin/products", () => {
}) })
it("returns a list of deleted products with free text query", async () => { it("returns a list of deleted products with free text query", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?deleted_at[gt]=01-26-1990&q=test", adminHeaders) .get(
"/admin/products?deleted_at[gt]=01-26-1990&q=test",
adminHeaders
)
.catch((err) => { .catch((err) => {
console.log(err) console.log(err)
}) })
@@ -377,8 +364,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products with free text query and limit", async () => { it("returns a list of products with free text query and limit", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?q=t&limit=2", adminHeaders) .get("/admin/products?q=t&limit=2", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -390,8 +375,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products with free text query including variant prices", async () => { it("returns a list of products with free text query including variant prices", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?q=test+product1", adminHeaders) .get("/admin/products?q=test+product1", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -416,8 +399,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products with free text query and offset", async () => { it("returns a list of products with free text query and offset", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?q=t&offset=1", adminHeaders) .get("/admin/products?q=t&offset=1", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -429,8 +410,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of deleted products", async () => { it("returns a list of deleted products", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?deleted_at[gt]=01-26-1990", adminHeaders) .get("/admin/products?deleted_at[gt]=01-26-1990", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -449,15 +428,16 @@ describe("/admin/products", () => {
}) })
it("returns a list of products in collection", async () => { it("returns a list of products in collection", async () => {
const api = useApi()
const notExpected = [ const notExpected = [
expect.objectContaining({ collection_id: "test-collection" }), expect.objectContaining({ collection_id: "test-collection" }),
expect.objectContaining({ collection_id: "test-collection2" }), expect.objectContaining({ collection_id: "test-collection2" }),
] ]
const response = await api const response = await api
.get("/admin/products?collection_id[]=test-collection1", adminHeaders) .get(
"/admin/products?collection_id[]=test-collection1",
adminHeaders
)
.catch((err) => { .catch((err) => {
console.log(err) console.log(err)
}) })
@@ -485,8 +465,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products with tags", async () => { it("returns a list of products with tags", async () => {
const api = useApi()
const notExpected = [ const notExpected = [
expect.objectContaining({ id: "tag1" }), expect.objectContaining({ id: "tag1" }),
expect.objectContaining({ id: "tag2" }), expect.objectContaining({ id: "tag2" }),
@@ -515,14 +493,14 @@ describe("/admin/products", () => {
) )
for (const product of response.data.products) { for (const product of response.data.products) {
for (const notExpect of notExpected) { for (const notExpect of notExpected) {
expect(product.tags).toEqual(expect.not.arrayContaining([notExpect])) expect(product.tags).toEqual(
expect.not.arrayContaining([notExpect])
)
} }
} }
}) })
it("returns a list of products with tags in a collection", async () => { it("returns a list of products with tags in a collection", async () => {
const api = useApi()
const notExpectedTags = [ const notExpectedTags = [
expect.objectContaining({ id: "tag1" }), expect.objectContaining({ id: "tag1" }),
expect.objectContaining({ id: "tag2" }), expect.objectContaining({ id: "tag2" }),
@@ -563,14 +541,14 @@ describe("/admin/products", () => {
for (const product of response.data.products) { for (const product of response.data.products) {
for (const notExpect of notExpectedTags) { for (const notExpect of notExpectedTags) {
expect(product.tags).toEqual(expect.not.arrayContaining([notExpect])) expect(product.tags).toEqual(
expect.not.arrayContaining([notExpect])
)
} }
} }
}) })
it("returns a list of products with only giftcard in list", async () => { it("returns a list of products with only giftcard in list", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test Giftcard", title: "Test Giftcard",
is_giftcard: true, is_giftcard: true,
@@ -585,7 +563,9 @@ describe("/admin/products", () => {
], ],
} }
await api.post("/admin/products", payload, adminHeaders).catch((err) => { await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err) console.log(err)
}) })
@@ -656,8 +636,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products not containing a giftcard in list", async () => { it("returns a list of products not containing a giftcard in list", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test Giftcard", title: "Test Giftcard",
is_giftcard: true, is_giftcard: true,
@@ -672,7 +650,9 @@ describe("/admin/products", () => {
], ],
} }
await api.post("/admin/products", payload, adminHeaders).catch((err) => { await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err) console.log(err)
}) })
@@ -690,8 +670,6 @@ describe("/admin/products", () => {
}) })
it("returns a list of products with child entities", async () => { it("returns a list of products with child entities", async () => {
const api = useApi()
const response = await api const response = await api
.get("/admin/products?order=created_at", adminHeaders) .get("/admin/products?order=created_at", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -961,18 +939,9 @@ describe("/admin/products", () => {
}, },
], ],
}) })
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
}) })
it("should get a product with default relations", async () => { it("should get a product with default relations", async () => {
const api = useApi()
const res = await api const res = await api
.get(`/admin/products/${productId}`, adminHeaders) .get(`/admin/products/${productId}`, adminHeaders)
.catch((err) => { .catch((err) => {
@@ -1033,8 +1002,6 @@ describe("/admin/products", () => {
}) })
it("should get a product with prices", async () => { it("should get a product with prices", async () => {
const api = useApi()
const res = await api const res = await api
.get( .get(
`/admin/products/${productId}?expand=variants,variants.prices`, `/admin/products/${productId}?expand=variants,variants.prices`,
@@ -1058,8 +1025,6 @@ describe("/admin/products", () => {
}) })
it("should get a product only with variants expanded", async () => { it("should get a product only with variants expanded", async () => {
const api = useApi()
const res = await api const res = await api
.get(`/admin/products/${productId}?expand=variants`, adminHeaders) .get(`/admin/products/${productId}?expand=variants`, adminHeaders)
.catch((err) => { .catch((err) => {
@@ -1082,7 +1047,6 @@ describe("/admin/products", () => {
describe("POST /admin/products", () => { describe("POST /admin/products", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
@@ -1091,14 +1055,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("creates a product", async () => { it("creates a product", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test", title: "Test",
description: "test-product-description", description: "test-product-description",
@@ -1262,8 +1219,6 @@ describe("/admin/products", () => {
}) })
it("creates a product that is not discountable", async () => { it("creates a product that is not discountable", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test", title: "Test",
discountable: false, discountable: false,
@@ -1298,8 +1253,6 @@ describe("/admin/products", () => {
}) })
it("Sets variant ranks when creating a product", async () => { it("Sets variant ranks when creating a product", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test product - 1", title: "Test product - 1",
description: "test-product-description 1", description: "test-product-description 1",
@@ -1356,8 +1309,6 @@ describe("/admin/products", () => {
}) })
it("creates a giftcard", async () => { it("creates a giftcard", async () => {
const api = useApi()
const payload = { const payload = {
title: "Test Giftcard", title: "Test Giftcard",
is_giftcard: true, is_giftcard: true,
@@ -1389,8 +1340,6 @@ describe("/admin/products", () => {
}) })
it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => { it("updates a product (update prices, tags, update status, delete collection, delete type, replaces images)", async () => {
const api = useApi()
const payload = { const payload = {
collection_id: null, collection_id: null,
variants: [ variants: [
@@ -1509,8 +1458,6 @@ describe("/admin/products", () => {
}) })
it("updates product (removes images when empty array included)", async () => { it("updates product (removes images when empty array included)", async () => {
const api = useApi()
const payload = { const payload = {
images: [], images: [],
} }
@@ -1527,8 +1474,6 @@ describe("/admin/products", () => {
}) })
it("updates a product by deleting a field from metadata", async () => { it("updates a product by deleting a field from metadata", async () => {
const api = useApi()
const product = await simpleProductFactory(dbConnection, { const product = await simpleProductFactory(dbConnection, {
metadata: { metadata: {
"test-key": "test-value", "test-key": "test-value",
@@ -1558,14 +1503,16 @@ describe("/admin/products", () => {
}) })
it("fails to update product with invalid status", async () => { it("fails to update product with invalid status", async () => {
const api = useApi()
const payload = { const payload = {
status: null, status: null,
} }
try { try {
await api.post("/admin/products/test-product", payload, adminHeaders) await api.post(
"/admin/products/test-product",
payload,
adminHeaders
)
} catch (e) { } catch (e) {
expect(e.response.status).toEqual(400) expect(e.response.status).toEqual(400)
expect(e.response.data.type).toEqual("invalid_data") expect(e.response.data.type).toEqual("invalid_data")
@@ -1573,8 +1520,6 @@ describe("/admin/products", () => {
}) })
it("updates a product (variant ordering)", async () => { it("updates a product (variant ordering)", async () => {
const api = useApi()
const payload = { const payload = {
collection_id: null, collection_id: null,
type: null, type: null,
@@ -1623,8 +1568,6 @@ describe("/admin/products", () => {
}) })
it("add option", async () => { it("add option", async () => {
const api = useApi()
const payload = { const payload = {
title: "should_add", title: "should_add",
} }
@@ -1667,7 +1610,9 @@ describe("/admin/products", () => {
variants: [ variants: [
{ {
product_id: "test-product-with-variant", product_id: "test-product-with-variant",
options: [{ option_id: "test-product-option-1", value: "test" }], options: [
{ option_id: "test-product-option-1", value: "test" },
],
}, },
], ],
options: [ options: [
@@ -1677,17 +1622,9 @@ describe("/admin/products", () => {
}, },
], ],
}) })
await adminSeeder(dbConnection)
})
afterEach(async () => {
const db = useDb()
await db.teardown()
}) })
it("deletes a product option", async () => { it("deletes a product option", async () => {
const api = useApi()
const response = await api const response = await api
.delete( .delete(
"/admin/products/test-product-without-variants/options/test-product-option", "/admin/products/test-product-without-variants/options/test-product-option",
@@ -1708,8 +1645,6 @@ describe("/admin/products", () => {
}) })
it("deletes a values associated with deleted option", async () => { it("deletes a values associated with deleted option", async () => {
const api = useApi()
const response = await api.delete( const response = await api.delete(
"/admin/products/test-product-with-variant/options/test-product-option-1", "/admin/products/test-product-with-variant/options/test-product-option-1",
adminHeaders adminHeaders
@@ -1729,7 +1664,6 @@ describe("/admin/products", () => {
describe("GET /admin/products/:id/variants", () => { describe("GET /admin/products/:id/variants", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
id: "default-channel", id: "default-channel",
@@ -1737,14 +1671,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("should return the variants related to the requested product", async () => { it("should return the variants related to the requested product", async () => {
const api = useApi()
const res = await api const res = await api
.get("/admin/products/test-product/variants", adminHeaders) .get("/admin/products/test-product/variants", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -1779,7 +1706,6 @@ describe("/admin/products", () => {
describe("updates a variant's default prices (ignores prices associated with a Price List)", () => { describe("updates a variant's default prices (ignores prices associated with a Price List)", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await priceListSeeder(dbConnection) await priceListSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
@@ -1788,13 +1714,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully updates a variant's default prices by changing an existing price (currency_code)", async () => { it("successfully updates a variant's default prices by changing an existing price (currency_code)", async () => {
const api = useApi()
const data = { const data = {
prices: [ prices: [
{ {
@@ -1834,8 +1754,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variant's price by changing an existing price (given a region_id)", async () => { it("successfully updates a variant's price by changing an existing price (given a region_id)", async () => {
const api = useApi()
const data = { const data = {
prices: [ prices: [
{ {
@@ -1876,7 +1794,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variant's prices by adding a new price", async () => { it("successfully updates a variant's prices by adding a new price", async () => {
const api = useApi()
const data = { const data = {
title: "Test variant prices", title: "Test variant prices",
prices: [ prices: [
@@ -1931,7 +1848,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variant's prices by replacing a price", async () => { it("successfully updates a variant's prices by replacing a price", async () => {
const api = useApi()
const variantId = "test-variant" const variantId = "test-variant"
const data = { const data = {
prices: [ prices: [
@@ -1968,7 +1884,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variant's prices by deleting a price and adding another price", async () => { it("successfully updates a variant's prices by deleting a price and adding another price", async () => {
const api = useApi()
const data = { const data = {
prices: [ prices: [
{ {
@@ -2015,7 +1930,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variant's prices by updating an existing price (using region_id) and adding another price", async () => { it("successfully updates a variant's prices by updating an existing price (using region_id) and adding another price", async () => {
const api = useApi()
const data = { const data = {
prices: [ prices: [
{ {
@@ -2063,8 +1977,6 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a region price", async () => { it("successfully deletes a region price", async () => {
const api = useApi()
const createRegionPricePayload = { const createRegionPricePayload = {
prices: [ prices: [
{ {
@@ -2156,8 +2068,6 @@ describe("/admin/products", () => {
}) })
it("successfully updates a variants prices by deleting both a currency and region price", async () => { it("successfully updates a variants prices by deleting both a currency and region price", async () => {
const api = useApi()
await Promise.all( await Promise.all(
["reg_1", "reg_2", "reg_3"].map(async (regionId) => { ["reg_1", "reg_2", "reg_3"].map(async (regionId) => {
return await simpleRegionFactory(dbConnection, { return await simpleRegionFactory(dbConnection, {
@@ -2256,7 +2166,6 @@ describe("/admin/products", () => {
beforeEach(async () => { beforeEach(async () => {
try { try {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
id: "default-channel", id: "default-channel",
@@ -2268,14 +2177,7 @@ describe("/admin/products", () => {
} }
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("create a product variant with prices (regional and currency)", async () => { it("create a product variant with prices (regional and currency)", async () => {
const api = useApi()
const payload = { const payload = {
title: "New variant", title: "New variant",
sku: "new-sku", sku: "new-sku",
@@ -2296,7 +2198,11 @@ describe("/admin/products", () => {
} }
const res = await api const res = await api
.post("/admin/products/test-product/variants", payload, adminHeaders) .post(
"/admin/products/test-product/variants",
payload,
adminHeaders
)
.catch((err) => console.log(err)) .catch((err) => console.log(err))
const insertedVariant = res.data.product.variants.find( const insertedVariant = res.data.product.variants.find(
@@ -2333,7 +2239,6 @@ describe("/admin/products", () => {
describe("testing for soft-deletion + uniqueness on handles, collection and variant properties", () => { describe("testing for soft-deletion + uniqueness on handles, collection and variant properties", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
id: "default-channel", id: "default-channel",
@@ -2341,14 +2246,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully deletes a product", async () => { it("successfully deletes a product", async () => {
const api = useApi()
const response = await api const response = await api
.delete("/admin/products/test-product", adminHeaders) .delete("/admin/products/test-product", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -2366,11 +2264,12 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a product and variants", async () => { it("successfully deletes a product and variants", async () => {
const api = useApi() const variantPre = await dbConnection.manager.findOne(
ProductVariant,
const variantPre = await dbConnection.manager.findOne(ProductVariant, { {
where: { id: "test-variant" }, where: { id: "test-variant" },
}) }
)
expect(variantPre).toBeTruthy() expect(variantPre).toBeTruthy()
@@ -2397,12 +2296,13 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a product variant and its associated option values", async () => { it("successfully deletes a product variant and its associated option values", async () => {
const api = useApi()
// Validate that the option value exists // Validate that the option value exists
const optValPre = await dbConnection.manager.findOne(ProductOptionValue, { const optValPre = await dbConnection.manager.findOne(
ProductOptionValue,
{
where: { variant_id: "test-variant_2" }, where: { variant_id: "test-variant_2" },
}) }
)
expect(optValPre).toBeTruthy() expect(optValPre).toBeTruthy()
@@ -2442,12 +2342,13 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a product and any option value associated with one of its variants", async () => { it("successfully deletes a product and any option value associated with one of its variants", async () => {
const api = useApi()
// Validate that the option value exists // Validate that the option value exists
const optValPre = await dbConnection.manager.findOne(ProductOptionValue, { const optValPre = await dbConnection.manager.findOne(
ProductOptionValue,
{
where: { variant_id: "test-variant_2" }, where: { variant_id: "test-variant_2" },
}) }
)
expect(optValPre).toBeTruthy() expect(optValPre).toBeTruthy()
@@ -2489,8 +2390,6 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a product variant and its associated prices", async () => { it("successfully deletes a product variant and its associated prices", async () => {
const api = useApi()
// Validate that the price exists // Validate that the price exists
const pricePre = await dbConnection.manager.findOne(MoneyAmount, { const pricePre = await dbConnection.manager.findOne(MoneyAmount, {
where: { id: "test-price" }, where: { id: "test-price" },
@@ -2514,12 +2413,15 @@ describe("/admin/products", () => {
expect(pricePost).not.toBeTruthy() expect(pricePost).not.toBeTruthy()
// Validate that the price still exists in the DB with deleted_at // Validate that the price still exists in the DB with deleted_at
const optValDeleted = await dbConnection.manager.findOne(MoneyAmount, { const optValDeleted = await dbConnection.manager.findOne(
MoneyAmount,
{
where: { where: {
id: "test-price", id: "test-price",
}, },
withDeleted: true, withDeleted: true,
}) }
)
expect(optValDeleted).toEqual( expect(optValDeleted).toEqual(
expect.objectContaining({ expect.objectContaining({
@@ -2530,8 +2432,6 @@ describe("/admin/products", () => {
}) })
it("successfully deletes a product and any prices associated with one of its variants", async () => { it("successfully deletes a product and any prices associated with one of its variants", async () => {
const api = useApi()
// Validate that the price exists // Validate that the price exists
const pricePre = await dbConnection.manager.findOne(MoneyAmount, { const pricePre = await dbConnection.manager.findOne(MoneyAmount, {
where: { id: "test-price" }, where: { id: "test-price" },
@@ -2555,12 +2455,15 @@ describe("/admin/products", () => {
expect(pricePost).not.toBeTruthy() expect(pricePost).not.toBeTruthy()
// Validate that the price still exists in the DB with deleted_at // Validate that the price still exists in the DB with deleted_at
const optValDeleted = await dbConnection.manager.findOne(MoneyAmount, { const optValDeleted = await dbConnection.manager.findOne(
MoneyAmount,
{
where: { where: {
id: "test-price", id: "test-price",
}, },
withDeleted: true, withDeleted: true,
}) }
)
expect(optValDeleted).toEqual( expect(optValDeleted).toEqual(
expect.objectContaining({ expect.objectContaining({
@@ -2571,8 +2474,6 @@ describe("/admin/products", () => {
}) })
it("successfully creates product with soft-deleted product handle and deletes it again", async () => { it("successfully creates product with soft-deleted product handle and deletes it again", async () => {
const api = useApi()
// First we soft-delete the product // First we soft-delete the product
const response = await api const response = await api
.delete("/admin/products/test-product", adminHeaders) .delete("/admin/products/test-product", adminHeaders)
@@ -2620,8 +2521,6 @@ describe("/admin/products", () => {
}) })
it("should fail when creating a product with a handle that already exists", async () => { it("should fail when creating a product with a handle that already exists", async () => {
const api = useApi()
// Lets try to create a product with same handle as deleted one // Lets try to create a product with same handle as deleted one
const payload = { const payload = {
title: "Test product", title: "Test product",
@@ -2652,8 +2551,6 @@ describe("/admin/products", () => {
}) })
it("successfully deletes product collection", async () => { it("successfully deletes product collection", async () => {
const api = useApi()
// First we soft-delete the product collection // First we soft-delete the product collection
const response = await api const response = await api
.delete("/admin/collections/test-collection", adminHeaders) .delete("/admin/collections/test-collection", adminHeaders)
@@ -2666,8 +2563,6 @@ describe("/admin/products", () => {
}) })
it("successfully creates soft-deleted product collection", async () => { it("successfully creates soft-deleted product collection", async () => {
const api = useApi()
const response = await api const response = await api
.delete("/admin/collections/test-collection", adminHeaders) .delete("/admin/collections/test-collection", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -2683,15 +2578,17 @@ describe("/admin/products", () => {
handle: "test-collection", handle: "test-collection",
} }
const res = await api.post("/admin/collections", payload, adminHeaders) const res = await api.post(
"/admin/collections",
payload,
adminHeaders
)
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
expect(res.data.collection.handle).toEqual("test-collection") expect(res.data.collection.handle).toEqual("test-collection")
}) })
it("should fail when creating a collection with a handle that already exists", async () => { it("should fail when creating a collection with a handle that already exists", async () => {
const api = useApi()
// Lets try to create a collection with same handle as deleted one // Lets try to create a collection with same handle as deleted one
const payload = { const payload = {
title: "Another test collection", title: "Another test collection",
@@ -2708,8 +2605,6 @@ describe("/admin/products", () => {
}) })
it("successfully creates soft-deleted product variant", async () => { it("successfully creates soft-deleted product variant", async () => {
const api = useApi()
await api await api
.get("/admin/products/test-product", adminHeaders) .get("/admin/products/test-product", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -2744,7 +2639,11 @@ describe("/admin/products", () => {
} }
const res = await api const res = await api
.post("/admin/products/test-product/variants", payload, adminHeaders) .post(
"/admin/products/test-product/variants",
payload,
adminHeaders
)
.catch((err) => console.log(err)) .catch((err) => console.log(err))
expect(res.status).toEqual(200) expect(res.status).toEqual(200)
@@ -2764,8 +2663,6 @@ describe("/admin/products", () => {
describe("POST /admin/products/:id/variants/:id", () => { describe("POST /admin/products/:id/variants/:id", () => {
beforeEach(async () => { beforeEach(async () => {
await adminSeeder(dbConnection)
await simpleProductFactory(dbConnection, { await simpleProductFactory(dbConnection, {
id: "test-product-to-update", id: "test-product-to-update",
variants: [ variants: [
@@ -2776,14 +2673,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully updates variant without prices", async () => { it("successfully updates variant without prices", async () => {
const api = useApi()
const res = await api const res = await api
.post( .post(
"/admin/products/test-product-to-update/variants/test-variant-to-update", "/admin/products/test-product-to-update/variants/test-variant-to-update",
@@ -2803,7 +2693,6 @@ describe("/admin/products", () => {
describe("GET /admin/products/tag-usage", () => { describe("GET /admin/products/tag-usage", () => {
beforeEach(async () => { beforeEach(async () => {
await productSeeder(dbConnection) await productSeeder(dbConnection)
await adminSeeder(dbConnection)
await simpleSalesChannelFactory(dbConnection, { await simpleSalesChannelFactory(dbConnection, {
name: "Default channel", name: "Default channel",
id: "default-channel", id: "default-channel",
@@ -2811,14 +2700,7 @@ describe("/admin/products", () => {
}) })
}) })
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("successfully gets the tags usage", async () => { it("successfully gets the tags usage", async () => {
const api = useApi()
const res = await api const res = await api
.get("/admin/products/tag-usage", adminHeaders) .get("/admin/products/tag-usage", adminHeaders)
.catch((err) => { .catch((err) => {
@@ -2836,4 +2718,6 @@ describe("/admin/products", () => {
) )
}) })
}) })
})
},
}) })