Skip to content

Commit 9f826ec

Browse files
committed
unit test improvements for discount tutorial sample
1 parent 4ca7082 commit 9f826ec

File tree

2 files changed

+93
-39
lines changed

2 files changed

+93
-39
lines changed

.graphqlrc.cjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Configures the GraphQL language server for all the function schemas in this repo.
3+
*/
4+
const fs = require('node:fs');
5+
6+
function getProjects(path) {
7+
const projects = {}
8+
9+
const extensions = fs.readdirSync(`./${path}`);
10+
for (const entry of extensions) {
11+
const extensionPath = `./${path}/${entry}`;
12+
const schema = `${extensionPath}/schema.graphql`;
13+
if(!fs.existsSync(schema)) {
14+
continue;
15+
}
16+
17+
const projectName = extensionPath.substring(2).replaceAll('/', '-');
18+
projects[projectName] = {
19+
schema,
20+
documents: `${extensionPath}/input.graphql`
21+
}
22+
}
23+
24+
return projects;
25+
}
26+
27+
const projects = {
28+
...getProjects("sample-apps/discounts-tutorial/extensions"),
29+
...getProjects("checkout/rust/delivery-customization"),
30+
...getProjects("checkout/rust/payment-customization"),
31+
...getProjects("discounts/rust/order-discounts"),
32+
...getProjects("discounts/rust/product-discounts"),
33+
...getProjects("discounts/rust/shipping-discounts"),
34+
}
35+
36+
module.exports = {
37+
projects
38+
};

sample-apps/discounts-tutorial/extensions/volume/src/tests.rs

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ fn test_discount_with_no_configuration() -> Result<()> {
77
function,
88
r#"
99
{
10-
"cart": {
11-
"lines": [
12-
{
13-
"quantity": 5,
14-
"merchandise": {
15-
"__typename": "ProductVariant",
16-
"id": "gid://shopify/ProductVariant/0"
17-
}
10+
"cart": {
11+
"lines": [
12+
{
13+
"quantity": 5,
14+
"merchandise": {
15+
"__typename": "ProductVariant",
16+
"id": "gid://shopify/ProductVariant/0"
1817
}
19-
]
20-
},
21-
"discountNode": {
22-
"metafield": null
23-
}
18+
}
19+
]
20+
},
21+
"discountNode": {
22+
"metafield": null
2423
}
24+
}
2525
"#,
2626
)?;
2727
let expected = 0;
@@ -36,39 +36,55 @@ fn test_discount_with_configuration() -> Result<()> {
3636
function,
3737
r#"
3838
{
39-
"cart": {
40-
"lines": [
41-
{
42-
"quantity": 5,
43-
"merchandise": {
44-
"__typename": "ProductVariant",
45-
"id": "gid://shopify/ProductVariant/0"
46-
}
47-
},
48-
{
49-
"quantity": 1,
50-
"merchandise": {
51-
"__typename": "ProductVariant",
52-
"id": "gid://shopify/ProductVariant/1"
53-
}
39+
"cart": {
40+
"lines": [
41+
{
42+
"quantity": 5,
43+
"merchandise": {
44+
"__typename": "ProductVariant",
45+
"id": "gid://shopify/ProductVariant/0"
46+
}
47+
},
48+
{
49+
"quantity": 1,
50+
"merchandise": {
51+
"__typename": "ProductVariant",
52+
"id": "gid://shopify/ProductVariant/1"
5453
}
55-
]
56-
},
57-
"discountNode": {
58-
"metafield": {
59-
"value": "{\"quantity\":5,\"percentage\":10.0}"
6054
}
55+
]
56+
},
57+
"discountNode": {
58+
"metafield": {
59+
"value": "{\"quantity\":5,\"percentage\":10.0}"
6160
}
6261
}
62+
}
6363
"#,
6464
)?;
65-
let expected = output::Target {
66-
product_variant: Some(output::ProductVariantTarget {
67-
id: "gid://shopify/ProductVariant/0".to_string(),
68-
quantity: None
69-
})
65+
let expected = output::FunctionResult {
66+
discount_application_strategy: output::DiscountApplicationStrategy::FIRST,
67+
discounts: vec![
68+
output::Discount {
69+
message: None,
70+
value: output::Value {
71+
fixed_amount: None,
72+
percentage: Some(output::Percentage {
73+
value: "10".to_string()
74+
})
75+
},
76+
targets: vec![
77+
output::Target {
78+
product_variant: Some(output::ProductVariantTarget {
79+
id: "gid://shopify/ProductVariant/0".to_string(),
80+
quantity: None
81+
})
82+
}
83+
]
84+
}
85+
]
7086
};
7187

72-
assert_eq!(result.discounts.first().unwrap().targets.first().unwrap(), &expected);
88+
assert_eq!(result, expected);
7389
Ok(())
7490
}

0 commit comments

Comments
 (0)