customer-group.e2e-spec.ts 11 KB

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