1
0

promotion.e2e-spec.ts 6.9 KB

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