promotion.e2e-spec.ts 4.6 KB

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