promotion.e2e-spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {
  2. CreatePromotion,
  3. GetAdjustmentOperations,
  4. GetPromotion,
  5. GetPromotionList,
  6. Promotion,
  7. UpdatePromotion,
  8. } from '../../shared/generated-types';
  9. import { pick } from '../../shared/pick';
  10. import {
  11. CREATE_PROMOTION,
  12. GET_ADJUSTMENT_OPERATIONS,
  13. GET_PROMOTION,
  14. GET_PROMOTION_LIST,
  15. UPDATE_PROMOTION,
  16. } from '../../admin-ui/src/app/data/definitions/promotion-definitions';
  17. import { PromotionAction, PromotionOrderAction } from '../src/config/promotion/promotion-action';
  18. import { PromotionCondition } from '../src/config/promotion/promotion-condition';
  19. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  20. import { TestClient } from './test-client';
  21. import { TestServer } from './test-server';
  22. // tslint:disable:no-non-null-assertion
  23. describe('Promotion resolver', () => {
  24. const client = new TestClient();
  25. const server = new TestServer();
  26. const promoCondition = generateTestCondition('promo_condition');
  27. const promoCondition2 = generateTestCondition('promo_condition2');
  28. const promoAction = generateTestAction('promo_action');
  29. const snapshotProps = ['name', 'actions', 'conditions', 'enabled'] as Array<
  30. 'name' | 'actions' | 'conditions' | 'enabled'
  31. >;
  32. let promotion: Promotion.Fragment;
  33. beforeAll(async () => {
  34. const token = await server.init(
  35. {
  36. productCount: 1,
  37. customerCount: 1,
  38. },
  39. {
  40. promotionOptions: {
  41. promotionConditions: [promoCondition, promoCondition2],
  42. promotionActions: [promoAction],
  43. },
  44. },
  45. );
  46. await client.init();
  47. }, TEST_SETUP_TIMEOUT_MS);
  48. afterAll(async () => {
  49. await server.destroy();
  50. });
  51. it('createPromotion promotion', async () => {
  52. const result = await client.query<CreatePromotion.Mutation, CreatePromotion.Variables>(
  53. CREATE_PROMOTION,
  54. {
  55. input: {
  56. name: 'test promotion',
  57. enabled: true,
  58. conditions: [
  59. {
  60. code: promoCondition.code,
  61. arguments: [{ name: 'arg', value: '500' }],
  62. },
  63. ],
  64. actions: [
  65. {
  66. code: promoAction.code,
  67. arguments: [{ name: 'percentage', value: '50' }],
  68. },
  69. ],
  70. },
  71. },
  72. );
  73. promotion = result.createPromotion;
  74. expect(pick(promotion, snapshotProps)).toMatchSnapshot();
  75. });
  76. it('updatePromotion', async () => {
  77. const result = await client.query<UpdatePromotion.Mutation, UpdatePromotion.Variables>(
  78. UPDATE_PROMOTION,
  79. {
  80. input: {
  81. id: promotion.id,
  82. conditions: [
  83. {
  84. code: promoCondition.code,
  85. arguments: [{ name: 'arg', value: '90' }],
  86. },
  87. {
  88. code: promoCondition2.code,
  89. arguments: [{ name: 'arg', value: '10' }],
  90. },
  91. ],
  92. },
  93. },
  94. );
  95. expect(pick(result.updatePromotion, snapshotProps)).toMatchSnapshot();
  96. });
  97. it('promotion', async () => {
  98. const result = await client.query<GetPromotion.Query, GetPromotion.Variables>(GET_PROMOTION, {
  99. id: promotion.id,
  100. });
  101. expect(result.promotion!.name).toBe(promotion.name);
  102. });
  103. it('promotions', async () => {
  104. const result = await client.query<GetPromotionList.Query, GetPromotionList.Variables>(
  105. GET_PROMOTION_LIST,
  106. {},
  107. );
  108. expect(result.promotions.totalItems).toBe(1);
  109. expect(result.promotions.items[0].name).toBe('test promotion');
  110. });
  111. it('adjustmentOperations', async () => {
  112. const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
  113. GET_ADJUSTMENT_OPERATIONS,
  114. );
  115. expect(result.adjustmentOperations).toMatchSnapshot();
  116. });
  117. });
  118. function generateTestCondition(code: string): PromotionCondition<any> {
  119. return new PromotionCondition({
  120. code,
  121. description: `description for ${code}`,
  122. args: { arg: 'money' },
  123. check: (order, args) => true,
  124. });
  125. }
  126. function generateTestAction(code: string): PromotionAction<any> {
  127. return new PromotionOrderAction({
  128. code,
  129. description: `description for ${code}`,
  130. args: { percentage: 'percentage' },
  131. execute: (order, args) => {
  132. return 42;
  133. },
  134. });
  135. }