customer-group.e2e-spec.ts 9.3 KB

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