forked from vendure-ecommerce/vendure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
promotion.e2e-spec.ts
281 lines (246 loc) · 8.74 KB
/
promotion.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { pick } from '@vendure/common/lib/pick';
import gql from 'graphql-tag';
import path from 'path';
import { LanguageCode } from '../../common/lib/generated-types';
import { PromotionAction, PromotionOrderAction } from '../src/config/promotion/promotion-action';
import { PromotionCondition } from '../src/config/promotion/promotion-condition';
import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
import { PROMOTION_FRAGMENT } from './graphql/fragments';
import {
CreatePromotion,
DeletePromotion,
DeletionResult,
GetAdjustmentOperations,
GetPromotion,
GetPromotionList,
Promotion,
UpdatePromotion,
} from './graphql/generated-e2e-admin-types';
import { TestAdminClient } from './test-client';
import { TestServer } from './test-server';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
// tslint:disable:no-non-null-assertion
describe('Promotion resolver', () => {
const client = new TestAdminClient();
const server = new TestServer();
const promoCondition = generateTestCondition('promo_condition');
const promoCondition2 = generateTestCondition('promo_condition2');
const promoAction = generateTestAction('promo_action');
const snapshotProps = ['name', 'actions', 'conditions', 'enabled'] as Array<
'name' | 'actions' | 'conditions' | 'enabled'
>;
let promotion: Promotion.Fragment;
beforeAll(async () => {
await server.init(
{
productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
customerCount: 1,
},
{
promotionOptions: {
promotionConditions: [promoCondition, promoCondition2],
promotionActions: [promoAction],
},
},
);
await client.init();
}, TEST_SETUP_TIMEOUT_MS);
afterAll(async () => {
await server.destroy();
});
it('createPromotion', async () => {
const result = await client.query<CreatePromotion.Mutation, CreatePromotion.Variables>(
CREATE_PROMOTION,
{
input: {
name: 'test promotion',
enabled: true,
conditions: [
{
code: promoCondition.code,
arguments: [{ name: 'arg', value: '500', type: 'int' }],
},
],
actions: [
{
code: promoAction.code,
arguments: [
{
name: 'facetValueIds',
value: '["T_1"]',
type: 'facetValueIds',
},
],
},
],
},
},
);
promotion = result.createPromotion;
expect(pick(promotion, snapshotProps)).toMatchSnapshot();
});
it('updatePromotion', async () => {
const result = await client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(
UPDATE_PROMOTION,
{
input: {
id: promotion.id,
conditions: [
{
code: promoCondition.code,
arguments: [{ name: 'arg', value: '90', type: 'int' }],
},
{
code: promoCondition2.code,
arguments: [{ name: 'arg', value: '10', type: 'int' }],
},
],
},
},
);
expect(pick(result.updatePromotion, snapshotProps)).toMatchSnapshot();
});
it('promotion', async () => {
const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
id: promotion.id,
});
expect(result.promotion!.name).toBe(promotion.name);
});
it('promotions', async () => {
const result = await client.query<GetPromotionList.Query, GetPromotionList.Variables>(
GET_PROMOTION_LIST,
{},
);
expect(result.promotions.totalItems).toBe(1);
expect(result.promotions.items[0].name).toBe('test promotion');
});
it('adjustmentOperations', async () => {
const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
GET_ADJUSTMENT_OPERATIONS,
);
expect(result.promotionActions).toMatchSnapshot();
expect(result.promotionConditions).toMatchSnapshot();
});
describe('deletion', () => {
let allPromotions: GetPromotionList.Items[];
let promotionToDelete: GetPromotionList.Items;
beforeAll(async () => {
const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
allPromotions = result.promotions.items;
});
it('deletes a promotion', async () => {
promotionToDelete = allPromotions[0];
const result = await client.query<DeletePromotion.Mutation, DeletePromotion.Variables>(
DELETE_PROMOTION,
{ id: promotionToDelete.id },
);
expect(result.deletePromotion).toEqual({ result: DeletionResult.DELETED });
});
it('cannot get a deleted promotion', async () => {
const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
id: promotionToDelete.id,
});
expect(result.promotion).toBe(null);
});
it('deleted promotion omitted from list', async () => {
const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
expect(result.promotions.items.length).toBe(allPromotions.length - 1);
expect(result.promotions.items.map(c => c.id).includes(promotionToDelete.id)).toBe(false);
});
it(
'updatePromotion throws for deleted promotion',
assertThrowsWithMessage(
() =>
client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(UPDATE_PROMOTION, {
input: {
id: promotionToDelete.id,
enabled: false,
},
}),
`No Promotion with the id '1' could be found`,
),
);
});
});
function generateTestCondition(code: string): PromotionCondition<any> {
return new PromotionCondition({
code,
description: [{ languageCode: LanguageCode.en, value: `description for ${code}` }],
args: { arg: { type: 'int' } },
check: (order, args) => true,
});
}
function generateTestAction(code: string): PromotionAction<any> {
return new PromotionOrderAction({
code,
description: [{ languageCode: LanguageCode.en, value: `description for ${code}` }],
args: { facetValueIds: { type: 'facetValueIds' } },
execute: (order, args) => {
return 42;
},
});
}
const DELETE_PROMOTION = gql`
mutation DeletePromotion($id: ID!) {
deletePromotion(id: $id) {
result
}
}
`;
export const GET_PROMOTION_LIST = gql`
query GetPromotionList($options: PromotionListOptions) {
promotions(options: $options) {
items {
...Promotion
}
totalItems
}
}
${PROMOTION_FRAGMENT}
`;
export const GET_PROMOTION = gql`
query GetPromotion($id: ID!) {
promotion(id: $id) {
...Promotion
}
}
${PROMOTION_FRAGMENT}
`;
export const CREATE_PROMOTION = gql`
mutation CreatePromotion($input: CreatePromotionInput!) {
createPromotion(input: $input) {
...Promotion
}
}
${PROMOTION_FRAGMENT}
`;
export const UPDATE_PROMOTION = gql`
mutation UpdatePromotion($input: UpdatePromotionInput!) {
updatePromotion(input: $input) {
...Promotion
}
}
${PROMOTION_FRAGMENT}
`;
export const CONFIGURABLE_DEF_FRAGMENT = gql`
fragment ConfigurableOperationDef on ConfigurableOperationDefinition {
args {
name
type
config
}
code
description
}
`;
export const GET_ADJUSTMENT_OPERATIONS = gql`
query GetAdjustmentOperations {
promotionActions {
...ConfigurableOperationDef
}
promotionConditions {
...ConfigurableOperationDef
}
}
${CONFIGURABLE_DEF_FRAGMENT}
`;