Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(medusa): Update shipping option price type #5895

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 83 additions & 89 deletions integration-tests/api/__tests__/admin/shipping-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("/admin/shipping-options", () => {
await db.teardown()
})

it("updates a shipping option with no existing requirements", async () => {
it("should update a shipping option with no existing requirements", async () => {
const api = useApi()

const payload = {
Expand All @@ -66,11 +66,11 @@ describe("/admin/shipping-options", () => {
],
}

const res = await api.post(`/admin/shipping-options/test-out`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.post(
`/admin/shipping-options/test-out`,
payload,
adminReqConfig
)

const requirements = res.data.shipping_option.requirements

Expand All @@ -92,7 +92,27 @@ describe("/admin/shipping-options", () => {
)
})

it("fails to add a a requirement with an id if it does not exists", async () => {
it("should update a shipping option with price_type", async () => {
const api = useApi()

const payload = {
price_type: "calculated",
requirements: [],
}

const res = await api
.post(`/admin/shipping-options/test-out`, payload, adminReqConfig)
.catch(console.log)

expect(res.status).toEqual(200)
expect(res.data.shipping_option).toEqual(
expect.objectContaining({
price_type: "calculated",
})
)
})

it("should fail to add a a requirement with an id if it does not exists", async () => {
const api = useApi()

const payload = {
Expand All @@ -113,11 +133,7 @@ describe("/admin/shipping-options", () => {
}

const res = await api
.post(`/admin/shipping-options/test-out`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.post(`/admin/shipping-options/test-out`, payload, adminReqConfig)
.catch((err) => {
return err.response
})
Expand All @@ -128,7 +144,7 @@ describe("/admin/shipping-options", () => {
)
})

it("it successfully updates a set of existing requirements", async () => {
it("should successfully updates a set of existing requirements", async () => {
const api = useApi()

const payload = {
Expand All @@ -148,19 +164,19 @@ describe("/admin/shipping-options", () => {
}

const res = await api
.post(`/admin/shipping-options/test-option-req`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.post(
`/admin/shipping-options/test-option-req`,
payload,
adminReqConfig
)
.catch((err) => {
console.log(err.response.data.message)
})

expect(res.status).toEqual(200)
})

it("it successfully updates a set of existing requirements by updating one and deleting the other", async () => {
it("should successfully updates a set of existing requirements by updating one and deleting the other", async () => {
const api = useApi()

const payload = {
Expand All @@ -174,19 +190,19 @@ describe("/admin/shipping-options", () => {
}

const res = await api
.post(`/admin/shipping-options/test-option-req`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.post(
`/admin/shipping-options/test-option-req`,
payload,
adminReqConfig
)
.catch((err) => {
console.log(err.response.data.message)
})

expect(res.status).toEqual(200)
})

it("successfully updates a set of requirements because max. subtotal >= min. subtotal", async () => {
it("should successfully updates a set of requirements because max. subtotal >= min. subtotal", async () => {
const api = useApi()

const payload = {
Expand All @@ -205,11 +221,11 @@ describe("/admin/shipping-options", () => {
}

const res = await api
.post(`/admin/shipping-options/test-option-req`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.post(
`/admin/shipping-options/test-option-req`,
payload,
adminReqConfig
)
.catch((err) => {
console.log(err.response.data.message)
})
Expand All @@ -219,7 +235,7 @@ describe("/admin/shipping-options", () => {
expect(res.data.shipping_option.requirements[1].amount).toEqual(200)
})

it("fails to updates a set of requirements because max. subtotal <= min. subtotal", async () => {
it("should fail to updates a set of requirements because max. subtotal <= min. subtotal", async () => {
const api = useApi()

const payload = {
Expand All @@ -238,11 +254,11 @@ describe("/admin/shipping-options", () => {
}

const res = await api
.post(`/admin/shipping-options/test-option-req`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
.post(
`/admin/shipping-options/test-option-req`,
payload,
adminReqConfig
)
.catch((err) => {
return err.response
})
Expand All @@ -267,11 +283,7 @@ describe("/admin/shipping-options", () => {
{
fulfillment_providers: ["test-ful"],
},
{
headers: {
"x-medusa-access-token": "test_token",
},
}
adminReqConfig
)

const manager = dbConnection.manager
Expand All @@ -297,7 +309,7 @@ describe("/admin/shipping-options", () => {
await db.teardown()
})

it("creates a shipping option with requirements", async () => {
it("should create a shipping option with requirements", async () => {
const api = useApi()
payload.requirements = [
{
Expand All @@ -310,29 +322,29 @@ describe("/admin/shipping-options", () => {
},
]

const res = await api.post(`/admin/shipping-options`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.post(
`/admin/shipping-options`,
payload,
adminReqConfig
)

expect(res.status).toEqual(200)
expect(res.data.shipping_option.requirements.length).toEqual(2)
})

it("creates a shipping option with no requirements", async () => {
it("should create a shipping option with no requirements", async () => {
const api = useApi()
const res = await api.post(`/admin/shipping-options`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.post(
`/admin/shipping-options`,
payload,
adminReqConfig
)

expect(res.status).toEqual(200)
expect(res.data.shipping_option.requirements.length).toEqual(0)
})

it("fails on same requirement types", async () => {
it("should fail on same requirement types", async () => {
const api = useApi()
payload.requirements = [
{
Expand All @@ -346,19 +358,15 @@ describe("/admin/shipping-options", () => {
]

try {
await api.post(`/admin/shipping-options`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
await api.post(`/admin/shipping-options`, payload, adminReqConfig)
} catch (error) {
expect(error.response.data.message).toEqual(
"Only one requirement of each type is allowed"
)
}
})

it("fails when min_subtotal > max_subtotal", async () => {
it("should fail when min_subtotal > max_subtotal", async () => {
const api = useApi()
payload.requirements = [
{
Expand All @@ -372,11 +380,7 @@ describe("/admin/shipping-options", () => {
]

try {
await api.post(`/admin/shipping-options`, payload, {
headers: {
"x-medusa-access-token": "test_token",
},
})
await api.post(`/admin/shipping-options`, payload, adminReqConfig)
} catch (error) {
expect(error.response.data.message).toEqual(
"Max. subtotal must be greater than Min. subtotal"
Expand All @@ -395,24 +399,19 @@ describe("/admin/shipping-options", () => {
await db.teardown()
})

it("lists shipping options", async () => {
it("should list shipping options", async () => {
const api = useApi()
const res = await api.get(`/admin/shipping-options`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.get(`/admin/shipping-options`, adminReqConfig)

expect(res.status).toEqual(200)
})

it("lists admin only shipping options", async () => {
it("should list admin only shipping options", async () => {
const api = useApi()
const res = await api.get(`/admin/shipping-options?admin_only=true`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.get(
`/admin/shipping-options?admin_only=true`,
adminReqConfig
)

expect(res.status).toEqual(200)
expect(res.data.shipping_options).toEqual(
Expand All @@ -425,13 +424,12 @@ describe("/admin/shipping-options", () => {
)
})

it("lists return shipping options", async () => {
it("should list return shipping options", async () => {
const api = useApi()
const res = await api.get(`/admin/shipping-options?is_return=true`, {
headers: {
"x-medusa-access-token": "test_token",
},
})
const res = await api.get(
`/admin/shipping-options?is_return=true`,
adminReqConfig
)

expect(res.status).toEqual(200)
expect(res.data.shipping_options).toEqual(
Expand All @@ -444,15 +442,11 @@ describe("/admin/shipping-options", () => {
)
})

it("lists shipping options without return and admin options", async () => {
it("should list shipping options without return and admin options", async () => {
const api = useApi()
const res = await api.get(
`/admin/shipping-options?is_return=false&admin_only=true`,
{
headers: {
"x-medusa-access-token": "test_token",
},
}
adminReqConfig
)

expect(res.status).toEqual(200)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ShippingOptionServiceMock } from "../../../../../services/__mocks__/shipping-option"
import { request } from "../../../../../helpers/test-request"

describe("POST /admin/shipping-options", () => {
describe("successful creation", () => {
Expand All @@ -13,6 +13,7 @@ describe("POST /admin/shipping-options", () => {
name: "Test option",
region_id: "testregion",
provider_id: "test_provider",
profile_id: "gift-card",
data: { id: "test" },
price_type: "flat_rate",
amount: 100,
Expand Down
Loading