Skip to content

Commit 536e5ac

Browse files
committed
added some unit tests for the discount fn
1 parent 26d6f8a commit 536e5ac

File tree

1 file changed

+108
-1
lines changed
  • sample-apps/discounts/extensions/product-discount/src

1 file changed

+108
-1
lines changed

sample-apps/discounts/extensions/product-discount/src/tests.rs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ use super::*;
22
use shopify_function::{run_function_with_input, Result};
33

44
#[test]
5-
fn test_result_contains_no_discounts() -> Result<()> {
5+
fn test_no_metafield_result_contains_no_discounts() -> Result<()> {
66
let result = run_function_with_input(
77
function,
88
r#"
99
{
10+
"cart": {
11+
"lines": [
12+
{
13+
"quantity": 10,
14+
"merchandise": {
15+
"__typename": "ProductVariant",
16+
"id": "gid://shopify/ProductVariant/123456789"
17+
}
18+
}
19+
]
20+
},
1021
"discountNode": {
1122
"metafield": null
1223
}
@@ -18,3 +29,99 @@ fn test_result_contains_no_discounts() -> Result<()> {
1829
assert_eq!(result.discounts.len(), expected);
1930
Ok(())
2031
}
32+
33+
#[test]
34+
fn test_quantity_unmet_result_contains_no_discounts() -> Result<()> {
35+
let result = run_function_with_input(
36+
function,
37+
r#"
38+
{
39+
"cart": {
40+
"lines": [
41+
{
42+
"quantity": 1,
43+
"merchandise": {
44+
"__typename": "ProductVariant",
45+
"id": "gid://shopify/ProductVariant/123456789"
46+
}
47+
}
48+
]
49+
},
50+
"discountNode": {
51+
"metafield": {
52+
"value": "{\"quantity\":2,\"percentage\":5}"
53+
}
54+
}
55+
}
56+
"#,
57+
)?;
58+
let expected = 0;
59+
60+
assert_eq!(result.discounts.len(), expected);
61+
Ok(())
62+
}
63+
64+
#[test]
65+
fn test_quantity_met_discounts_variants() -> Result<()> {
66+
let result = run_function_with_input(
67+
function,
68+
r#"
69+
{
70+
"cart": {
71+
"lines": [
72+
{
73+
"quantity": 2,
74+
"merchandise": {
75+
"__typename": "ProductVariant",
76+
"id": "gid://shopify/ProductVariant/123456789"
77+
}
78+
},
79+
{
80+
"quantity": 3,
81+
"merchandise": {
82+
"__typename": "ProductVariant",
83+
"id": "gid://shopify/ProductVariant/987654321"
84+
}
85+
}
86+
]
87+
},
88+
"discountNode": {
89+
"metafield": {
90+
"value": "{\"quantity\":2,\"percentage\":5}"
91+
}
92+
}
93+
}
94+
"#,
95+
)?;
96+
let expected = output::FunctionResult {
97+
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
98+
discounts: vec![
99+
output::Discount {
100+
message: None,
101+
targets: vec![
102+
output::Target {
103+
product_variant: Some(output::ProductVariantTarget {
104+
id: "gid://shopify/ProductVariant/123456789".to_string(),
105+
quantity: None
106+
})
107+
},
108+
output::Target {
109+
product_variant: Some(output::ProductVariantTarget {
110+
id: "gid://shopify/ProductVariant/987654321".to_string(),
111+
quantity: None
112+
})
113+
},
114+
],
115+
value: output::Value {
116+
fixed_amount: None,
117+
percentage: Some(output::Percentage {
118+
value: "5".to_string()
119+
})
120+
}
121+
}
122+
]
123+
};
124+
125+
assert_eq!(result, expected);
126+
Ok(())
127+
}

0 commit comments

Comments
 (0)