customer.e2e-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import gql from 'graphql-tag';
  2. import path from 'path';
  3. import {
  4. CREATE_CUSTOMER_ADDRESS,
  5. GET_CUSTOMER,
  6. GET_CUSTOMER_LIST,
  7. UPDATE_CUSTOMER,
  8. UPDATE_CUSTOMER_ADDRESS,
  9. } from '../../admin-ui/src/app/data/definitions/customer-definitions';
  10. import {
  11. CreateCustomerAddress,
  12. DeletionResult,
  13. GetCustomer,
  14. GetCustomerList,
  15. UpdateCustomer,
  16. UpdateCustomerAddress,
  17. } from '../../shared/generated-types';
  18. import { omit } from '../../shared/omit';
  19. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  20. import { TestClient } from './test-client';
  21. import { TestServer } from './test-server';
  22. import { assertThrowsWithMessage } from './test-utils';
  23. // tslint:disable:no-non-null-assertion
  24. describe('Customer resolver', () => {
  25. const client = new TestClient();
  26. const server = new TestServer();
  27. let firstCustomer: GetCustomerList.Items;
  28. let secondCustomer: GetCustomerList.Items;
  29. let thirdCustomer: GetCustomerList.Items;
  30. beforeAll(async () => {
  31. const token = await server.init({
  32. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  33. customerCount: 5,
  34. });
  35. await client.init();
  36. }, TEST_SETUP_TIMEOUT_MS);
  37. afterAll(async () => {
  38. await server.destroy();
  39. });
  40. it('customers list', async () => {
  41. const result = await client.query<GetCustomerList.Query, GetCustomerList.Variables>(
  42. GET_CUSTOMER_LIST,
  43. );
  44. expect(result.customers.items.length).toBe(5);
  45. expect(result.customers.totalItems).toBe(5);
  46. firstCustomer = result.customers.items[0];
  47. secondCustomer = result.customers.items[1];
  48. thirdCustomer = result.customers.items[2];
  49. });
  50. describe('addresses', () => {
  51. let firstCustomerAddressIds: string[] = [];
  52. it(
  53. 'createCustomerAddress throws on invalid countryCode',
  54. assertThrowsWithMessage(
  55. () =>
  56. client.query(CREATE_ADDRESS, {
  57. id: firstCustomer.id,
  58. input: {
  59. streetLine1: 'streetLine1',
  60. countryCode: 'INVALID',
  61. },
  62. }),
  63. `The countryCode "INVALID" was not recognized`,
  64. ),
  65. );
  66. it('createCustomerAddress creates a new address', async () => {
  67. const result = await client.query(CREATE_ADDRESS, {
  68. id: firstCustomer.id,
  69. input: {
  70. fullName: 'fullName',
  71. company: 'company',
  72. streetLine1: 'streetLine1',
  73. streetLine2: 'streetLine2',
  74. city: 'city',
  75. province: 'province',
  76. postalCode: 'postalCode',
  77. countryCode: 'GB',
  78. phoneNumber: 'phoneNumber',
  79. defaultShippingAddress: false,
  80. defaultBillingAddress: false,
  81. },
  82. });
  83. expect(omit(result.createCustomerAddress, ['id'])).toEqual({
  84. fullName: 'fullName',
  85. company: 'company',
  86. streetLine1: 'streetLine1',
  87. streetLine2: 'streetLine2',
  88. city: 'city',
  89. province: 'province',
  90. postalCode: 'postalCode',
  91. country: {
  92. code: 'GB',
  93. name: 'United Kingdom',
  94. },
  95. phoneNumber: 'phoneNumber',
  96. defaultShippingAddress: false,
  97. defaultBillingAddress: false,
  98. });
  99. });
  100. it('customer query returns addresses', async () => {
  101. const result = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  102. id: firstCustomer.id,
  103. });
  104. expect(result.customer!.addresses!.length).toBe(2);
  105. firstCustomerAddressIds = result.customer!.addresses!.map(a => a.id);
  106. });
  107. it('updateCustomerAddress allows only a single default address', async () => {
  108. // set the first customer's second address to be default
  109. const result1 = await client.query(UPDATE_ADDRESS, {
  110. input: {
  111. id: firstCustomerAddressIds[1],
  112. defaultShippingAddress: true,
  113. defaultBillingAddress: true,
  114. },
  115. });
  116. expect(result1.updateCustomerAddress.defaultShippingAddress).toBe(true);
  117. expect(result1.updateCustomerAddress.defaultBillingAddress).toBe(true);
  118. // assert the first customer's first address is not default
  119. const result2 = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  120. id: firstCustomer.id,
  121. });
  122. expect(result2.customer!.addresses![0].defaultShippingAddress).toBe(false);
  123. expect(result2.customer!.addresses![0].defaultBillingAddress).toBe(false);
  124. // set the first customer's first address to be default
  125. const result3 = await client.query(UPDATE_ADDRESS, {
  126. input: {
  127. id: firstCustomerAddressIds[0],
  128. defaultShippingAddress: true,
  129. defaultBillingAddress: true,
  130. },
  131. });
  132. expect(result3.updateCustomerAddress.defaultShippingAddress).toBe(true);
  133. expect(result3.updateCustomerAddress.defaultBillingAddress).toBe(true);
  134. // assert the first customer's second address is not default
  135. const result4 = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  136. id: firstCustomer.id,
  137. });
  138. expect(result4.customer!.addresses![1].defaultShippingAddress).toBe(false);
  139. expect(result4.customer!.addresses![1].defaultBillingAddress).toBe(false);
  140. // get the second customer's address id
  141. const result5 = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  142. id: secondCustomer.id,
  143. });
  144. const secondCustomerAddressId = result5.customer!.addresses![0].id;
  145. // set the second customer's address to be default
  146. const result6 = await client.query(UPDATE_ADDRESS, {
  147. input: {
  148. id: secondCustomerAddressId,
  149. defaultShippingAddress: true,
  150. defaultBillingAddress: true,
  151. },
  152. });
  153. expect(result6.updateCustomerAddress.defaultShippingAddress).toBe(true);
  154. expect(result6.updateCustomerAddress.defaultBillingAddress).toBe(true);
  155. // assets the first customer's address defaults are unchanged
  156. const result7 = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  157. id: firstCustomer.id,
  158. });
  159. expect(result7.customer!.addresses![0].defaultShippingAddress).toBe(true);
  160. expect(result7.customer!.addresses![0].defaultBillingAddress).toBe(true);
  161. expect(result7.customer!.addresses![1].defaultShippingAddress).toBe(false);
  162. expect(result7.customer!.addresses![1].defaultBillingAddress).toBe(false);
  163. });
  164. it('createCustomerAddress with true defaults unsets existing defaults', async () => {
  165. const result1 = await client.query(CREATE_ADDRESS, {
  166. id: firstCustomer.id,
  167. input: {
  168. streetLine1: 'new default streetline',
  169. countryCode: 'GB',
  170. defaultShippingAddress: true,
  171. defaultBillingAddress: true,
  172. },
  173. });
  174. expect(omit(result1.createCustomerAddress, ['id'])).toEqual({
  175. fullName: '',
  176. company: '',
  177. streetLine1: 'new default streetline',
  178. streetLine2: '',
  179. city: '',
  180. province: '',
  181. postalCode: '',
  182. country: {
  183. code: 'GB',
  184. name: 'United Kingdom',
  185. },
  186. phoneNumber: '',
  187. defaultShippingAddress: true,
  188. defaultBillingAddress: true,
  189. });
  190. const result2 = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  191. id: firstCustomer.id,
  192. });
  193. expect(result2.customer!.addresses![0].defaultShippingAddress).toBe(false);
  194. expect(result2.customer!.addresses![0].defaultBillingAddress).toBe(false);
  195. expect(result2.customer!.addresses![1].defaultShippingAddress).toBe(false);
  196. expect(result2.customer!.addresses![1].defaultBillingAddress).toBe(false);
  197. expect(result2.customer!.addresses![2].defaultShippingAddress).toBe(true);
  198. expect(result2.customer!.addresses![2].defaultBillingAddress).toBe(true);
  199. });
  200. });
  201. describe('orders', () => {
  202. it(`lists that user\'s orders`, async () => {
  203. // log in as first customer
  204. await client.asUserWithCredentials(firstCustomer.emailAddress, 'test');
  205. // add an item to the order to create an order
  206. const result1 = await client.query(ADD_ITEM_TO_ORDER, {
  207. productVariantId: 'T_1',
  208. quantity: 1,
  209. });
  210. await client.asSuperAdmin();
  211. const result2 = await client.query(GET_CUSTOMER_ORDERS, { id: firstCustomer.id });
  212. expect(result2.customer.orders.totalItems).toBe(1);
  213. expect(result2.customer.orders.items[0].id).toBe(result1.addItemToOrder.id);
  214. });
  215. });
  216. describe('deletion', () => {
  217. it('deletes a customer', async () => {
  218. const result = await client.query(DELETE_CUSTOMER, { id: thirdCustomer.id });
  219. expect(result.deleteCustomer).toEqual({ result: DeletionResult.DELETED });
  220. });
  221. it('cannot get a deleted customer', async () => {
  222. const result = await client.query<GetCustomer.Query, GetCustomer.Variables>(GET_CUSTOMER, {
  223. id: thirdCustomer.id,
  224. });
  225. expect(result.customer).toBe(null);
  226. });
  227. it('deleted customer omitted from list', async () => {
  228. const result = await client.query<GetCustomerList.Query, GetCustomerList.Variables>(
  229. GET_CUSTOMER_LIST,
  230. );
  231. expect(result.customers.items.map(c => c.id).includes(thirdCustomer.id)).toBe(false);
  232. });
  233. it(
  234. 'updateCustomer throws for deleted customer',
  235. assertThrowsWithMessage(
  236. () =>
  237. client.query<UpdateCustomer.Mutation, UpdateCustomer.Variables>(UPDATE_CUSTOMER, {
  238. input: {
  239. id: thirdCustomer.id,
  240. firstName: 'updated',
  241. },
  242. }),
  243. `No Customer with the id '3' could be found`,
  244. ),
  245. );
  246. it(
  247. 'createCustomerAddress throws for deleted customer',
  248. assertThrowsWithMessage(
  249. () =>
  250. client.query<CreateCustomerAddress.Mutation, CreateCustomerAddress.Variables>(
  251. CREATE_CUSTOMER_ADDRESS,
  252. {
  253. customerId: thirdCustomer.id,
  254. input: {
  255. streetLine1: 'test',
  256. countryCode: 'GB',
  257. },
  258. },
  259. ),
  260. `No Customer with the id '3' could be found`,
  261. ),
  262. );
  263. });
  264. });
  265. const CREATE_ADDRESS = gql`
  266. mutation CreateAddress($id: ID!, $input: CreateAddressInput!) {
  267. createCustomerAddress(customerId: $id, input: $input) {
  268. id
  269. fullName
  270. company
  271. streetLine1
  272. streetLine2
  273. city
  274. province
  275. postalCode
  276. country {
  277. code
  278. name
  279. }
  280. phoneNumber
  281. defaultShippingAddress
  282. defaultBillingAddress
  283. }
  284. }
  285. `;
  286. const UPDATE_ADDRESS = gql`
  287. mutation UpdateAddress($input: UpdateAddressInput!) {
  288. updateCustomerAddress(input: $input) {
  289. id
  290. defaultShippingAddress
  291. defaultBillingAddress
  292. }
  293. }
  294. `;
  295. const ADD_ITEM_TO_ORDER = gql`
  296. mutation AddItemToOrder($productVariantId: ID!, $quantity: Int!) {
  297. addItemToOrder(productVariantId: $productVariantId, quantity: $quantity) {
  298. id
  299. }
  300. }
  301. `;
  302. const GET_CUSTOMER_ORDERS = gql`
  303. query GetCustomerOrders($id: ID!) {
  304. customer(id: $id) {
  305. orders {
  306. items {
  307. id
  308. }
  309. totalItems
  310. }
  311. }
  312. }
  313. `;
  314. const DELETE_CUSTOMER = gql`
  315. mutation DeleteCustomer($id: ID!) {
  316. deleteCustomer(id: $id) {
  317. result
  318. }
  319. }
  320. `;