customer-group.e2e-spec.ts 10 KB

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