forked from vendure-ecommerce/vendure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock-control.e2e-spec.ts
243 lines (218 loc) · 9.29 KB
/
stock-control.e2e-spec.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* tslint:disable:no-non-null-assertion */
import { mergeConfig, OrderState } from '@vendure/core';
import { createTestEnvironment } from '@vendure/testing';
import gql from 'graphql-tag';
import path from 'path';
import { initialData } from '../../../e2e-common/e2e-initial-data';
import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
import { VARIANT_WITH_STOCK_FRAGMENT } from './graphql/fragments';
import {
CreateAddressInput,
GetStockMovement,
StockMovementType,
UpdateProductVariantInput,
UpdateStock,
VariantWithStockFragment,
} from './graphql/generated-e2e-admin-types';
import {
AddItemToOrder,
AddPaymentToOrder,
PaymentInput,
SetShippingAddress,
TransitionToState,
} from './graphql/generated-e2e-shop-types';
import { GET_STOCK_MOVEMENT } from './graphql/shared-definitions';
import {
ADD_ITEM_TO_ORDER,
ADD_PAYMENT,
SET_SHIPPING_ADDRESS,
TRANSITION_TO_STATE,
} from './graphql/shop-definitions';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
describe('Stock control', () => {
const { server, adminClient, shopClient } = createTestEnvironment(
mergeConfig(testConfig, {
paymentOptions: {
paymentMethodHandlers: [testSuccessfulPaymentMethod],
},
}),
);
beforeAll(async () => {
await server.init({
dataDir: path.join(__dirname, '__data__'),
initialData,
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-stock-control.csv'),
customerCount: 2,
});
await adminClient.asSuperAdmin();
}, TEST_SETUP_TIMEOUT_MS);
afterAll(async () => {
await server.destroy();
});
describe('stock adjustments', () => {
let variants: VariantWithStockFragment[];
it('stockMovements are initially empty', async () => {
const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
GET_STOCK_MOVEMENT,
{ id: 'T_1' },
);
variants = product!.variants;
for (const variant of variants) {
expect(variant.stockMovements.items).toEqual([]);
expect(variant.stockMovements.totalItems).toEqual(0);
}
});
it('updating ProductVariant with same stockOnHand does not create a StockMovement', async () => {
const { updateProductVariants } = await adminClient.query<
UpdateStock.Mutation,
UpdateStock.Variables
>(UPDATE_STOCK_ON_HAND, {
input: [
{
id: variants[0].id,
stockOnHand: variants[0].stockOnHand,
},
] as UpdateProductVariantInput[],
});
expect(updateProductVariants[0]!.stockMovements.items).toEqual([]);
expect(updateProductVariants[0]!.stockMovements.totalItems).toEqual(0);
});
it('increasing stockOnHand creates a StockMovement with correct quantity', async () => {
const { updateProductVariants } = await adminClient.query<
UpdateStock.Mutation,
UpdateStock.Variables
>(UPDATE_STOCK_ON_HAND, {
input: [
{
id: variants[0].id,
stockOnHand: variants[0].stockOnHand + 5,
},
] as UpdateProductVariantInput[],
});
expect(updateProductVariants[0]!.stockOnHand).toBe(5);
expect(updateProductVariants[0]!.stockMovements.totalItems).toEqual(1);
expect(updateProductVariants[0]!.stockMovements.items[0].type).toBe(StockMovementType.ADJUSTMENT);
expect(updateProductVariants[0]!.stockMovements.items[0].quantity).toBe(5);
});
it('decreasing stockOnHand creates a StockMovement with correct quantity', async () => {
const { updateProductVariants } = await adminClient.query<
UpdateStock.Mutation,
UpdateStock.Variables
>(UPDATE_STOCK_ON_HAND, {
input: [
{
id: variants[0].id,
stockOnHand: variants[0].stockOnHand + 5 - 2,
},
] as UpdateProductVariantInput[],
});
expect(updateProductVariants[0]!.stockOnHand).toBe(3);
expect(updateProductVariants[0]!.stockMovements.totalItems).toEqual(2);
expect(updateProductVariants[0]!.stockMovements.items[1].type).toBe(StockMovementType.ADJUSTMENT);
expect(updateProductVariants[0]!.stockMovements.items[1].quantity).toBe(-2);
});
it(
'attempting to set a negative stockOnHand throws',
assertThrowsWithMessage(async () => {
const result = await adminClient.query<UpdateStock.Mutation, UpdateStock.Variables>(
UPDATE_STOCK_ON_HAND,
{
input: [
{
id: variants[0].id,
stockOnHand: -1,
},
] as UpdateProductVariantInput[],
},
);
}, 'stockOnHand cannot be a negative value'),
);
});
describe('sales', () => {
beforeAll(async () => {
const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
GET_STOCK_MOVEMENT,
{ id: 'T_2' },
);
const [variant1, variant2] = product!.variants;
await adminClient.query<UpdateStock.Mutation, UpdateStock.Variables>(UPDATE_STOCK_ON_HAND, {
input: [
{
id: variant1.id,
stockOnHand: 5,
trackInventory: false,
},
{
id: variant2.id,
stockOnHand: 5,
trackInventory: true,
},
] as UpdateProductVariantInput[],
});
// Add items to order and check out
await shopClient.asUserWithCredentials('[email protected]', 'test');
await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
productVariantId: variant1.id,
quantity: 2,
});
await shopClient.query<AddItemToOrder.Mutation, AddItemToOrder.Variables>(ADD_ITEM_TO_ORDER, {
productVariantId: variant2.id,
quantity: 3,
});
await shopClient.query<SetShippingAddress.Mutation, SetShippingAddress.Variables>(
SET_SHIPPING_ADDRESS,
{
input: {
streetLine1: '1 Test Street',
countryCode: 'GB',
} as CreateAddressInput,
},
);
await shopClient.query<TransitionToState.Mutation, TransitionToState.Variables>(
TRANSITION_TO_STATE,
{ state: 'ArrangingPayment' as OrderState },
);
});
it('creates a Sale when order completed', async () => {
const { addPaymentToOrder } = await shopClient.query<
AddPaymentToOrder.Mutation,
AddPaymentToOrder.Variables
>(ADD_PAYMENT, {
input: {
method: testSuccessfulPaymentMethod.code,
metadata: {},
} as PaymentInput,
});
expect(addPaymentToOrder).not.toBeNull();
const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
GET_STOCK_MOVEMENT,
{ id: 'T_2' },
);
const [variant1, variant2] = product!.variants;
expect(variant1.stockMovements.totalItems).toBe(2);
expect(variant1.stockMovements.items[1].type).toBe(StockMovementType.SALE);
expect(variant1.stockMovements.items[1].quantity).toBe(-2);
expect(variant2.stockMovements.totalItems).toBe(2);
expect(variant2.stockMovements.items[1].type).toBe(StockMovementType.SALE);
expect(variant2.stockMovements.items[1].quantity).toBe(-3);
});
it('stockOnHand is updated according to trackInventory setting', async () => {
const { product } = await adminClient.query<GetStockMovement.Query, GetStockMovement.Variables>(
GET_STOCK_MOVEMENT,
{ id: 'T_2' },
);
const [variant1, variant2] = product!.variants;
expect(variant1.stockOnHand).toBe(5); // untracked inventory
expect(variant2.stockOnHand).toBe(2); // tracked inventory
});
});
});
const UPDATE_STOCK_ON_HAND = gql`
mutation UpdateStock($input: [UpdateProductVariantInput!]!) {
updateProductVariants(input: $input) {
...VariantWithStock
}
}
${VARIANT_WITH_STOCK_FRAGMENT}
`;