merge develop

This commit is contained in:
Sebastian Rindom
2021-10-15 20:09:31 +02:00
parent 10c87e8d5a
commit 4fd361fddd
207 changed files with 8699 additions and 4124 deletions

View File

@@ -158,6 +158,193 @@ describe("/admin/discounts", () => {
})
)
})
it("creates a discount with start and end dates", 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,
starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/15/2021 17:50"),
},
{
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,
starts_at: expect.any(String),
ends_at: expect.any(String),
})
)
expect(new Date(response.data.discount.starts_at)).toEqual(
new Date("09/15/2021 11:50")
)
expect(new Date(response.data.discount.ends_at)).toEqual(
new Date("09/15/2021 17:50")
)
const updated = await api
.post(
`/admin/discounts/${response.data.discount.id}`,
{
usage_limit: 20,
starts_at: new Date("09/14/2021 11:50"),
ends_at: new Date("09/17/2021 17:50"),
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
console.log(err)
})
expect(updated.status).toEqual(200)
expect(updated.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
usage_limit: 20,
starts_at: expect.any(String),
ends_at: expect.any(String),
})
)
expect(new Date(updated.data.discount.starts_at)).toEqual(
new Date("09/14/2021 11:50")
)
expect(new Date(updated.data.discount.ends_at)).toEqual(
new Date("09/17/2021 17:50")
)
})
it("fails to update end date to a date before start date", async () => {
expect.assertions(6)
const api = useApi()
const response = await api
.post(
"/admin/discounts",
{
code: "HELLOWORLD",
rule: {
description: "test",
type: "percentage",
value: 10,
allocation: "total",
},
usage_limit: 10,
starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/15/2021 17:50"),
},
{
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,
starts_at: expect.any(String),
ends_at: expect.any(String),
})
)
expect(new Date(response.data.discount.starts_at)).toEqual(
new Date("09/15/2021 11:50")
)
expect(new Date(response.data.discount.ends_at)).toEqual(
new Date("09/15/2021 17:50")
)
await api
.post(
`/admin/discounts/${response.data.discount.id}`,
{
usage_limit: 20,
ends_at: new Date("09/11/2021 17:50"),
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual(
`"ends_at" must be greater than "starts_at"`
)
})
})
it("fails to create discount with end date before start date", async () => {
expect.assertions(2)
const api = useApi()
const response = await api
.post(
"/admin/discounts",
{
code: "HELLOWORLD",
rule: {
description: "test",
type: "percentage",
value: 10,
allocation: "total",
},
usage_limit: 10,
starts_at: new Date("09/15/2021 11:50"),
ends_at: new Date("09/14/2021 17:50"),
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toEqual([
expect.objectContaining({
message: `"ends_at" must be greater than "ref:starts_at"`,
}),
])
})
})
})
describe("testing for soft-deletion + uniqueness on discount codes", () => {
@@ -286,6 +473,21 @@ describe("/admin/discounts", () => {
is_dynamic: true,
is_disabled: false,
rule_id: "test-discount-rule",
valid_duration: "P2Y",
})
await manager.insert(DiscountRule, {
id: "test-discount-rule1",
description: "Dynamic rule",
type: "percentage",
value: 10,
allocation: "total",
})
await manager.insert(Discount, {
id: "test-discount1",
code: "DYNAMICCode",
is_dynamic: true,
is_disabled: false,
rule_id: "test-discount-rule1",
})
} catch (err) {
console.log(err)
@@ -298,7 +500,7 @@ describe("/admin/discounts", () => {
await db.teardown()
})
it("creates a dynamic discount", async () => {
it("creates a dynamic discount with ends_at", async () => {
const api = useApi()
const response = await api
@@ -318,6 +520,40 @@ describe("/admin/discounts", () => {
})
expect(response.status).toEqual(200)
expect(response.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
ends_at: expect.any(String),
})
)
})
it("creates a dynamic discount without ends_at", async () => {
const api = useApi()
const response = await api
.post(
"/admin/discounts/test-discount1/dynamic-codes",
{
code: "HELLOWORLD",
},
{
headers: {
Authorization: "Bearer test_token",
},
}
)
.catch((err) => {
// console.log(err)
})
expect(response.status).toEqual(200)
expect(response.data.discount).toEqual(
expect.objectContaining({
code: "HELLOWORLD",
ends_at: null,
})
)
})
})
})