customer.e2e-spec.ts 13 KB

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