customer-group.e2e-spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { DeletionResult, HistoryEntryType } from '@vendure/common/lib/generated-types';
  2. import { pick } from '@vendure/common/lib/pick';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import path from 'path';
  5. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { ResultOf } from './graphql/graphql-admin';
  9. import {
  10. addCustomersToGroupDocument,
  11. createCustomerGroupDocument,
  12. deleteCustomerDocument,
  13. deleteCustomerGroupDocument,
  14. getCustomerGroupDocument,
  15. getCustomerGroupsDocument,
  16. getCustomerHistoryDocument,
  17. getCustomerListDocument,
  18. getCustomerWithGroupsDocument,
  19. removeCustomersFromGroupDocument,
  20. updateCustomerGroupDocument,
  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 } = createTestEnvironment(testConfig());
  26. let customers: ResultOf<typeof getCustomerListDocument>['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(getCustomerListDocument);
  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(createCustomerGroupDocument, {
  42. input: {
  43. name: 'group 1',
  44. customerIds: [customers[0].id, customers[1].id],
  45. },
  46. });
  47. expect(createCustomerGroup.name).toBe('group 1');
  48. expect(createCustomerGroup.customers.items.sort(sortById)).toEqual([
  49. { id: customers[0].id },
  50. { id: customers[1].id },
  51. ]);
  52. });
  53. it('history entry for CUSTOMER_ADDED_TO_GROUP after group created', async () => {
  54. const { customer } = await adminClient.query(getCustomerHistoryDocument, {
  55. id: customers[0].id,
  56. options: {
  57. skip: 3,
  58. },
  59. });
  60. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  61. {
  62. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  63. data: {
  64. groupName: 'group 1',
  65. },
  66. },
  67. ]);
  68. });
  69. it('customerGroups', async () => {
  70. const { customerGroups } = await adminClient.query(getCustomerGroupsDocument, {
  71. options: {},
  72. });
  73. expect(customerGroups.totalItems).toBe(1);
  74. expect(customerGroups.items[0].name).toBe('group 1');
  75. });
  76. it('customerGroup with customer list options', async () => {
  77. const { customerGroup } = await adminClient.query(getCustomerGroupDocument, {
  78. id: 'T_1',
  79. options: {
  80. take: 1,
  81. },
  82. });
  83. expect(customerGroup?.id).toBe('T_1');
  84. expect(customerGroup?.name).toBe('group 1');
  85. expect(customerGroup?.customers.items.length).toBe(1);
  86. expect(customerGroup?.customers.totalItems).toBe(2);
  87. });
  88. it('update', async () => {
  89. const { updateCustomerGroup } = await adminClient.query(updateCustomerGroupDocument, {
  90. input: {
  91. id: 'T_1',
  92. name: 'group 1 updated',
  93. },
  94. });
  95. expect(updateCustomerGroup.name).toBe('group 1 updated');
  96. });
  97. it('addCustomersToGroup with existing customer', async () => {
  98. const { addCustomersToGroup } = await adminClient.query(addCustomersToGroupDocument, {
  99. groupId: 'T_1',
  100. customerIds: [customers[0].id],
  101. });
  102. expect(addCustomersToGroup.customers.items.sort(sortById)).toEqual([
  103. { id: customers[0].id },
  104. { id: customers[1].id },
  105. ]);
  106. });
  107. it('addCustomersToGroup with new customers', async () => {
  108. const { addCustomersToGroup } = await adminClient.query(addCustomersToGroupDocument, {
  109. groupId: 'T_1',
  110. customerIds: [customers[2].id, customers[3].id],
  111. });
  112. expect(addCustomersToGroup.customers.items.sort(sortById)).toEqual([
  113. { id: customers[0].id },
  114. { id: customers[1].id },
  115. { id: customers[2].id },
  116. { id: customers[3].id },
  117. ]);
  118. });
  119. it('history entry for CUSTOMER_ADDED_TO_GROUP not duplicated', async () => {
  120. const { customer } = await adminClient.query(getCustomerHistoryDocument, {
  121. id: customers[0].id,
  122. options: {
  123. filter: {
  124. type: { eq: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP },
  125. },
  126. },
  127. });
  128. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  129. {
  130. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  131. data: {
  132. groupName: 'group 1',
  133. },
  134. },
  135. ]);
  136. });
  137. it('history entry for CUSTOMER_ADDED_TO_GROUP after customer added', async () => {
  138. const { customer } = await adminClient.query(getCustomerHistoryDocument, {
  139. id: customers[2].id,
  140. options: {
  141. skip: 3,
  142. },
  143. });
  144. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  145. {
  146. type: HistoryEntryType.CUSTOMER_ADDED_TO_GROUP,
  147. data: {
  148. groupName: 'group 1 updated',
  149. },
  150. },
  151. ]);
  152. });
  153. it('customer.groups field resolver', async () => {
  154. const { customer } = await adminClient.query(getCustomerWithGroupsDocument, {
  155. id: customers[0].id,
  156. });
  157. expect(customer?.groups).toEqual([{ id: 'T_1', name: 'group 1 updated' }]);
  158. });
  159. it(
  160. 'removeCustomersFromGroup with invalid customerId',
  161. assertThrowsWithMessage(async () => {
  162. await adminClient.query(removeCustomersFromGroupDocument, {
  163. groupId: 'T_1',
  164. customerIds: [customers[4].id],
  165. });
  166. }, 'Customer does not belong to this CustomerGroup'),
  167. );
  168. it('removeCustomersFromGroup with valid customerIds', async () => {
  169. const { removeCustomersFromGroup } = await adminClient.query(removeCustomersFromGroupDocument, {
  170. groupId: 'T_1',
  171. customerIds: [customers[1].id, customers[3].id],
  172. });
  173. expect(removeCustomersFromGroup.customers.items.sort(sortById)).toEqual([
  174. { id: customers[0].id },
  175. { id: customers[2].id },
  176. ]);
  177. });
  178. it('history entry for CUSTOMER_REMOVED_FROM_GROUP', async () => {
  179. const { customer } = await adminClient.query(getCustomerHistoryDocument, {
  180. id: customers[1].id,
  181. options: {
  182. skip: 4,
  183. },
  184. });
  185. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  186. {
  187. type: HistoryEntryType.CUSTOMER_REMOVED_FROM_GROUP,
  188. data: {
  189. groupName: 'group 1 updated',
  190. },
  191. },
  192. ]);
  193. });
  194. it('deleteCustomerGroup', async () => {
  195. const { deleteCustomerGroup } = await adminClient.query(deleteCustomerGroupDocument, {
  196. id: 'T_1',
  197. });
  198. expect(deleteCustomerGroup.message).toBeNull();
  199. expect(deleteCustomerGroup.result).toBe(DeletionResult.DELETED);
  200. const { customerGroups } = await adminClient.query(getCustomerGroupsDocument);
  201. expect(customerGroups.totalItems).toBe(0);
  202. });
  203. // https://github.com/vendure-ecommerce/vendure/issues/1785
  204. it('removes customer from group when customer is deleted', async () => {
  205. const customer5Id = customers[4].id;
  206. const { createCustomerGroup } = await adminClient.query(createCustomerGroupDocument, {
  207. input: {
  208. name: 'group-1785',
  209. customerIds: [customer5Id],
  210. },
  211. });
  212. expect(createCustomerGroup.customers.items).toEqual([{ id: customer5Id }]);
  213. await adminClient.query(deleteCustomerDocument, {
  214. id: customer5Id,
  215. });
  216. const { customerGroup } = await adminClient.query(getCustomerGroupDocument, {
  217. id: createCustomerGroup.id,
  218. });
  219. expect(customerGroup?.customers.items).toEqual([]);
  220. });
  221. });