fix: api claim types + tests (#967)
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import { request } from "../../../../../helpers/test-request"
|
||||
import { InviteServiceMock } from "../../../../../services/__mocks__/invite"
|
||||
import { UserRole } from "../../../../../types/user"
|
||||
|
||||
describe("POST /invites", () => {
|
||||
describe("checks validation rules", () => {
|
||||
describe("checks that role must not be empty", () => {
|
||||
let subject
|
||||
|
||||
beforeAll(async () => {
|
||||
@@ -26,4 +27,35 @@ describe("POST /invites", () => {
|
||||
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)
|
||||
}
|
||||
|
||||
enum ClaimTypes {
|
||||
enum ClaimTypeEnum {
|
||||
replace = "replace",
|
||||
refund = "refund",
|
||||
}
|
||||
|
||||
enum ClaimItemReason {
|
||||
type ClaimType = `${ClaimTypeEnum}`
|
||||
|
||||
enum ClaimItemReasonEnum {
|
||||
missing_item = "missing_item",
|
||||
wrong_item = "wrong_item",
|
||||
production_failure = "production_failure",
|
||||
other = "other",
|
||||
}
|
||||
|
||||
type ClaimItemReasonType = `${ClaimItemReasonEnum}`
|
||||
|
||||
export class AdminPostOrdersOrderClaimsReq {
|
||||
@IsEnum(ClaimTypes)
|
||||
@IsEnum(ClaimTypeEnum)
|
||||
@IsNotEmpty()
|
||||
type: ClaimTypes
|
||||
type: ClaimType
|
||||
|
||||
@IsArray()
|
||||
@IsNotEmpty()
|
||||
@@ -414,9 +418,9 @@ class Item {
|
||||
@IsOptional()
|
||||
note?: string
|
||||
|
||||
@IsEnum(ClaimItemReason)
|
||||
@IsEnum(ClaimItemReasonEnum)
|
||||
@IsOptional()
|
||||
reason?: ClaimItemReason
|
||||
reason?: ClaimItemReasonType
|
||||
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
|
||||
@@ -3,17 +3,20 @@ export const ClaimServiceMock = {
|
||||
withTransaction: function() {
|
||||
return this
|
||||
},
|
||||
retrieve: jest.fn().mockImplementation(data => {
|
||||
retrieve: jest.fn().mockImplementation((data) => {
|
||||
return Promise.resolve({ order_id: IdMap.getId("test-order") })
|
||||
}),
|
||||
|
||||
cancel: jest.fn().mockImplementation(f => {
|
||||
cancel: jest.fn().mockImplementation((f) => {
|
||||
return Promise.resolve({ f })
|
||||
}),
|
||||
|
||||
cancelFulfillment: jest.fn().mockImplementation(f => {
|
||||
cancelFulfillment: jest.fn().mockImplementation((f) => {
|
||||
return Promise.resolve({ f })
|
||||
}),
|
||||
create: jest.fn().mockImplementation((f) => {
|
||||
return Promise.resolve(f)
|
||||
}),
|
||||
}
|
||||
|
||||
const mock = jest.fn().mockImplementation(() => {
|
||||
|
||||
Reference in New Issue
Block a user