customer-group.e2e-spec.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { pick } from '@vendure/common/lib/pick';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import path from 'path';
  4. import { initialData } from '../../../e2e-common/e2e-initial-data';
  5. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  6. import * as Codegen from './graphql/generated-e2e-admin-types';
  7. import { HistoryEntryType } from './graphql/generated-e2e-admin-types';
  8. import { DeletionResult } from './graphql/generated-e2e-shop-types';
  9. import {
  10. ADD_CUSTOMERS_TO_GROUP,
  11. CREATE_CUSTOMER_GROUP,
  12. DELETE_CUSTOMER_GROUP,
  13. GET_CUSTOMER_GROUP,
  14. GET_CUSTOMER_GROUPS,
  15. GET_CUSTOMER_HISTORY,
  16. GET_CUSTOMER_LIST,
  17. GET_CUSTOMER_WITH_GROUPS,
  18. REMOVE_CUSTOMERS_FROM_GROUP,
  19. UPDATE_CUSTOMER_GROUP,
  20. } from './graphql/shared-definitions';
  21. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  22. import { sortById } from './utils/test-order-utils';
  23. describe('CustomerGroup resolver', () => {
  24. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  25. let customers: Codegen.GetCustomerListQuery['customers']['items'];
  26. beforeAll(async () => {
  27. await server.init({
  28. initialData,
  29. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  30. customerCount: 5,
  31. });
  32. await adminClient.asSuperAdmin();
  33. const result = await adminClient.query<Codegen.GetCustomerListQuery>(GET_CUSTOMER_LIST);
  34. customers = result.customers.items;
  35. }, TEST_SETUP_TIMEOUT_MS);
  36. afterAll(async () => {
  37. await server.destroy();
  38. });
  39. it('create', async () => {
  40. const { createCustomerGroup } = await adminClient.query<
  41. Codegen.CreateCustomerGroupMutation,
  42. Codegen.CreateCustomerGroupMutationVariables
  43. >(CREATE_CUSTOMER_GROUP, {
  44. input: {
  45. name: 'group 1',
  46. customerIds: [customers[0].id, customers[1].id],
  47. },
  48. });
  49. expect(createCustomerGroup.name).toBe('group 1');
  50. expect(createCustomerGroup.customers.items.sort(sortById)).toEqual([
  51. { id: customers[0].id },
  52. { id: customers[1].id },
  53. ]);
  54. });
  55. it('history entry for CUSTOMER_ADDED_TO_GROUP after group created', async () => {
  56. const { customer } = await adminClient.query<
  57. Codegen.GetCustomerHistoryQuery,
  58. Codegen.GetCustomerHistoryQueryVariables
  59. >(GET_CUSTOMER_HISTORY, {
  60. id: customers[0].id,
  61. options: {
  62. skip: 3,
  63. },
  64. });
  65. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  66. {
  67. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  68. data: {
  69. groupName: 'group 1',
  70. },
  71. },
  72. ]);
  73. });
  74. it('customerGroups', async () => {
  75. const { customerGroups } = await adminClient.query<
  76. Codegen.GetCustomerGroupsQuery,
  77. Codegen.GetCustomerGroupsQueryVariables
  78. >(GET_CUSTOMER_GROUPS, {
  79. options: {},
  80. });
  81. expect(customerGroups.totalItems).toBe(1);
  82. expect(customerGroups.items[0].name).toBe('group 1');
  83. });
  84. it('customerGroup with customer list options', async () => {
  85. const { customerGroup } = await adminClient.query<
  86. Codegen.GetCustomerGroupQuery,
  87. Codegen.GetCustomerGroupQueryVariables
  88. >(GET_CUSTOMER_GROUP, {
  89. id: 'T_1',
  90. options: {
  91. take: 1,
  92. },
  93. });
  94. expect(customerGroup?.id).toBe('T_1');
  95. expect(customerGroup?.name).toBe('group 1');
  96. expect(customerGroup?.customers.items.length).toBe(1);
  97. expect(customerGroup?.customers.totalItems).toBe(2);
  98. });
  99. it('update', async () => {
  100. const { updateCustomerGroup } = await adminClient.query<
  101. Codegen.UpdateCustomerGroupMutation,
  102. Codegen.UpdateCustomerGroupMutationVariables
  103. >(UPDATE_CUSTOMER_GROUP, {
  104. input: {
  105. id: 'T_1',
  106. name: 'group 1 updated',
  107. },
  108. });
  109. expect(updateCustomerGroup.name).toBe('group 1 updated');
  110. });
  111. it('addCustomersToGroup with existing customer', async () => {
  112. const { addCustomersToGroup } = await adminClient.query<
  113. Codegen.AddCustomersToGroupMutation,
  114. Codegen.AddCustomersToGroupMutationVariables
  115. >(ADD_CUSTOMERS_TO_GROUP, {
  116. groupId: 'T_1',
  117. customerIds: [customers[0].id],
  118. });
  119. expect(addCustomersToGroup.customers.items.sort(sortById)).toEqual([
  120. { id: customers[0].id },
  121. { id: customers[1].id },
  122. ]);
  123. });
  124. it('addCustomersToGroup with new customers', async () => {
  125. const { addCustomersToGroup } = await adminClient.query<
  126. Codegen.AddCustomersToGroupMutation,
  127. Codegen.AddCustomersToGroupMutationVariables
  128. >(ADD_CUSTOMERS_TO_GROUP, {
  129. groupId: 'T_1',
  130. customerIds: [customers[2].id, customers[3].id],
  131. });
  132. expect(addCustomersToGroup.customers.items.sort(sortById)).toEqual([
  133. { id: customers[0].id },
  134. { id: customers[1].id },
  135. { id: customers[2].id },
  136. { id: customers[3].id },
  137. ]);
  138. });
  139. it('history entry for CUSTOMER_ADDED_TO_GROUP not duplicated', async () => {
  140. const { customer } = await adminClient.query<
  141. Codegen.GetCustomerHistoryQuery,
  142. Codegen.GetCustomerHistoryQueryVariables
  143. >(GET_CUSTOMER_HISTORY, {
  144. id: customers[0].id,
  145. options: {
  146. filter: {
  147. type: { eq: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP },
  148. },
  149. },
  150. });
  151. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  152. {
  153. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  154. data: {
  155. groupName: 'group 1',
  156. },
  157. },
  158. ]);
  159. });
  160. it('history entry for CUSTOMER_ADDED_TO_GROUP after customer added', async () => {
  161. const { customer } = await adminClient.query<
  162. Codegen.GetCustomerHistoryQuery,
  163. Codegen.GetCustomerHistoryQueryVariables
  164. >(GET_CUSTOMER_HISTORY, {
  165. id: customers[2].id,
  166. options: {
  167. skip: 3,
  168. },
  169. });
  170. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  171. {
  172. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  173. data: {
  174. groupName: 'group 1 updated',
  175. },
  176. },
  177. ]);
  178. });
  179. it('customer.groups field resolver', async () => {
  180. const { customer } = await adminClient.query<
  181. Codegen.GetCustomerWithGroupsQuery,
  182. Codegen.GetCustomerWithGroupsQueryVariables
  183. >(GET_CUSTOMER_WITH_GROUPS, {
  184. id: customers[0].id,
  185. });
  186. expect(customer?.groups).toEqual([{ id: 'T_1', name: 'group 1 updated' }]);
  187. });
  188. it(
  189. 'removeCustomersFromGroup with invalid customerId',
  190. assertThrowsWithMessage(async () => {
  191. await adminClient.query<
  192. Codegen.RemoveCustomersFromGroupMutation,
  193. Codegen.RemoveCustomersFromGroupMutationVariables
  194. >(REMOVE_CUSTOMERS_FROM_GROUP, {
  195. groupId: 'T_1',
  196. customerIds: [customers[4].id],
  197. });
  198. }, `Customer does not belong to this CustomerGroup`),
  199. );
  200. it('removeCustomersFromGroup with valid customerIds', async () => {
  201. const { removeCustomersFromGroup } = await adminClient.query<
  202. Codegen.RemoveCustomersFromGroupMutation,
  203. Codegen.RemoveCustomersFromGroupMutationVariables
  204. >(REMOVE_CUSTOMERS_FROM_GROUP, {
  205. groupId: 'T_1',
  206. customerIds: [customers[1].id, customers[3].id],
  207. });
  208. expect(removeCustomersFromGroup.customers.items.sort(sortById)).toEqual([
  209. { id: customers[0].id },
  210. { id: customers[2].id },
  211. ]);
  212. });
  213. it('history entry for CUSTOMER_REMOVED_FROM_GROUP', async () => {
  214. const { customer } = await adminClient.query<
  215. Codegen.GetCustomerHistoryQuery,
  216. Codegen.GetCustomerHistoryQueryVariables
  217. >(GET_CUSTOMER_HISTORY, {
  218. id: customers[1].id,
  219. options: {
  220. skip: 4,
  221. },
  222. });
  223. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  224. {
  225. type: HistoryEntryType.CUSTOMER_REMOVED_FROM_GROUP,
  226. data: {
  227. groupName: 'group 1 updated',
  228. },
  229. },
  230. ]);
  231. });
  232. it('deleteCustomerGroup', async () => {
  233. const { deleteCustomerGroup } = await adminClient.query<
  234. Codegen.DeleteCustomerGroupMutation,
  235. Codegen.DeleteCustomerGroupMutationVariables
  236. >(DELETE_CUSTOMER_GROUP, {
  237. id: 'T_1',
  238. });
  239. expect(deleteCustomerGroup.message).toBeNull();
  240. expect(deleteCustomerGroup.result).toBe(DeletionResult.DELETED);
  241. const { customerGroups } = await adminClient.query<Codegen.GetCustomerGroupsQuery>(
  242. GET_CUSTOMER_GROUPS,
  243. );
  244. expect(customerGroups.totalItems).toBe(0);
  245. });
  246. });