customer-group.e2e-spec.ts 11 KB

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