|
1
|
import { HttpTypes } from "@medusajs/types" |
|
2
|
import i18next from "i18next" |
|
3
|
import { describe, expect, it } from "vitest" |
|
4
|
|
|
5
|
// promotionStatusMap resolves its labels through i18n.t at module load, so |
|
6
|
// i18next has to be initialized before the module under test is imported. |
|
7
|
await i18next.init({ lng: "en", fallbackLng: "en", resources: {} }) |
|
8
|
|
|
9
|
const { getPromotionStatus, promotionStatusMap, PromotionStatus } = |
|
10
|
await import("../promotions") |
|
11
|
|
|
12
|
const buildPromotion = ( |
|
13
|
campaign: Partial<HttpTypes.AdminCampaign> |
|
14
|
): HttpTypes.AdminPromotion => |
|
15
|
({ |
|
16
|
status: "active", |
|
17
|
campaign: { |
|
18
|
starts_at: null, |
|
19
|
ends_at: null, |
|
20
|
...campaign, |
|
21
|
}, |
|
22
|
} as HttpTypes.AdminPromotion) |
|
23
|
|
|
24
|
describe("getPromotionStatus", () => { |
|
25
|
it("should not mark a promotion as expired when a use_by_attribute budget's aggregate usage exceeds the per-attribute limit", () => { |
|
26
|
const promotion = buildPromotion({ |
|
27
|
budget: { |
|
28
|
type: "use_by_attribute", |
|
29
|
limit: 1, |
|
30
|
used: 5, |
|
31
|
} as HttpTypes.AdminCampaign["budget"], |
|
32
|
}) |
|
33
|
|
|
34
|
expect(getPromotionStatus(promotion)).toEqual( |
|
35
|
promotionStatusMap[PromotionStatus.ACTIVE] |
|
36
|
) |
|
37
|
}) |
|
38
|
|
|
39
|
it("should not mark a promotion as expired when a spend_by_attribute budget's aggregate usage exceeds the per-attribute limit", () => { |
|
40
|
const promotion = buildPromotion({ |
|
41
|
budget: { |
|
42
|
type: "spend_by_attribute", |
|
43
|
limit: 100, |
|
44
|
used: 500, |
|
45
|
} as HttpTypes.AdminCampaign["budget"], |
|
46
|
}) |
|
47
|
|
|
48
|
expect(getPromotionStatus(promotion)).toEqual( |
|
49
|
promotionStatusMap[PromotionStatus.ACTIVE] |
|
50
|
) |
|
51
|
}) |
|
52
|
|
|
53
|
it("should mark a promotion as expired when a global budget is exceeded", () => { |
|
54
|
const promotion = buildPromotion({ |
|
55
|
budget: { |
|
56
|
type: "usage", |
|
57
|
limit: 1, |
|
58
|
used: 5, |
|
59
|
} as HttpTypes.AdminCampaign["budget"], |
|
60
|
}) |
|
61
|
|
|
62
|
expect(getPromotionStatus(promotion)).toEqual( |
|
63
|
promotionStatusMap[PromotionStatus.EXPIRED] |
|
64
|
) |
|
65
|
}) |
|
66
|
|
|
67
|
it("should mark a promotion as expired when its campaign has ended, regardless of budget type", () => { |
|
68
|
const promotion = buildPromotion({ |
|
69
|
ends_at: "2020-01-01T00:00:00.000Z", |
|
70
|
budget: { |
|
71
|
type: "use_by_attribute", |
|
72
|
limit: 1, |
|
73
|
used: 0, |
|
74
|
} as HttpTypes.AdminCampaign["budget"], |
|
75
|
}) |
|
76
|
|
|
77
|
expect(getPromotionStatus(promotion)).toEqual( |
|
78
|
promotionStatusMap[PromotionStatus.EXPIRED] |
|
79
|
) |
|
80
|
}) |
|
81
|
}) |