promotion.e2e-spec.ts 6.8 KB

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