test-order-utils.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { ID } from '@vendure/common/lib/shared-types';
  3. import { PaymentMethodHandler } from '@vendure/core';
  4. import { SimpleGraphQLClient } from '@vendure/testing';
  5. import { ResultOf } from '../graphql/graphql-shop';
  6. import {
  7. addPaymentDocument,
  8. getEligibleShippingMethodsDocument,
  9. setShippingAddressDocument,
  10. setShippingMethodDocument,
  11. transitionToStateDocument,
  12. } from '../graphql/shop-definitions';
  13. export async function proceedToArrangingPayment(
  14. shopClient: SimpleGraphQLClient,
  15. shippingMethodIdx = 1,
  16. ): Promise<ID> {
  17. await shopClient.query(setShippingAddressDocument, {
  18. input: {
  19. fullName: 'name',
  20. streetLine1: '12 the street',
  21. city: 'foo',
  22. postalCode: '123456',
  23. countryCode: 'US',
  24. },
  25. });
  26. const { eligibleShippingMethods } = await shopClient.query(getEligibleShippingMethodsDocument);
  27. await shopClient.query(setShippingMethodDocument, {
  28. id: [eligibleShippingMethods[shippingMethodIdx].id],
  29. });
  30. const { transitionOrderToState } = await shopClient.query(transitionToStateDocument, {
  31. state: 'ArrangingPayment',
  32. });
  33. return (transitionOrderToState as Extract<typeof transitionOrderToState, { id: string }>).id;
  34. }
  35. export async function addPaymentToOrder(
  36. shopClient: SimpleGraphQLClient,
  37. handler: PaymentMethodHandler,
  38. ): Promise<
  39. Extract<NonNullable<ResultOf<typeof addPaymentDocument>['addPaymentToOrder']>, { __typename?: 'Order' }>
  40. > {
  41. const result = await shopClient.query(addPaymentDocument, {
  42. input: {
  43. method: handler.code,
  44. metadata: {
  45. baz: 'quux',
  46. },
  47. },
  48. });
  49. return result.addPaymentToOrder as any;
  50. }
  51. /**
  52. * Sorts an array of entities by the id key. Useful for compensating for the fact that different DBs
  53. * return arrays in different orders.
  54. */
  55. export function sortById<T extends { id: string | number }>(a: T, b: T): 1 | -1 {
  56. return a.id < b.id ? -1 : 1;
  57. }