fix: api claim types + tests (#967)

This commit is contained in:
Zakaria El Asri authored and GitHub committed 2022-01-07 11:45:23 +01:00
1 parent 7cc3640796
commit d365839a08
4 files changed
+223 -10

No files matched your search

@@ -1,8 +1,9 @@
import { request } from "../../../../../helpers/test-request" import { request } from "../../../../../helpers/test-request"
import { InviteServiceMock } from "../../../../../services/__mocks__/invite" import { InviteServiceMock } from "../../../../../services/__mocks__/invite"
import { UserRole } from "../../../../../types/user"
describe("POST /invites", () => { describe("POST /invites", () => {
describe("checks validation rules", () => { describe("checks that role must not be empty", () => {
let subject let subject
beforeAll(async () => { beforeAll(async () => {
@@ -26,4 +27,35 @@ describe("POST /invites", () => {
expect(subject.error).toBeTruthy() expect(subject.error).toBeTruthy()
}) })
}) })
describe("successfully creates an invite", () => {
let subject
beforeAll(async () => {
subject = await request("POST", `/admin/invites`, {
payload: {
role: "admin",
user: "lebron@james.com",
},
session: {
jwt: {
userId: "test_user",
},
},
})
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls InviteService create", () => {
console.log(subject.error)
expect(InviteServiceMock.create).toHaveBeenCalledTimes(1)
expect(InviteServiceMock.create).toHaveBeenCalledWith(
"lebron@james.com",
UserRole.ADMIN
)
})
})
}) })
@@ -0,0 +1,174 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ClaimServiceMock } from "../../../../../services/__mocks__/claim"
describe("POST /admin/order/:id/claims", () => {
describe("successfully creates a claim", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
payload: {
type: "replace",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
},
],
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls ClaimService create", () => {
expect(ClaimServiceMock.create).toHaveBeenCalledTimes(1)
expect(ClaimServiceMock.create).toHaveBeenCalledWith(
expect.objectContaining({
type: "replace",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
},
],
})
)
})
})
describe("fails to create a claim when type is not known", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
payload: {
type: "something",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
},
],
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("throws an error", () => {
expect(subject.status).toEqual(400)
expect(subject.body.message).toEqual("type must be a valid enum value")
})
})
describe("successfully creates a claim with a reason", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
payload: {
type: "replace",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
reason: "production_failure",
},
],
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("calls ClaimService create", () => {
expect(ClaimServiceMock.create).toHaveBeenCalledTimes(1)
expect(ClaimServiceMock.create).toHaveBeenCalledWith(
expect.objectContaining({
type: "replace",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
reason: "production_failure",
},
],
})
)
})
describe("fails to create a claim when type is not known", () => {
let subject
beforeAll(async () => {
subject = await request(
"POST",
`/admin/orders/${IdMap.getId("test-order")}/claims`,
{
adminSession: {
jwt: {
userId: IdMap.getId("admin_user"),
},
},
payload: {
type: "refund",
claim_items: [
{
item_id: "test-claim-item",
quantity: 1,
reason: "should_throw_error",
},
],
},
}
)
})
afterAll(() => {
jest.clearAllMocks()
})
it("throws an error", () => {
expect(subject.status).toEqual(400)
expect(subject.body.message).toEqual(
"reason must be a valid enum value"
)
})
})
})
})
@@ -318,22 +318,26 @@ export default async (req, res) => {
res.status(idempotencyKey.response_code).json(idempotencyKey.response_body) res.status(idempotencyKey.response_code).json(idempotencyKey.response_body)
} }
enum ClaimTypes { enum ClaimTypeEnum {
replace = "replace", replace = "replace",
refund = "refund", refund = "refund",
} }
enum ClaimItemReason { type ClaimType = `${ClaimTypeEnum}`
enum ClaimItemReasonEnum {
missing_item = "missing_item", missing_item = "missing_item",
wrong_item = "wrong_item", wrong_item = "wrong_item",
production_failure = "production_failure", production_failure = "production_failure",
other = "other", other = "other",
} }
type ClaimItemReasonType = `${ClaimItemReasonEnum}`
export class AdminPostOrdersOrderClaimsReq { export class AdminPostOrdersOrderClaimsReq {
@IsEnum(ClaimTypes) @IsEnum(ClaimTypeEnum)
@IsNotEmpty() @IsNotEmpty()
type: ClaimTypes type: ClaimType
@IsArray() @IsArray()
@IsNotEmpty() @IsNotEmpty()
@@ -414,9 +418,9 @@ class Item {
@IsOptional() @IsOptional()
note?: string note?: string
@IsEnum(ClaimItemReason) @IsEnum(ClaimItemReasonEnum)
@IsOptional() @IsOptional()
reason?: ClaimItemReason reason?: ClaimItemReasonType
@IsArray() @IsArray()
@IsOptional() @IsOptional()
@@ -3,17 +3,20 @@ export const ClaimServiceMock = {
withTransaction: function() { withTransaction: function() {
return this return this
}, },
retrieve: jest.fn().mockImplementation(data => { retrieve: jest.fn().mockImplementation((data) => {
return Promise.resolve({ order_id: IdMap.getId("test-order") }) return Promise.resolve({ order_id: IdMap.getId("test-order") })
}), }),
cancel: jest.fn().mockImplementation(f => { cancel: jest.fn().mockImplementation((f) => {
return Promise.resolve({ f }) return Promise.resolve({ f })
}), }),
cancelFulfillment: jest.fn().mockImplementation(f => { cancelFulfillment: jest.fn().mockImplementation((f) => {
return Promise.resolve({ f }) return Promise.resolve({ f })
}), }),
create: jest.fn().mockImplementation((f) => {
return Promise.resolve(f)
}),
} }
const mock = jest.fn().mockImplementation(() => { const mock = jest.fn().mockImplementation(() => {