promotion.e2e-spec.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {
  2. ConfigArgType,
  3. CreatePromotion,
  4. DeletionResult,
  5. GetAdjustmentOperations,
  6. GetPromotion,
  7. GetPromotionList,
  8. Promotion,
  9. UpdatePromotion,
  10. } from '@vendure/common/lib/generated-types';
  11. import { pick } from '@vendure/common/lib/pick';
  12. import gql from 'graphql-tag';
  13. import path from 'path';
  14. import {
  15. CREATE_PROMOTION,
  16. GET_ADJUSTMENT_OPERATIONS,
  17. GET_PROMOTION,
  18. GET_PROMOTION_LIST,
  19. UPDATE_PROMOTION,
  20. } from '../../../admin-ui/src/app/data/definitions/promotion-definitions';
  21. import { PromotionAction, PromotionOrderAction } from '../src/config/promotion/promotion-action';
  22. import { PromotionCondition } from '../src/config/promotion/promotion-condition';
  23. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  24. import { TestAdminClient } from './test-client';
  25. import { TestServer } from './test-server';
  26. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  27. // tslint:disable:no-non-null-assertion
  28. describe('Promotion resolver', () => {
  29. const client = new TestAdminClient();
  30. const server = new TestServer();
  31. const promoCondition = generateTestCondition('promo_condition');
  32. const promoCondition2 = generateTestCondition('promo_condition2');
  33. const promoAction = generateTestAction('promo_action');
  34. const snapshotProps = ['name', 'actions', 'conditions', 'enabled'] as Array<
  35. 'name' | 'actions' | 'conditions' | 'enabled'
  36. >;
  37. let promotion: Promotion.Fragment;
  38. beforeAll(async () => {
  39. await server.init(
  40. {
  41. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  42. customerCount: 1,
  43. },
  44. {
  45. promotionOptions: {
  46. promotionConditions: [promoCondition, promoCondition2],
  47. promotionActions: [promoAction],
  48. },
  49. },
  50. );
  51. await client.init();
  52. }, TEST_SETUP_TIMEOUT_MS);
  53. afterAll(async () => {
  54. await server.destroy();
  55. });
  56. it('createPromotion', async () => {
  57. const result = await client.query<CreatePromotion.Mutation, CreatePromotion.Variables>(
  58. CREATE_PROMOTION,
  59. {
  60. input: {
  61. name: 'test promotion',
  62. enabled: true,
  63. conditions: [
  64. {
  65. code: promoCondition.code,
  66. arguments: [{ name: 'arg', value: '500', type: ConfigArgType.MONEY }],
  67. },
  68. ],
  69. actions: [
  70. {
  71. code: promoAction.code,
  72. arguments: [
  73. {
  74. name: 'facetValueIds',
  75. value: '["T_1"]',
  76. type: ConfigArgType.FACET_VALUE_IDS,
  77. },
  78. ],
  79. },
  80. ],
  81. },
  82. },
  83. );
  84. promotion = result.createPromotion;
  85. expect(pick(promotion, snapshotProps)).toMatchSnapshot();
  86. });
  87. it('updatePromotion', async () => {
  88. const result = await client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(
  89. UPDATE_PROMOTION,
  90. {
  91. input: {
  92. id: promotion.id,
  93. conditions: [
  94. {
  95. code: promoCondition.code,
  96. arguments: [{ name: 'arg', value: '90', type: ConfigArgType.MONEY }],
  97. },
  98. {
  99. code: promoCondition2.code,
  100. arguments: [{ name: 'arg', value: '10', type: ConfigArgType.MONEY }],
  101. },
  102. ],
  103. },
  104. },
  105. );
  106. expect(pick(result.updatePromotion, snapshotProps)).toMatchSnapshot();
  107. });
  108. it('promotion', async () => {
  109. const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
  110. id: promotion.id,
  111. });
  112. expect(result.promotion!.name).toBe(promotion.name);
  113. });
  114. it('promotions', async () => {
  115. const result = await client.query<GetPromotionList.Query, GetPromotionList.Variables>(
  116. GET_PROMOTION_LIST,
  117. {},
  118. );
  119. expect(result.promotions.totalItems).toBe(1);
  120. expect(result.promotions.items[0].name).toBe('test promotion');
  121. });
  122. it('adjustmentOperations', async () => {
  123. const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
  124. GET_ADJUSTMENT_OPERATIONS,
  125. );
  126. expect(result.adjustmentOperations).toMatchSnapshot();
  127. });
  128. describe('deletion', () => {
  129. let allPromotions: GetPromotionList.Items[];
  130. let promotionToDelete: GetPromotionList.Items;
  131. beforeAll(async () => {
  132. const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
  133. allPromotions = result.promotions.items;
  134. });
  135. it('deletes a promotion', async () => {
  136. promotionToDelete = allPromotions[0];
  137. const result = await client.query(DELETE_PROMOTION, { id: promotionToDelete.id });
  138. expect(result.deletePromotion).toEqual({ result: DeletionResult.DELETED });
  139. });
  140. it('cannot get a deleted promotion', async () => {
  141. const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
  142. id: promotionToDelete.id,
  143. });
  144. expect(result.promotion).toBe(null);
  145. });
  146. it('deleted promotion omitted from list', async () => {
  147. const result = await client.query<GetPromotionList.Query>(GET_PROMOTION_LIST);
  148. expect(result.promotions.items.length).toBe(allPromotions.length - 1);
  149. expect(result.promotions.items.map(c => c.id).includes(promotionToDelete.id)).toBe(false);
  150. });
  151. it(
  152. 'updatePromotion throws for deleted promotion',
  153. assertThrowsWithMessage(
  154. () =>
  155. client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(UPDATE_PROMOTION, {
  156. input: {
  157. id: promotionToDelete.id,
  158. enabled: false,
  159. },
  160. }),
  161. `No Promotion with the id '1' could be found`,
  162. ),
  163. );
  164. });
  165. });
  166. function generateTestCondition(code: string): PromotionCondition<any> {
  167. return new PromotionCondition({
  168. code,
  169. description: `description for ${code}`,
  170. args: { arg: ConfigArgType.MONEY },
  171. check: (order, args) => true,
  172. });
  173. }
  174. function generateTestAction(code: string): PromotionAction<any> {
  175. return new PromotionOrderAction({
  176. code,
  177. description: `description for ${code}`,
  178. args: { facetValueIds: ConfigArgType.FACET_VALUE_IDS },
  179. execute: (order, args) => {
  180. return 42;
  181. },
  182. });
  183. }
  184. const DELETE_PROMOTION = gql`
  185. mutation DeletePromotion($id: ID!) {
  186. deletePromotion(id: $id) {
  187. result
  188. }
  189. }
  190. `;