-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-product-creation.js
More file actions
72 lines (69 loc) · 1.61 KB
/
test-product-creation.js
File metadata and controls
72 lines (69 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Test to verify the complete request transformation
const mockCreateProductInput = {
name: 'Test product',
description: 'joisdfpjasodf asdf',
currency: 'DKK',
categoryId: '4',
variants: [
{
sku: 'TESTPROD-MB',
price: 0,
stock: 0,
attributes: {
Size: 'L',
Color: 'Black'
},
images: [],
isDefault: false
}
]
};
// This is what our transformation in commercify.ts should produce:
const expectedApiRequest = {
name: 'Test product',
description: 'joisdfpjasodf asdf',
currency: 'DKK',
category_id: 4,
images: [],
active: true,
variants: [
{
sku: 'TESTPROD-MB',
price: 0,
stock: 0,
weight: 0,
attributes: [
{
name: 'Size',
value: 'L'
},
{
name: 'Color',
value: 'Black'
}
],
images: [],
is_default: false
}
]
};
// Transform attributes like our createProduct method does
const transformedVariants = mockCreateProductInput.variants.map((variant) => ({
sku: variant.sku,
price: variant.price,
stock: variant.stock,
weight: variant.weight || 0,
attributes: variant.attributes
? Object.entries(variant.attributes).map(([name, value]) => ({ name, value }))
: [],
images: variant.images || [],
is_default: variant.isDefault
}));
console.log('Input variant attributes:', mockCreateProductInput.variants[0].attributes);
console.log('Transformed variant attributes:', transformedVariants[0].attributes);
console.log('Expected format:', expectedApiRequest.variants[0].attributes);
console.log(
'Transformation correct:',
JSON.stringify(transformedVariants[0].attributes) ===
JSON.stringify(expectedApiRequest.variants[0].attributes)
);