customer-group.e2e-spec.ts 10 KB

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