feat: Typescript for API layer (#817)

Co-authored-by: Philip Korsholm <88927411+pKorsholm@users.noreply.github.com>
Co-authored-by: Zakaria El Asri <33696020+zakariaelas@users.noreply.github.com>
Co-authored-by: Kasper Fabricius Kristensen <45367945+kasperkristensen@users.noreply.github.com>
Co-authored-by: Philip Korsholm <philip.korsholm@hotmail.com>
Co-authored-by: Sebastian Rindom <seb@medusa-commerce.com>
This commit is contained in:
Oliver Windall Juhl
2021-11-18 15:19:17 +01:00
committed by GitHub
parent 55e200bf68
commit 373532ecbc
413 changed files with 20961 additions and 10353 deletions

View File

@@ -0,0 +1,204 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`/admin/collections /admin/collections creates a collection 1`] = `
Object {
"collection": Object {
"created_at": Any<String>,
"deleted_at": null,
"handle": "test-handle-creation",
"id": StringMatching /\\^pcol_\\*/,
"metadata": null,
"title": "test collection creation",
"updated_at": Any<String>,
},
}
`;
exports[`/admin/collections /admin/collections lists collections 1`] = `
Object {
"collections": Array [
Object {
"handle": "test-collection",
"id": "test-collection",
"products": Array [
Object {
"collection_id": "test-collection",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product",
"height": null,
"hs_code": null,
"id": "test-product",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"status": "draft",
"subtitle": null,
"thumbnail": null,
"title": "Test product",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"collection_id": "test-collection",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description1",
"discountable": true,
"handle": "test-product1",
"height": null,
"hs_code": null,
"id": "test-product1",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"status": "draft",
"subtitle": null,
"thumbnail": null,
"title": "Test product1",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"title": "Test collection",
},
Object {
"handle": "test-collection1",
"id": "test-collection1",
"products": Array [
Object {
"collection_id": "test-collection1",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product_filtering_1",
"height": null,
"hs_code": null,
"id": "test-product_filtering_1",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"status": "proposed",
"subtitle": null,
"thumbnail": null,
"title": "Test product filtering 1",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
Object {
"collection_id": "test-collection1",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product_filtering_3",
"height": null,
"hs_code": null,
"id": "test-product_filtering_3",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"status": "draft",
"subtitle": null,
"thumbnail": null,
"title": "Test product filtering 3",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"title": "Test collection 1",
},
Object {
"handle": "test-collection2",
"id": "test-collection2",
"products": Array [
Object {
"collection_id": "test-collection2",
"created_at": Any<String>,
"deleted_at": null,
"description": "test-product-description",
"discountable": true,
"handle": "test-product_filtering_2",
"height": null,
"hs_code": null,
"id": "test-product_filtering_2",
"is_giftcard": false,
"length": null,
"material": null,
"metadata": null,
"mid_code": null,
"origin_country": null,
"profile_id": StringMatching /\\^sp_\\*/,
"status": "published",
"subtitle": null,
"thumbnail": null,
"title": "Test product filtering 2",
"type_id": "test-type",
"updated_at": Any<String>,
"weight": null,
"width": null,
},
],
"title": "Test collection 2",
},
],
"count": 3,
"limit": 10,
"offset": 0,
}
`;
exports[`/admin/collections /admin/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>,
},
}
`;
exports[`/admin/collections /admin/collections/:id updates a collection 1`] = `
Object {
"collection": Object {
"created_at": Any<String>,
"deleted_at": null,
"handle": "test-handle-creation",
"id": StringMatching /\\^pcol_\\*/,
"metadata": null,
"title": "test collection creation",
"updated_at": Any<String>,
},
}
`;

View File

@@ -479,7 +479,7 @@ Array [
]
`;
exports[`/admin/products GET /admin/products returns a list of products with giftcard in list 1`] = `
exports[`/admin/products GET /admin/products returns a list of products with only giftcard in list 1`] = `
Array [
Object {
"collection": null,

View File

@@ -0,0 +1,220 @@
const path = require("path")
const setupServer = require("../../../helpers/setup-server")
const { useApi } = require("../../../helpers/use-api")
const { initDb, useDb } = require("../../../helpers/use-db")
const productSeeder = require("../../helpers/product-seeder")
const adminSeeder = require("../../helpers/admin-seeder")
jest.setTimeout(30000)
describe("/admin/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("/admin/collections/:id", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
await productSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("updates a collection", async () => {
const api = useApi()
const creationResponse = await api.post(
"/admin/collections",
{
title: "test",
},
{ headers: { Authorization: "Bearer test_token" } }
)
const response = await api.post(
`/admin/collections/${creationResponse.data.collection.id}`,
{
title: "test collection creation",
handle: "test-handle-creation",
},
{ headers: { Authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
expect(response.data).toMatchSnapshot({
collection: {
id: expect.stringMatching(/^pcol_*/),
title: "test collection creation",
handle: "test-handle-creation",
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
it("deletes a collection", async () => {
const api = useApi()
const creationResponse = await api.post(
"/admin/collections",
{
title: "test",
},
{ headers: { Authorization: "Bearer test_token" } }
)
const response = await api.delete(
`/admin/collections/${creationResponse.data.collection.id}`,
{ headers: { Authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
expect(response.data).toEqual({
id: creationResponse.data.collection.id,
object: "product-collection",
deleted: true,
})
})
it("gets collection", async () => {
const api = useApi()
const response = await api.get("/admin/collections/test-collection", {
headers: { Authorization: "Bearer test_token" },
})
expect(response.data).toMatchSnapshot({
collection: {
id: "test-collection",
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
})
describe("/admin/collections", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
await productSeeder(dbConnection)
} catch (err) {
console.log(err)
throw err
}
})
afterEach(async () => {
const db = useDb()
await db.teardown()
})
it("creates a collection", async () => {
const api = useApi()
const response = await api.post(
"/admin/collections",
{
title: "test collection creation",
handle: "test-handle-creation",
},
{ headers: { Authorization: "Bearer test_token" } }
)
expect(response.status).toEqual(200)
expect(response.data).toMatchSnapshot({
collection: {
id: expect.stringMatching(/^pcol_*/),
title: "test collection creation",
handle: "test-handle-creation",
created_at: expect.any(String),
updated_at: expect.any(String),
},
})
})
it("lists collections", async () => {
const api = useApi()
const response = await api.get("/admin/collections", {
headers: { Authorization: "Bearer test_token" },
})
expect(response.data).toMatchSnapshot({
collections: [
{
id: "test-collection",
handle: "test-collection",
title: "Test collection",
products: [
{
collection_id: "test-collection",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
{
collection_id: "test-collection",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
],
},
{
id: "test-collection1",
handle: "test-collection1",
title: "Test collection 1",
products: [
{
collection_id: "test-collection1",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
{
collection_id: "test-collection1",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
],
},
{
id: "test-collection2",
handle: "test-collection2",
title: "Test collection 2",
products: [
{
collection_id: "test-collection2",
created_at: expect.any(String),
updated_at: expect.any(String),
profile_id: expect.stringMatching(/^sp_*/),
},
],
},
],
count: 3,
})
})
})
})

View File

@@ -1,4 +1,3 @@
const { dropDatabase } = require("pg-god")
const path = require("path")
const setupServer = require("../../../helpers/setup-server")

View File

@@ -29,32 +29,42 @@ describe("/admin/discounts", () => {
describe("GET /admin/discounts", () => {
beforeEach(async () => {
const manager = dbConnection.manager
try {
await adminSeeder(dbConnection)
await manager.insert(DiscountRule, {
id: "test-discount-rule",
description: "Test discount rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.insert(Discount, {
id: "test-discount",
code: "TESTING",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
await manager.insert(Discount, {
id: "messi-discount",
code: "BARCA100",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
} catch (err) {
throw err
}
await adminSeeder(dbConnection)
await manager.insert(DiscountRule, {
id: "test-discount-rule",
description: "Test discount rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.insert(Discount, {
id: "test-discount",
code: "TESTING",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
await manager.insert(Discount, {
id: "messi-discount",
code: "BARCA100",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
await manager.insert(Discount, {
id: "dynamic-discount",
code: "Dyn100",
rule_id: "test-discount-rule",
is_dynamic: true,
is_disabled: false,
})
await manager.insert(Discount, {
id: "disabled-discount",
code: "Dis100",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: true,
})
})
afterEach(async () => {
@@ -85,6 +95,50 @@ describe("/admin/discounts", () => {
])
)
})
it("lists dynamic discounts ", async () => {
const api = useApi()
const response = await api
.get("/admin/discounts?is_dynamic=true", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.discounts).toEqual([
expect.objectContaining({
id: "dynamic-discount",
code: "Dyn100",
}),
])
})
it("lists disabled discounts ", async () => {
const api = useApi()
const response = await api
.get("/admin/discounts?is_disabled=true", {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.count).toEqual(1)
expect(response.data.discounts).toEqual([
expect.objectContaining({
id: "disabled-discount",
code: "Dis100",
}),
])
})
})
describe("POST /admin/discounts", () => {
@@ -103,6 +157,60 @@ describe("/admin/discounts", () => {
await db.teardown()
})
it("creates a discount with a rule", async () => {
const api = useApi()
const response = await api
.post(
"/admin/discounts",
{
code: "HELLOWORLD",
rule: {
description: "test",
type: "percentage",
value: 10,
allocation: "total",
},
usage_limit: 10,
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
usage_limit: 10,
})
)
const test = await api.get(
`/admin/discounts/${response.data.discount.id}`,
{ headers: { Authorization: "Bearer test_token" } }
)
expect(test.status).toEqual(200)
expect(test.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
usage_limit: 10,
rule: expect.objectContaining({
value: 10,
type: "percentage",
description: "test",
allocation: "total",
}),
})
)
})
it("creates a discount and updates it", async () => {
const api = useApi()
@@ -591,11 +699,11 @@ describe("/admin/discounts", () => {
)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual([
expect(err.response.data).toEqual(
expect.objectContaining({
message: `"ends_at" must be greater than "ref:starts_at"`,
}),
])
message: `"ends_at" must be greater than "starts_at"`,
})
)
})
})
})
@@ -604,25 +712,21 @@ describe("/admin/discounts", () => {
let manager
beforeEach(async () => {
manager = dbConnection.manager
try {
await adminSeeder(dbConnection)
await manager.insert(DiscountRule, {
id: "test-discount-rule",
description: "Test discount rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.insert(Discount, {
id: "test-discount",
code: "TESTING",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
} catch (err) {
throw err
}
await adminSeeder(dbConnection)
await manager.insert(DiscountRule, {
id: "test-discount-rule",
description: "Test discount rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.insert(Discount, {
id: "test-discount",
code: "TESTING",
rule_id: "test-discount-rule",
is_dynamic: false,
is_disabled: false,
})
})
afterEach(async () => {

View File

@@ -158,12 +158,15 @@ describe("/admin/gift-cards", () => {
it("creates a gift card", async () => {
const api = useApi()
const dateString = new Date().toISOString()
const response = await api
.post(
"/admin/gift-cards",
{
value: 1000,
region_id: "region",
ends_at: dateString,
},
{
headers: {

View File

@@ -3,7 +3,6 @@ const {
ReturnReason,
Order,
LineItem,
ProductVariant,
CustomShippingOption,
} = require("@medusajs/medusa")
@@ -76,12 +75,8 @@ describe("/admin/orders", () => {
describe("POST /admin/orders/:id", () => {
beforeEach(async () => {
try {
await adminSeeder(dbConnection)
await orderSeeder(dbConnection)
} catch (err) {
throw err
}
await adminSeeder(dbConnection)
await orderSeeder(dbConnection)
})
afterEach(async () => {
@@ -220,7 +215,6 @@ describe("/admin/orders", () => {
it("cancels an order and increments inventory_quantity", async () => {
const api = useApi()
const manager = dbConnection.manager
const initialInventoryRes = await api.get("/store/variants/test-variant")
@@ -1111,7 +1105,7 @@ describe("/admin/orders", () => {
}
)
//Find variant that should have its inventory_quantity updated
// Find variant that should have its inventory_quantity updated
const toTest = returned.data.order.items.find((i) => i.id === "test-item")
expect(returned.status).toEqual(200)
@@ -1144,7 +1138,7 @@ describe("/admin/orders", () => {
}
)
//Find variant that should have its inventory_quantity updated
// Find variant that should have its inventory_quantity updated
const toTest = returned.data.order.items.find((i) => i.id === "test-item")
expect(returned.status).toEqual(200)
@@ -1203,6 +1197,44 @@ describe("/admin/orders", () => {
])
})
it("lists all orders with a fulfillment status = fulfilled", async () => {
const api = useApi()
const response = await api
.get("/admin/orders?fulfillment_status[]=fulfilled", {
headers: {
authorization: "Bearer test_token",
},
})
.catch((err) => console.log(err))
expect(response.status).toEqual(200)
expect(response.data.orders).toEqual([
expect.objectContaining({
id: "test-order",
}),
])
})
it("fails to lists all orders with an invalid status", async () => {
expect.assertions(3)
const api = useApi()
await api
.get("/admin/orders?status[]=test", {
headers: {
authorization: "Bearer test_token",
},
})
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.type).toEqual("invalid_data")
expect(err.response.data.message).toEqual(
"each value in status must be a valid enum value"
)
})
})
it("list all orders with matching order email", async () => {
const api = useApi()
@@ -1696,7 +1728,7 @@ describe("/admin/orders", () => {
expect(returnOnOrder.status).toEqual(200)
const captured = await api.post(
await api.post(
"/admin/orders/test-order/capture",
{},
{

View File

@@ -340,7 +340,7 @@ describe("/admin/products", () => {
}
})
it("returns a list of products with giftcard in list", async () => {
it("returns a list of products with only giftcard in list", async () => {
const api = useApi()
const payload = {
@@ -641,7 +641,6 @@ describe("/admin/products", () => {
options: expect.any(Array),
tags: expect.any(Array),
variants: expect.any(Array),
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
@@ -653,7 +652,6 @@ describe("/admin/products", () => {
options: expect.any(Array),
tags: expect.any(Array),
variants: expect.any(Array),
created_at: expect.any(String),
updated_at: expect.any(String),
},
{
@@ -665,7 +663,6 @@ describe("/admin/products", () => {
options: expect.any(Array),
tags: expect.any(Array),
variants: expect.any(Array),
created_at: expect.any(String),
updated_at: expect.any(String),
},
])
@@ -782,6 +779,46 @@ describe("/admin/products", () => {
)
})
it("creates a product that is not discountable", async () => {
const api = useApi()
const payload = {
title: "Test",
discountable: false,
description: "test-product-description",
type: { value: "test-type" },
images: ["test-image.png", "test-image-2.png"],
collection_id: "test-collection",
tags: [{ value: "123" }, { value: "456" }],
options: [{ title: "size" }, { title: "color" }],
variants: [
{
title: "Test variant",
inventory_quantity: 10,
prices: [{ currency_code: "usd", amount: 100 }],
options: [{ value: "large" }, { value: "green" }],
},
],
}
const response = await api
.post("/admin/products", payload, {
headers: {
Authorization: "Bearer test_token",
},
})
.catch((err) => {
console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.product).toEqual(
expect.objectContaining({
discountable: false,
})
)
})
it("Sets variant ranks when creating a product", async () => {
const api = useApi()
@@ -890,7 +927,6 @@ describe("/admin/products", () => {
const payload = {
collection_id: null,
type: null,
variants: [
{
id: "test-variant",
@@ -944,7 +980,6 @@ describe("/admin/products", () => {
],
}),
],
type: null,
status: "published",
collection: null,
type: expect.objectContaining({

View File

@@ -67,6 +67,11 @@ describe("/admin/discounts", () => {
console.log(err)
})
expect(response.data).toEqual({
id: "test-region",
object: "region",
deleted: true,
})
expect(response.status).toEqual(200)
})
})