feat(medusa): Add proper pagination (#4517)
* update method for listing regions * add changeset * fix unit tests * listAndCount swaps * add count calculation to list-returns * swap integration test * notes pagination * pagination props for notifications * listAndCount store regions * fix nit * fix note unit test * update list-regions store unit test * cleanup integration test * rename introduced tests --------- Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
1 parent
55db914151
commit
5b91a3503a
21 files changed
+401
-192
No files matched your search
@@ -163,6 +163,22 @@ describe("/admin/notes", () => {
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
it("should list the notes with correct count in pagination", async () => {
|
||||
const api = useApi()
|
||||
const response = await api
|
||||
.get("/admin/notes?limit=2", {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
expect(response.data.notes.length).toEqual(2)
|
||||
expect(response.data.count).toEqual(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe("POST /admin/notes/:id", () => {
|
||||
|
||||
@@ -24,6 +24,12 @@ const {
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const adminHeaders = {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
|
||||
describe("/admin/swaps", () => {
|
||||
describe("tax exclusive", () => {
|
||||
let medusaProcess
|
||||
@@ -57,11 +63,7 @@ describe("/admin/swaps", () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/test-swap", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get("/admin/swaps/test-swap", adminHeaders)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
@@ -88,11 +90,7 @@ describe("/admin/swaps", () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/disc-swap", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get("/admin/swaps/disc-swap", adminHeaders)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
@@ -131,11 +129,7 @@ describe("/admin/swaps", () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api
|
||||
.get("/admin/swaps/", {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get("/admin/swaps/", adminHeaders)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
@@ -150,6 +144,17 @@ describe("/admin/swaps", () => {
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("should list the swaps with correct pagination", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/admin/swaps?limit=5", adminHeaders)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
expect(response.data.count).toBe(8)
|
||||
expect(response.data.limit).toBe(5)
|
||||
expect(response.data.swaps.length).toBe(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Complete swap flow", () => {
|
||||
@@ -268,11 +273,7 @@ describe("/admin/swaps", () => {
|
||||
{
|
||||
items: [{ item_id: "line-item", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const fulfillmentId = fulfilledOrder.data.order.fulfillments[0].id
|
||||
@@ -282,22 +283,10 @@ describe("/admin/swaps", () => {
|
||||
{
|
||||
fulfillment_id: fulfillmentId,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/orders/${orderId}/capture`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
await api.post(`/admin/orders/${orderId}/capture`, {}, adminHeaders)
|
||||
|
||||
// ********* CREATE SWAP *********
|
||||
const createSwap = await api.post(
|
||||
@@ -311,11 +300,7 @@ describe("/admin/swaps", () => {
|
||||
],
|
||||
additional_items: [{ variant_id: "prod-b-var", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
let swap = createSwap.data.order.swaps[0]
|
||||
@@ -335,11 +320,7 @@ describe("/admin/swaps", () => {
|
||||
await api.post(`/store/carts/${swap.cart_id}/complete`)
|
||||
|
||||
swap = await api
|
||||
.get(`/admin/swaps/${swap.id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get(`/admin/swaps/${swap.id}`, adminHeaders)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
@@ -495,11 +476,7 @@ describe("/admin/swaps", () => {
|
||||
{
|
||||
items: [{ item_id: "line-item", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const fulfillmentId = fulfilledOrder.data.order.fulfillments[0].id
|
||||
@@ -509,22 +486,10 @@ describe("/admin/swaps", () => {
|
||||
{
|
||||
fulfillment_id: fulfillmentId,
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
await api.post(
|
||||
`/admin/orders/${orderId}/capture`,
|
||||
{},
|
||||
{
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
)
|
||||
await api.post(`/admin/orders/${orderId}/capture`, {}, adminHeaders)
|
||||
|
||||
// ********* CREATE SWAP *********
|
||||
const createSwap = await api.post(
|
||||
@@ -538,11 +503,7 @@ describe("/admin/swaps", () => {
|
||||
],
|
||||
additional_items: [{ variant_id: "prod-b-var", quantity: 1 }],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
let swap = createSwap.data.order.swaps[0]
|
||||
@@ -562,11 +523,7 @@ describe("/admin/swaps", () => {
|
||||
await api.post(`/store/carts/${swap.cart_id}/complete`)
|
||||
|
||||
swap = await api
|
||||
.get(`/admin/swaps/${swap.id}`, {
|
||||
headers: {
|
||||
Authorization: "Bearer test_token",
|
||||
},
|
||||
})
|
||||
.get(`/admin/swaps/${swap.id}`, adminHeaders)
|
||||
.catch((err) => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
@@ -18,6 +18,12 @@ const {
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
const adminHeaders = {
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
|
||||
describe("/admin/orders", () => {
|
||||
let medusaProcess
|
||||
let dbConnection
|
||||
@@ -60,11 +66,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -103,11 +105,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -156,11 +154,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -215,11 +209,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -364,11 +354,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
@@ -421,7 +407,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{ headers: { authorization: "Bearer test_token" } }
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const claimId = createRes.data.order.claims[0].id
|
||||
@@ -430,7 +416,7 @@ describe("/admin/orders", () => {
|
||||
const claimFulfillmentCreatedResponse = await api.post(
|
||||
`/admin/orders/${order.id}/claims/${claimId}/fulfillments`,
|
||||
{},
|
||||
{ headers: { authorization: "Bearer test_token" } }
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const fulfillmentId =
|
||||
@@ -438,7 +424,7 @@ describe("/admin/orders", () => {
|
||||
await api.post(
|
||||
`/admin/orders/${order.id}/claims/${claimId}/shipments`,
|
||||
{ fulfillment_id: fulfillmentId },
|
||||
{ headers: { authorization: "Bearer test_token" } }
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const returnCreatedResponse = await api.post(
|
||||
@@ -452,11 +438,7 @@ describe("/admin/orders", () => {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
const returnOrder = returnCreatedResponse.data.order.returns[0]
|
||||
@@ -469,23 +451,56 @@ describe("/admin/orders", () => {
|
||||
quantity: i.quantity,
|
||||
})),
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
authorization: "Bearer test_token",
|
||||
},
|
||||
}
|
||||
adminHeaders
|
||||
)
|
||||
|
||||
expect(returnReceivedResponse.status).toEqual(200)
|
||||
})
|
||||
|
||||
it("shoutl list the returns with correct pagination", async () => {
|
||||
await adminSeeder(dbConnection)
|
||||
const api = useApi()
|
||||
|
||||
const createOrderAndReturn = async (suffix = "") => {
|
||||
const order = await createReturnableOrder(
|
||||
dbConnection,
|
||||
{
|
||||
discount: false,
|
||||
oldTaxes: false,
|
||||
},
|
||||
suffix
|
||||
)
|
||||
|
||||
const response = await api.post(
|
||||
`/admin/orders/${order.id}/return`,
|
||||
{
|
||||
items: [
|
||||
{
|
||||
item_id: "test-item" + suffix,
|
||||
quantity: 1,
|
||||
note: "TOO SMALL",
|
||||
},
|
||||
],
|
||||
},
|
||||
adminHeaders
|
||||
)
|
||||
}
|
||||
|
||||
await Promise.all([createOrderAndReturn(), createOrderAndReturn("-1")])
|
||||
|
||||
const result = await api.get(`/admin/returns?limit=1`, adminHeaders)
|
||||
|
||||
expect(result.status).toEqual(200)
|
||||
expect(result.data.count).toEqual(2)
|
||||
})
|
||||
})
|
||||
|
||||
const createReturnableOrder = async (dbConnection, options) => {
|
||||
const createReturnableOrder = async (dbConnection, options, suffix = "") => {
|
||||
await simpleProductFactory(
|
||||
dbConnection,
|
||||
{
|
||||
id: "test-product",
|
||||
variants: [{ id: "test-variant" }],
|
||||
id: "test-product" + suffix,
|
||||
variants: [{ id: "test-variant" + suffix }],
|
||||
},
|
||||
100
|
||||
)
|
||||
@@ -495,24 +510,24 @@ const createReturnableOrder = async (dbConnection, options) => {
|
||||
if (options.discount) {
|
||||
discounts = [
|
||||
{
|
||||
code: "TESTCODE",
|
||||
code: "TESTCODE" + suffix,
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
return await simpleOrderFactory(dbConnection, {
|
||||
email: "test@testson.com",
|
||||
email: "test@testson.com" + suffix,
|
||||
tax_rate: options.oldTaxes ? undefined : null,
|
||||
region: {
|
||||
id: "test-region",
|
||||
id: "test-region" + suffix,
|
||||
name: "Test region",
|
||||
tax_rate: 12.5, // Should be ignored due to item tax line
|
||||
},
|
||||
discounts,
|
||||
line_items: [
|
||||
{
|
||||
id: "test-item",
|
||||
variant_id: "test-variant",
|
||||
id: "test-item" + suffix,
|
||||
variant_id: "test-variant" + suffix,
|
||||
quantity: 2,
|
||||
fulfilled_quantity: options.shipped ? 2 : undefined,
|
||||
shipped_quantity: options.shipped ? 2 : undefined,
|
||||
@@ -521,9 +536,9 @@ const createReturnableOrder = async (dbConnection, options) => {
|
||||
? [
|
||||
{
|
||||
amount: 200,
|
||||
discount_code: "TESTCODE",
|
||||
discount_code: "TESTCODE" + suffix,
|
||||
description: "discount",
|
||||
item_id: "test-item",
|
||||
item_id: "test-item" + suffix,
|
||||
},
|
||||
]
|
||||
: [],
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
const path = require("path")
|
||||
const {
|
||||
Region,
|
||||
ReturnReason,
|
||||
Order,
|
||||
Customer,
|
||||
ShippingProfile,
|
||||
Product,
|
||||
ProductVariant,
|
||||
ShippingOption,
|
||||
FulfillmentProvider,
|
||||
LineItem,
|
||||
Discount,
|
||||
DiscountRule,
|
||||
} = require("@medusajs/medusa")
|
||||
|
||||
const setupServer = require("../../../helpers/setup-server")
|
||||
const { useApi } = require("../../../helpers/use-api")
|
||||
const { initDb, useDb } = require("../../../helpers/use-db")
|
||||
|
||||
jest.setTimeout(30000)
|
||||
|
||||
describe("/store/carts", () => {
|
||||
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("GET /store/regions", () => {
|
||||
beforeEach(async () => {
|
||||
const manager = dbConnection.manager
|
||||
|
||||
await manager.insert(Region, {
|
||||
id: "region",
|
||||
name: "Test Region",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
})
|
||||
await manager.insert(Region, {
|
||||
id: "region-1",
|
||||
name: "Test Region 1",
|
||||
currency_code: "usd",
|
||||
tax_rate: 0,
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
const db = useDb()
|
||||
await db.teardown()
|
||||
})
|
||||
|
||||
it("should list the store regions with pagination", async () => {
|
||||
const api = useApi()
|
||||
|
||||
const response = await api.get("/store/regions?limit=1&offset=1")
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
|
||||
expect(response.data.regions.length).toEqual(1)
|
||||
expect(response.data.count).toEqual(2)
|
||||
expect(response.data.offset).toEqual(1)
|
||||
expect(response.data.limit).toEqual(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user