Skip to content

Commit d9240e9

Browse files
authored
Merge pull request #608 from Shopify/js-discount-split-tests
Split Discount JS template test files
2 parents a7d057e + 24f03d9 commit d9240e9

File tree

2 files changed

+160
-16
lines changed

2 files changed

+160
-16
lines changed

discounts/javascript/discount/default/src/discount.test.liquid renamed to discounts/javascript/discount/default/src/generate_cart_run.test.liquid

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
{%- if flavor contains "vanilla-js" -%}
12
import { describe, it, expect } from "vitest";
23

34
import { generateCartRun } from "./generate_cart_run";
4-
import { generateDeliveryRun } from "./generate_delivery_run";
55
import {
66
OrderDiscountSelectionStrategy,
77
ProductDiscountSelectionStrategy,
8-
DeliveryDiscountSelectionStrategy,
98
} from "../generated/api";
109

11-
describe("discount", () => {
12-
it("cart.lines.discounts.generate.run returns a product and order discount", () => {
10+
/**
11+
* @typedef {import("../generated/api").CartLinesDiscountsGenerateRunResult} CartLinesDiscountsGenerateRunResult
12+
*/
13+
14+
describe("generateCartRun", () => {
15+
it("returns a product and order discount", () => {
1316
const input = {
1417
cart: {
1518
lines: [
@@ -72,42 +75,81 @@ describe("discount", () => {
7275
},
7376
});
7477
});
78+
});
79+
{%- elsif flavor contains "typescript" -%}
80+
import { describe, it, expect } from "vitest";
7581

76-
it("cart.delivery-options.generate.run returns a delivery discount", () => {
77-
const input = {
82+
import { generateCartRun } from "./generate_cart_run";
83+
import {
84+
OrderDiscountSelectionStrategy,
85+
ProductDiscountSelectionStrategy,
86+
CartInput,
87+
CartLinesDiscountsGenerateRunResult
88+
} from "../generated/api";
89+
90+
describe("generateCartRun", () => {
91+
it("returns a product and order discount", () => {
92+
const input: CartInput = {
7893
cart: {
79-
deliveryGroups: [
94+
lines: [
8095
{
81-
id: "gid://shopify/DeliveryGroup/0",
96+
id: "gid://shopify/CartLine/0",
97+
cost: {
98+
subtotalAmount: 100,
99+
},
82100
},
83101
],
84102
},
85103
};
86104

87-
const result = generateDeliveryRun(input);
105+
const result: CartLinesDiscountsGenerateRunResult = generateCartRun(input);
88106

89-
expect(result.operations.length).toBe(1);
107+
expect(result.operations.length).toBe(2);
90108
expect(result.operations[0]).toMatchObject({
91-
deliveryDiscountsAdd: {
109+
orderDiscountsAdd: {
92110
candidates: [
93111
{
94-
message: "FREE DELIVERY",
112+
message: "10% OFF ORDER",
95113
targets: [
96114
{
97-
deliveryGroup: {
98-
id: "gid://shopify/DeliveryGroup/0",
115+
orderSubtotal: {
116+
excludedCartLineIds: [],
99117
},
100118
},
101119
],
102120
value: {
103121
percentage: {
104-
value: 100,
122+
value: 10,
105123
},
106124
},
107125
},
108126
],
109-
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
127+
selectionStrategy: OrderDiscountSelectionStrategy.First,
128+
},
129+
});
130+
131+
expect(result.operations[1]).toMatchObject({
132+
productDiscountsAdd: {
133+
candidates: [
134+
{
135+
message: "20% OFF PRODUCT",
136+
targets: [
137+
{
138+
cartLine: {
139+
id: "gid://shopify/CartLine/0",
140+
},
141+
},
142+
],
143+
value: {
144+
percentage: {
145+
value: 20,
146+
},
147+
},
148+
},
149+
],
150+
selectionStrategy: ProductDiscountSelectionStrategy.First,
110151
},
111152
});
112153
});
113154
});
155+
{%- endif -%}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{%- if flavor contains "vanilla-js" -%}
2+
import { describe, it, expect } from "vitest";
3+
4+
import { generateDeliveryRun } from "./generate_delivery_run";
5+
import {
6+
DeliveryDiscountSelectionStrategy,
7+
} from "../generated/api";
8+
9+
/**
10+
* @typedef {import("../generated/api").CartDeliveryOptionsDiscountsGenerateRunResult} CartDeliveryOptionsDiscountsGenerateRunResult
11+
* @typedef {import("../generated/api").DeliveryInput} DeliveryInput
12+
*/
13+
14+
describe("generateDeliveryRun", () => {
15+
it("returns a delivery discount", () => {
16+
const input = /** @type {DeliveryInput} */ ({
17+
cart: {
18+
deliveryGroups: [
19+
{
20+
id: "gid://shopify/DeliveryGroup/0",
21+
},
22+
],
23+
},
24+
});
25+
26+
const result = generateDeliveryRun(input);
27+
28+
expect(result.operations.length).toBe(1);
29+
expect(result.operations[0]).toMatchObject({
30+
deliveryDiscountsAdd: {
31+
candidates: [
32+
{
33+
message: "FREE DELIVERY",
34+
targets: [
35+
{
36+
deliveryGroup: {
37+
id: "gid://shopify/DeliveryGroup/0",
38+
},
39+
},
40+
],
41+
value: {
42+
percentage: {
43+
value: 100,
44+
},
45+
},
46+
},
47+
],
48+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
49+
},
50+
});
51+
});
52+
});
53+
{%- elsif flavor contains "typescript" -%}
54+
import { describe, it, expect } from "vitest";
55+
56+
import { generateDeliveryRun } from "./generate_delivery_run";
57+
import {
58+
DeliveryDiscountSelectionStrategy,
59+
DeliveryInput,
60+
CartDeliveryOptionsDiscountsGenerateRunResult
61+
} from "../generated/api";
62+
63+
describe("generateDeliveryRun", () => {
64+
it("returns a delivery discount", () => {
65+
const input: DeliveryInput = {
66+
cart: {
67+
deliveryGroups: [
68+
{
69+
id: "gid://shopify/DeliveryGroup/0",
70+
},
71+
],
72+
},
73+
};
74+
75+
const result: CartDeliveryOptionsDiscountsGenerateRunResult = generateDeliveryRun(input);
76+
77+
expect(result.operations.length).toBe(1);
78+
expect(result.operations[0]).toMatchObject({
79+
deliveryDiscountsAdd: {
80+
candidates: [
81+
{
82+
message: "FREE DELIVERY",
83+
targets: [
84+
{
85+
deliveryGroup: {
86+
id: "gid://shopify/DeliveryGroup/0",
87+
},
88+
},
89+
],
90+
value: {
91+
percentage: {
92+
value: 100,
93+
},
94+
},
95+
},
96+
],
97+
selectionStrategy: DeliveryDiscountSelectionStrategy.All,
98+
},
99+
});
100+
});
101+
});
102+
{%- endif -%}

0 commit comments

Comments
 (0)