adjustment-source.e2e-spec.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import {
  2. AdjustmentSource,
  3. AdjustmentType,
  4. CreateAdjustmentSource,
  5. GetAdjustmentOperations,
  6. GetAdjustmentSource,
  7. GetAdjustmentSourceList,
  8. UpdateAdjustmentSource,
  9. } from 'shared/generated-types';
  10. import { pick } from 'shared/pick';
  11. import {
  12. CREATE_ADJUSTMENT_SOURCE,
  13. GET_ADJUSTMENT_OPERATIONS,
  14. GET_ADJUSTMENT_SOURCE,
  15. GET_ADJUSTMENT_SOURCE_LIST,
  16. UPDATE_ADJUSTMENT_SOURCE,
  17. } from '../../admin-ui/src/app/data/definitions/adjustment-source-definitions';
  18. import {
  19. AdjustmentActionDefinition,
  20. AdjustmentConditionDefinition,
  21. } from '../src/config/adjustment/adjustment-types';
  22. import { TestClient } from './test-client';
  23. import { TestServer } from './test-server';
  24. // tslint:disable:no-non-null-assertion
  25. describe('AdjustmentSource resolver', () => {
  26. const client = new TestClient();
  27. const server = new TestServer();
  28. const promoCondition = generateTestCondition('promo_condition', AdjustmentType.PROMOTION);
  29. const promoCondition2 = generateTestCondition('promo_condition2', AdjustmentType.PROMOTION);
  30. const taxCondition = generateTestCondition('tax_condition', AdjustmentType.TAX);
  31. const shippingCondition = generateTestCondition('shipping_condition', AdjustmentType.SHIPPING);
  32. const promoAction = generateTestAction('promo_action', AdjustmentType.PROMOTION);
  33. const taxAction = generateTestAction('tax_action', AdjustmentType.TAX);
  34. const shippingAction = generateTestAction('shipping_action', AdjustmentType.SHIPPING);
  35. const snapshotProps = ['name', 'type', 'actions', 'conditions', 'enabled'] as Array<
  36. 'name' | 'type' | 'actions' | 'conditions' | 'enabled'
  37. >;
  38. let promoAdjustmentSource: AdjustmentSource.Fragment;
  39. beforeAll(async () => {
  40. const token = await server.init(
  41. {
  42. productCount: 1,
  43. customerCount: 1,
  44. },
  45. {
  46. adjustmentConditions: [promoCondition, promoCondition2, taxCondition, shippingCondition],
  47. adjustmentActions: [promoAction, taxAction, shippingAction],
  48. },
  49. );
  50. await client.init();
  51. }, 60000);
  52. afterAll(async () => {
  53. await server.destroy();
  54. });
  55. it('createAdjustmentSource promotion', async () => {
  56. const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
  57. CREATE_ADJUSTMENT_SOURCE,
  58. {
  59. input: {
  60. name: 'promo adjustment source',
  61. type: AdjustmentType.PROMOTION,
  62. enabled: true,
  63. conditions: [
  64. {
  65. code: promoCondition.code,
  66. arguments: ['500'],
  67. },
  68. ],
  69. actions: [
  70. {
  71. code: promoAction.code,
  72. arguments: ['50'],
  73. },
  74. ],
  75. },
  76. },
  77. );
  78. promoAdjustmentSource = result.createAdjustmentSource;
  79. expect(pick(promoAdjustmentSource, snapshotProps)).toMatchSnapshot();
  80. });
  81. it('createAdjustmentSource tax', async () => {
  82. const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
  83. CREATE_ADJUSTMENT_SOURCE,
  84. {
  85. input: {
  86. name: 'tax adjustment source',
  87. type: AdjustmentType.TAX,
  88. enabled: true,
  89. conditions: [
  90. {
  91. code: taxCondition.code,
  92. arguments: ['500'],
  93. },
  94. ],
  95. actions: [
  96. {
  97. code: taxAction.code,
  98. arguments: ['50'],
  99. },
  100. ],
  101. },
  102. },
  103. );
  104. expect(pick(result.createAdjustmentSource, snapshotProps)).toMatchSnapshot();
  105. });
  106. it('createAdjustmentSource shipping', async () => {
  107. const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
  108. CREATE_ADJUSTMENT_SOURCE,
  109. {
  110. input: {
  111. name: 'shipping adjustment source',
  112. type: AdjustmentType.SHIPPING,
  113. enabled: true,
  114. conditions: [
  115. {
  116. code: shippingCondition.code,
  117. arguments: ['500'],
  118. },
  119. ],
  120. actions: [
  121. {
  122. code: shippingAction.code,
  123. arguments: ['50'],
  124. },
  125. ],
  126. },
  127. },
  128. );
  129. expect(pick(result.createAdjustmentSource, snapshotProps)).toMatchSnapshot();
  130. });
  131. it('updateAdjustmentSource', async () => {
  132. const result = await client.query<UpdateAdjustmentSource.Mutation, UpdateAdjustmentSource.Variables>(
  133. UPDATE_ADJUSTMENT_SOURCE,
  134. {
  135. input: {
  136. id: promoAdjustmentSource.id,
  137. conditions: [
  138. {
  139. code: promoCondition.code,
  140. arguments: ['90'],
  141. },
  142. {
  143. code: promoCondition2.code,
  144. arguments: ['10'],
  145. },
  146. ],
  147. },
  148. },
  149. );
  150. expect(pick(result.updateAdjustmentSource, snapshotProps)).toMatchSnapshot();
  151. });
  152. it('adjustmentSource', async () => {
  153. const result = await client.query<GetAdjustmentSource.Query, GetAdjustmentSource.Variables>(
  154. GET_ADJUSTMENT_SOURCE,
  155. {
  156. id: promoAdjustmentSource.id,
  157. },
  158. );
  159. expect(result.adjustmentSource!.name).toBe(promoAdjustmentSource.name);
  160. });
  161. it('adjustmentSources, type = promotion', async () => {
  162. const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
  163. GET_ADJUSTMENT_SOURCE_LIST,
  164. {
  165. type: AdjustmentType.PROMOTION,
  166. },
  167. );
  168. expect(result.adjustmentSources.totalItems).toBe(1);
  169. expect(result.adjustmentSources.items[0].name).toBe('promo adjustment source');
  170. });
  171. it('adjustmentSources, type = tax', async () => {
  172. const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
  173. GET_ADJUSTMENT_SOURCE_LIST,
  174. {
  175. type: AdjustmentType.TAX,
  176. },
  177. );
  178. // 4 = 3 generated by the populate script + 1 created in this test suite.
  179. expect(result.adjustmentSources.totalItems).toBe(4);
  180. expect(result.adjustmentSources.items[3].name).toBe('tax adjustment source');
  181. });
  182. it('adjustmentSources, type = shipping', async () => {
  183. const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
  184. GET_ADJUSTMENT_SOURCE_LIST,
  185. {
  186. type: AdjustmentType.SHIPPING,
  187. },
  188. );
  189. expect(result.adjustmentSources.totalItems).toBe(1);
  190. expect(result.adjustmentSources.items[0].name).toBe('shipping adjustment source');
  191. });
  192. it('adjustmentOperations, type = promotion', async () => {
  193. const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
  194. GET_ADJUSTMENT_OPERATIONS,
  195. {
  196. type: AdjustmentType.PROMOTION,
  197. },
  198. );
  199. expect(result.adjustmentOperations).toMatchSnapshot();
  200. });
  201. it('adjustmentOperations, type = tax', async () => {
  202. const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
  203. GET_ADJUSTMENT_OPERATIONS,
  204. {
  205. type: AdjustmentType.TAX,
  206. },
  207. );
  208. expect(result.adjustmentOperations).toMatchSnapshot();
  209. });
  210. it('adjustmentOperations, type = shipping', async () => {
  211. const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
  212. GET_ADJUSTMENT_OPERATIONS,
  213. {
  214. type: AdjustmentType.SHIPPING,
  215. },
  216. );
  217. expect(result.adjustmentOperations).toMatchSnapshot();
  218. });
  219. });
  220. function generateTestCondition(code: string, type: AdjustmentType): AdjustmentConditionDefinition {
  221. return {
  222. code,
  223. description: `description for ${code}`,
  224. args: [{ name: 'arg', type: 'money' }],
  225. type,
  226. predicate: (order, args) => true,
  227. };
  228. }
  229. function generateTestAction(code: string, type: AdjustmentType): AdjustmentActionDefinition {
  230. return {
  231. code,
  232. description: `description for ${code}`,
  233. args: [{ name: 'percentage', type: 'percentage' }],
  234. type,
  235. calculate: (order, args) => {
  236. return [{ amount: 42 }];
  237. },
  238. };
  239. }