customer.e2e-spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. import { OnModuleInit } from '@nestjs/common';
  2. import { HistoryEntryType } from '@vendure/common/lib/generated-types';
  3. import { omit } from '@vendure/common/lib/omit';
  4. import { pick } from '@vendure/common/lib/pick';
  5. import {
  6. AccountRegistrationEvent,
  7. EventBus,
  8. EventBusModule,
  9. mergeConfig,
  10. VendurePlugin,
  11. } from '@vendure/core';
  12. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
  13. import gql from 'graphql-tag';
  14. import path from 'path';
  15. import { initialData } from '../../../e2e-common/e2e-initial-data';
  16. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  17. import { CUSTOMER_FRAGMENT } from './graphql/fragments';
  18. import {
  19. AddNoteToCustomer,
  20. CreateAddress,
  21. CreateCustomer,
  22. CustomerFragment,
  23. DeleteCustomer,
  24. DeleteCustomerAddress,
  25. DeleteCustomerNote,
  26. DeletionResult,
  27. ErrorCode,
  28. GetCustomer,
  29. GetCustomerHistory,
  30. GetCustomerList,
  31. GetCustomerOrders,
  32. GetCustomerWithUser,
  33. Me,
  34. UpdateAddress,
  35. UpdateCustomer,
  36. UpdateCustomerNote,
  37. } from './graphql/generated-e2e-admin-types';
  38. import { AddItemToOrder, UpdatedOrderFragment } from './graphql/generated-e2e-shop-types';
  39. import {
  40. CREATE_ADDRESS,
  41. CREATE_CUSTOMER,
  42. DELETE_CUSTOMER,
  43. DELETE_CUSTOMER_NOTE,
  44. GET_CUSTOMER,
  45. GET_CUSTOMER_HISTORY,
  46. GET_CUSTOMER_LIST,
  47. ME,
  48. UPDATE_ADDRESS,
  49. UPDATE_CUSTOMER,
  50. UPDATE_CUSTOMER_NOTE,
  51. } from './graphql/shared-definitions';
  52. import { ADD_ITEM_TO_ORDER } from './graphql/shop-definitions';
  53. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  54. // tslint:disable:no-non-null-assertion
  55. let sendEmailFn: jest.Mock;
  56. /**
  57. * This mock plugin simulates an EmailPlugin which would send emails
  58. * on the registration & password reset events.
  59. */
  60. @VendurePlugin({
  61. imports: [EventBusModule],
  62. })
  63. class TestEmailPlugin implements OnModuleInit {
  64. constructor(private eventBus: EventBus) {}
  65. onModuleInit() {
  66. this.eventBus.ofType(AccountRegistrationEvent).subscribe(event => {
  67. sendEmailFn(event);
  68. });
  69. }
  70. }
  71. describe('Customer resolver', () => {
  72. const { server, adminClient, shopClient } = createTestEnvironment(
  73. mergeConfig(testConfig(), { plugins: [TestEmailPlugin] }),
  74. );
  75. let firstCustomer: GetCustomerList.Items;
  76. let secondCustomer: GetCustomerList.Items;
  77. let thirdCustomer: GetCustomerList.Items;
  78. const customerErrorGuard: ErrorResultGuard<CustomerFragment> = createErrorResultGuard(
  79. input => !!input.emailAddress,
  80. );
  81. beforeAll(async () => {
  82. await server.init({
  83. initialData,
  84. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  85. customerCount: 5,
  86. });
  87. await adminClient.asSuperAdmin();
  88. }, TEST_SETUP_TIMEOUT_MS);
  89. afterAll(async () => {
  90. await server.destroy();
  91. });
  92. it('customers list', async () => {
  93. const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
  94. GET_CUSTOMER_LIST,
  95. );
  96. expect(result.customers.items.length).toBe(5);
  97. expect(result.customers.totalItems).toBe(5);
  98. firstCustomer = result.customers.items[0];
  99. secondCustomer = result.customers.items[1];
  100. thirdCustomer = result.customers.items[2];
  101. });
  102. it('customers list filter by postalCode', async () => {
  103. const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
  104. GET_CUSTOMER_LIST,
  105. {
  106. options: {
  107. filter: {
  108. postalCode: {
  109. eq: 'NU9 0PW',
  110. },
  111. },
  112. },
  113. },
  114. );
  115. expect(result.customers.items.length).toBe(1);
  116. expect(result.customers.items[0].emailAddress).toBe('eliezer56@yahoo.com');
  117. });
  118. it('customer resolver resolves User', async () => {
  119. const { customer } = await adminClient.query<
  120. GetCustomerWithUser.Query,
  121. GetCustomerWithUser.Variables
  122. >(GET_CUSTOMER_WITH_USER, {
  123. id: firstCustomer.id,
  124. });
  125. expect(customer!.user).toEqual({
  126. id: 'T_2',
  127. identifier: firstCustomer.emailAddress,
  128. verified: true,
  129. });
  130. });
  131. describe('addresses', () => {
  132. let firstCustomerAddressIds: string[] = [];
  133. let firstCustomerThirdAddressId: string;
  134. it(
  135. 'createCustomerAddress throws on invalid countryCode',
  136. assertThrowsWithMessage(
  137. () =>
  138. adminClient.query<CreateAddress.Mutation, CreateAddress.Variables>(CREATE_ADDRESS, {
  139. id: firstCustomer.id,
  140. input: {
  141. streetLine1: 'streetLine1',
  142. countryCode: 'INVALID',
  143. },
  144. }),
  145. `The countryCode "INVALID" was not recognized`,
  146. ),
  147. );
  148. it('createCustomerAddress creates a new address', async () => {
  149. const result = await adminClient.query<CreateAddress.Mutation, CreateAddress.Variables>(
  150. CREATE_ADDRESS,
  151. {
  152. id: firstCustomer.id,
  153. input: {
  154. fullName: 'fullName',
  155. company: 'company',
  156. streetLine1: 'streetLine1',
  157. streetLine2: 'streetLine2',
  158. city: 'city',
  159. province: 'province',
  160. postalCode: 'postalCode',
  161. countryCode: 'GB',
  162. phoneNumber: 'phoneNumber',
  163. defaultShippingAddress: false,
  164. defaultBillingAddress: false,
  165. },
  166. },
  167. );
  168. expect(omit(result.createCustomerAddress, ['id'])).toEqual({
  169. fullName: 'fullName',
  170. company: 'company',
  171. streetLine1: 'streetLine1',
  172. streetLine2: 'streetLine2',
  173. city: 'city',
  174. province: 'province',
  175. postalCode: 'postalCode',
  176. country: {
  177. code: 'GB',
  178. name: 'United Kingdom',
  179. },
  180. phoneNumber: 'phoneNumber',
  181. defaultShippingAddress: false,
  182. defaultBillingAddress: false,
  183. });
  184. });
  185. it('customer query returns addresses', async () => {
  186. const result = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  187. id: firstCustomer.id,
  188. });
  189. expect(result.customer!.addresses!.length).toBe(2);
  190. firstCustomerAddressIds = result.customer!.addresses!.map(a => a.id).sort();
  191. });
  192. it('updateCustomerAddress updates the country', async () => {
  193. const result = await adminClient.query<UpdateAddress.Mutation, UpdateAddress.Variables>(
  194. UPDATE_ADDRESS,
  195. {
  196. input: {
  197. id: firstCustomerAddressIds[0],
  198. countryCode: 'AT',
  199. },
  200. },
  201. );
  202. expect(result.updateCustomerAddress.country).toEqual({
  203. code: 'AT',
  204. name: 'Austria',
  205. });
  206. });
  207. it('updateCustomerAddress allows only a single default address', async () => {
  208. // set the first customer's second address to be default
  209. const result1 = await adminClient.query<UpdateAddress.Mutation, UpdateAddress.Variables>(
  210. UPDATE_ADDRESS,
  211. {
  212. input: {
  213. id: firstCustomerAddressIds[1],
  214. defaultShippingAddress: true,
  215. defaultBillingAddress: true,
  216. },
  217. },
  218. );
  219. expect(result1.updateCustomerAddress.defaultShippingAddress).toBe(true);
  220. expect(result1.updateCustomerAddress.defaultBillingAddress).toBe(true);
  221. // assert the first customer's other addresse is not default
  222. const result2 = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  223. id: firstCustomer.id,
  224. });
  225. const otherAddress = result2.customer!.addresses!.filter(
  226. a => a.id !== firstCustomerAddressIds[1],
  227. )[0]!;
  228. expect(otherAddress.defaultShippingAddress).toBe(false);
  229. expect(otherAddress.defaultBillingAddress).toBe(false);
  230. // set the first customer's first address to be default
  231. const result3 = await adminClient.query<UpdateAddress.Mutation, UpdateAddress.Variables>(
  232. UPDATE_ADDRESS,
  233. {
  234. input: {
  235. id: firstCustomerAddressIds[0],
  236. defaultShippingAddress: true,
  237. defaultBillingAddress: true,
  238. },
  239. },
  240. );
  241. expect(result3.updateCustomerAddress.defaultShippingAddress).toBe(true);
  242. expect(result3.updateCustomerAddress.defaultBillingAddress).toBe(true);
  243. // assert the first customer's second address is not default
  244. const result4 = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  245. id: firstCustomer.id,
  246. });
  247. const otherAddress2 = result4.customer!.addresses!.filter(
  248. a => a.id !== firstCustomerAddressIds[0],
  249. )[0]!;
  250. expect(otherAddress2.defaultShippingAddress).toBe(false);
  251. expect(otherAddress2.defaultBillingAddress).toBe(false);
  252. // get the second customer's address id
  253. const result5 = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  254. id: secondCustomer.id,
  255. });
  256. const secondCustomerAddressId = result5.customer!.addresses![0].id;
  257. // set the second customer's address to be default
  258. const result6 = await adminClient.query<UpdateAddress.Mutation, UpdateAddress.Variables>(
  259. UPDATE_ADDRESS,
  260. {
  261. input: {
  262. id: secondCustomerAddressId,
  263. defaultShippingAddress: true,
  264. defaultBillingAddress: true,
  265. },
  266. },
  267. );
  268. expect(result6.updateCustomerAddress.defaultShippingAddress).toBe(true);
  269. expect(result6.updateCustomerAddress.defaultBillingAddress).toBe(true);
  270. // assets the first customer's address defaults are unchanged
  271. const result7 = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  272. id: firstCustomer.id,
  273. });
  274. expect(result7.customer!.addresses![0].defaultShippingAddress).toBe(true);
  275. expect(result7.customer!.addresses![0].defaultBillingAddress).toBe(true);
  276. expect(result7.customer!.addresses![1].defaultShippingAddress).toBe(false);
  277. expect(result7.customer!.addresses![1].defaultBillingAddress).toBe(false);
  278. });
  279. it('createCustomerAddress with true defaults unsets existing defaults', async () => {
  280. const { createCustomerAddress } = await adminClient.query<
  281. CreateAddress.Mutation,
  282. CreateAddress.Variables
  283. >(CREATE_ADDRESS, {
  284. id: firstCustomer.id,
  285. input: {
  286. streetLine1: 'new default streetline',
  287. countryCode: 'GB',
  288. defaultShippingAddress: true,
  289. defaultBillingAddress: true,
  290. },
  291. });
  292. expect(omit(createCustomerAddress, ['id'])).toEqual({
  293. fullName: '',
  294. company: '',
  295. streetLine1: 'new default streetline',
  296. streetLine2: '',
  297. city: '',
  298. province: '',
  299. postalCode: '',
  300. country: {
  301. code: 'GB',
  302. name: 'United Kingdom',
  303. },
  304. phoneNumber: '',
  305. defaultShippingAddress: true,
  306. defaultBillingAddress: true,
  307. });
  308. const { customer } = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(
  309. GET_CUSTOMER,
  310. {
  311. id: firstCustomer.id,
  312. },
  313. );
  314. for (const address of customer!.addresses!) {
  315. const shouldBeDefault = address.id === createCustomerAddress.id;
  316. expect(address.defaultShippingAddress).toEqual(shouldBeDefault);
  317. expect(address.defaultBillingAddress).toEqual(shouldBeDefault);
  318. }
  319. firstCustomerThirdAddressId = createCustomerAddress.id;
  320. });
  321. it('deleteCustomerAddress on default address resets defaults', async () => {
  322. const { deleteCustomerAddress } = await adminClient.query<
  323. DeleteCustomerAddress.Mutation,
  324. DeleteCustomerAddress.Variables
  325. >(
  326. gql`
  327. mutation DeleteCustomerAddress($id: ID!) {
  328. deleteCustomerAddress(id: $id) {
  329. success
  330. }
  331. }
  332. `,
  333. { id: firstCustomerThirdAddressId },
  334. );
  335. expect(deleteCustomerAddress.success).toBe(true);
  336. const { customer } = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(
  337. GET_CUSTOMER,
  338. {
  339. id: firstCustomer.id,
  340. },
  341. );
  342. expect(customer!.addresses!.length).toBe(2);
  343. const defaultAddress = customer!.addresses!.filter(
  344. a => a.defaultBillingAddress && a.defaultShippingAddress,
  345. );
  346. const otherAddress = customer!.addresses!.filter(
  347. a => !a.defaultBillingAddress && !a.defaultShippingAddress,
  348. );
  349. expect(defaultAddress.length).toBe(1);
  350. expect(otherAddress.length).toBe(1);
  351. });
  352. });
  353. describe('orders', () => {
  354. const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard(
  355. input => !!input.lines,
  356. );
  357. it(`lists that user\'s orders`, async () => {
  358. // log in as first customer
  359. await shopClient.asUserWithCredentials(firstCustomer.emailAddress, 'test');
  360. // add an item to the order to create an order
  361. const { addItemToOrder } = await shopClient.query<
  362. AddItemToOrder.Mutation,
  363. AddItemToOrder.Variables
  364. >(ADD_ITEM_TO_ORDER, {
  365. productVariantId: 'T_1',
  366. quantity: 1,
  367. });
  368. orderResultGuard.assertSuccess(addItemToOrder);
  369. const { customer } = await adminClient.query<
  370. GetCustomerOrders.Query,
  371. GetCustomerOrders.Variables
  372. >(GET_CUSTOMER_ORDERS, { id: firstCustomer.id });
  373. expect(customer!.orders.totalItems).toBe(1);
  374. expect(customer!.orders.items[0].id).toBe(addItemToOrder!.id);
  375. });
  376. });
  377. describe('creation', () => {
  378. it('triggers verification event if no password supplied', async () => {
  379. sendEmailFn = jest.fn();
  380. const { createCustomer } = await adminClient.query<
  381. CreateCustomer.Mutation,
  382. CreateCustomer.Variables
  383. >(CREATE_CUSTOMER, {
  384. input: {
  385. emailAddress: 'test1@test.com',
  386. firstName: 'New',
  387. lastName: 'Customer',
  388. },
  389. });
  390. customerErrorGuard.assertSuccess(createCustomer);
  391. expect(createCustomer.user!.verified).toBe(false);
  392. expect(sendEmailFn).toHaveBeenCalledTimes(1);
  393. expect(sendEmailFn.mock.calls[0][0] instanceof AccountRegistrationEvent).toBe(true);
  394. expect(sendEmailFn.mock.calls[0][0].user.identifier).toBe('test1@test.com');
  395. });
  396. it('creates a verified Customer', async () => {
  397. sendEmailFn = jest.fn();
  398. const { createCustomer } = await adminClient.query<
  399. CreateCustomer.Mutation,
  400. CreateCustomer.Variables
  401. >(CREATE_CUSTOMER, {
  402. input: {
  403. emailAddress: 'test2@test.com',
  404. firstName: 'New',
  405. lastName: 'Customer',
  406. },
  407. password: 'test',
  408. });
  409. customerErrorGuard.assertSuccess(createCustomer);
  410. expect(createCustomer.user!.verified).toBe(true);
  411. expect(sendEmailFn).toHaveBeenCalledTimes(0);
  412. });
  413. it('return error result when using an existing, non-deleted emailAddress', async () => {
  414. const { createCustomer } = await adminClient.query<
  415. CreateCustomer.Mutation,
  416. CreateCustomer.Variables
  417. >(CREATE_CUSTOMER, {
  418. input: {
  419. emailAddress: 'test2@test.com',
  420. firstName: 'New',
  421. lastName: 'Customer',
  422. },
  423. password: 'test',
  424. });
  425. customerErrorGuard.assertErrorResult(createCustomer);
  426. expect(createCustomer.message).toBe('The email address is not available.');
  427. expect(createCustomer.errorCode).toBe(ErrorCode.EMAIL_ADDRESS_CONFLICT_ERROR);
  428. });
  429. });
  430. describe('update', () => {
  431. it('returns error result when emailAddress not available', async () => {
  432. const { updateCustomer } = await adminClient.query<
  433. UpdateCustomer.Mutation,
  434. UpdateCustomer.Variables
  435. >(UPDATE_CUSTOMER, {
  436. input: {
  437. id: thirdCustomer.id,
  438. emailAddress: firstCustomer.emailAddress,
  439. },
  440. });
  441. customerErrorGuard.assertErrorResult(updateCustomer);
  442. expect(updateCustomer.message).toBe('The email address is not available.');
  443. expect(updateCustomer.errorCode).toBe(ErrorCode.EMAIL_ADDRESS_CONFLICT_ERROR);
  444. });
  445. it('succeeds when emailAddress is available', async () => {
  446. const { updateCustomer } = await adminClient.query<
  447. UpdateCustomer.Mutation,
  448. UpdateCustomer.Variables
  449. >(UPDATE_CUSTOMER, {
  450. input: {
  451. id: thirdCustomer.id,
  452. emailAddress: 'unique-email@test.com',
  453. },
  454. });
  455. customerErrorGuard.assertSuccess(updateCustomer);
  456. expect(updateCustomer.emailAddress).toBe('unique-email@test.com');
  457. });
  458. // https://github.com/vendure-ecommerce/vendure/issues/1071
  459. it('updates the associated User email address', async () => {
  460. await shopClient.asUserWithCredentials('unique-email@test.com', 'test');
  461. const { me } = await shopClient.query<Me.Query>(ME);
  462. expect(me?.identifier).toBe('unique-email@test.com');
  463. });
  464. });
  465. describe('deletion', () => {
  466. it('deletes a customer', async () => {
  467. const result = await adminClient.query<DeleteCustomer.Mutation, DeleteCustomer.Variables>(
  468. DELETE_CUSTOMER,
  469. { id: thirdCustomer.id },
  470. );
  471. expect(result.deleteCustomer).toEqual({ result: DeletionResult.DELETED });
  472. });
  473. it('cannot get a deleted customer', async () => {
  474. const result = await adminClient.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  475. id: thirdCustomer.id,
  476. });
  477. expect(result.customer).toBe(null);
  478. });
  479. it('deleted customer omitted from list', async () => {
  480. const result = await adminClient.query<GetCustomerList.Query, GetCustomerList.Variables>(
  481. GET_CUSTOMER_LIST,
  482. );
  483. expect(result.customers.items.map(c => c.id).includes(thirdCustomer.id)).toBe(false);
  484. });
  485. it(
  486. 'updateCustomer throws for deleted customer',
  487. assertThrowsWithMessage(
  488. () =>
  489. adminClient.query<UpdateCustomer.Mutation, UpdateCustomer.Variables>(UPDATE_CUSTOMER, {
  490. input: {
  491. id: thirdCustomer.id,
  492. firstName: 'updated',
  493. },
  494. }),
  495. `No Customer with the id '3' could be found`,
  496. ),
  497. );
  498. it(
  499. 'createCustomerAddress throws for deleted customer',
  500. assertThrowsWithMessage(
  501. () =>
  502. adminClient.query<CreateAddress.Mutation, CreateAddress.Variables>(CREATE_ADDRESS, {
  503. id: thirdCustomer.id,
  504. input: {
  505. streetLine1: 'test',
  506. countryCode: 'GB',
  507. },
  508. }),
  509. `No Customer with the id '3' could be found`,
  510. ),
  511. );
  512. it('new Customer can be created with same emailAddress as a deleted Customer', async () => {
  513. const { createCustomer } = await adminClient.query<
  514. CreateCustomer.Mutation,
  515. CreateCustomer.Variables
  516. >(CREATE_CUSTOMER, {
  517. input: {
  518. emailAddress: thirdCustomer.emailAddress,
  519. firstName: 'Reusing Email',
  520. lastName: 'Customer',
  521. },
  522. });
  523. customerErrorGuard.assertSuccess(createCustomer);
  524. expect(createCustomer.emailAddress).toBe(thirdCustomer.emailAddress);
  525. expect(createCustomer.firstName).toBe('Reusing Email');
  526. expect(createCustomer.user?.identifier).toBe(thirdCustomer.emailAddress);
  527. });
  528. });
  529. describe('customer notes', () => {
  530. let noteId: string;
  531. it('addNoteToCustomer', async () => {
  532. const { addNoteToCustomer } = await adminClient.query<
  533. AddNoteToCustomer.Mutation,
  534. AddNoteToCustomer.Variables
  535. >(ADD_NOTE_TO_CUSTOMER, {
  536. input: {
  537. id: firstCustomer.id,
  538. isPublic: false,
  539. note: 'Test note',
  540. },
  541. });
  542. const { customer } = await adminClient.query<
  543. GetCustomerHistory.Query,
  544. GetCustomerHistory.Variables
  545. >(GET_CUSTOMER_HISTORY, {
  546. id: firstCustomer.id,
  547. options: {
  548. filter: {
  549. type: {
  550. eq: HistoryEntryType.CUSTOMER_NOTE,
  551. },
  552. },
  553. },
  554. });
  555. expect(customer?.history.items.map(pick(['type', 'data']))).toEqual([
  556. {
  557. type: HistoryEntryType.CUSTOMER_NOTE,
  558. data: {
  559. note: 'Test note',
  560. },
  561. },
  562. ]);
  563. noteId = customer?.history.items[0].id!;
  564. });
  565. it('update note', async () => {
  566. const { updateCustomerNote } = await adminClient.query<
  567. UpdateCustomerNote.Mutation,
  568. UpdateCustomerNote.Variables
  569. >(UPDATE_CUSTOMER_NOTE, {
  570. input: {
  571. noteId,
  572. note: 'An updated note',
  573. },
  574. });
  575. expect(updateCustomerNote.data).toEqual({
  576. note: 'An updated note',
  577. });
  578. });
  579. it('delete note', async () => {
  580. const { customer: before } = await adminClient.query<
  581. GetCustomerHistory.Query,
  582. GetCustomerHistory.Variables
  583. >(GET_CUSTOMER_HISTORY, { id: firstCustomer.id });
  584. const historyCount = before?.history.totalItems!;
  585. const { deleteCustomerNote } = await adminClient.query<
  586. DeleteCustomerNote.Mutation,
  587. DeleteCustomerNote.Variables
  588. >(DELETE_CUSTOMER_NOTE, {
  589. id: noteId,
  590. });
  591. expect(deleteCustomerNote.result).toBe(DeletionResult.DELETED);
  592. const { customer: after } = await adminClient.query<
  593. GetCustomerHistory.Query,
  594. GetCustomerHistory.Variables
  595. >(GET_CUSTOMER_HISTORY, { id: firstCustomer.id });
  596. expect(after?.history.totalItems).toBe(historyCount - 1);
  597. });
  598. });
  599. });
  600. const GET_CUSTOMER_WITH_USER = gql`
  601. query GetCustomerWithUser($id: ID!) {
  602. customer(id: $id) {
  603. id
  604. user {
  605. id
  606. identifier
  607. verified
  608. }
  609. }
  610. }
  611. `;
  612. const GET_CUSTOMER_ORDERS = gql`
  613. query GetCustomerOrders($id: ID!) {
  614. customer(id: $id) {
  615. orders {
  616. items {
  617. id
  618. }
  619. totalItems
  620. }
  621. }
  622. }
  623. `;
  624. const ADD_NOTE_TO_CUSTOMER = gql`
  625. mutation AddNoteToCustomer($input: AddNoteToCustomerInput!) {
  626. addNoteToCustomer(input: $input) {
  627. ...Customer
  628. }
  629. }
  630. ${CUSTOMER_FRAGMENT}
  631. `;