test-order-utils.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* tslint:disable: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 * as CodegenShop from '../graphql/generated-e2e-shop-types';
  6. import { TestOrderFragmentFragment } from '../graphql/generated-e2e-shop-types';
  7. import {
  8. ADD_PAYMENT,
  9. GET_ELIGIBLE_SHIPPING_METHODS,
  10. SET_SHIPPING_ADDRESS,
  11. SET_SHIPPING_METHOD,
  12. TRANSITION_TO_STATE,
  13. } from '../graphql/shop-definitions';
  14. export async function proceedToArrangingPayment(shopClient: SimpleGraphQLClient): Promise<ID> {
  15. await shopClient.query<
  16. CodegenShop.SetShippingAddressMutation,
  17. CodegenShop.SetShippingAddressMutationVariables
  18. >(SET_SHIPPING_ADDRESS, {
  19. input: {
  20. fullName: 'name',
  21. streetLine1: '12 the street',
  22. city: 'foo',
  23. postalCode: '123456',
  24. countryCode: 'US',
  25. },
  26. });
  27. const { eligibleShippingMethods } = await shopClient.query<CodegenShop.GetShippingMethodsQuery>(
  28. GET_ELIGIBLE_SHIPPING_METHODS,
  29. );
  30. await shopClient.query<
  31. CodegenShop.SetShippingMethodMutation,
  32. CodegenShop.SetShippingMethodMutationVariables
  33. >(SET_SHIPPING_METHOD, {
  34. id: eligibleShippingMethods[1].id,
  35. });
  36. const { transitionOrderToState } = await shopClient.query<
  37. CodegenShop.TransitionToStateMutation,
  38. CodegenShop.TransitionToStateMutationVariables
  39. >(TRANSITION_TO_STATE, { state: 'ArrangingPayment' });
  40. return (transitionOrderToState as TestOrderFragmentFragment)!.id;
  41. }
  42. export async function addPaymentToOrder(
  43. shopClient: SimpleGraphQLClient,
  44. handler: PaymentMethodHandler,
  45. ): Promise<NonNullable<CodegenShop.AddPaymentToOrderMutation['addPaymentToOrder']>> {
  46. const result = await shopClient.query<
  47. CodegenShop.AddPaymentToOrderMutation,
  48. CodegenShop.AddPaymentToOrderMutationVariables
  49. >(ADD_PAYMENT, {
  50. input: {
  51. method: handler.code,
  52. metadata: {
  53. baz: 'quux',
  54. },
  55. },
  56. });
  57. const order = result.addPaymentToOrder!;
  58. return order as any;
  59. }
  60. /**
  61. * Sorts an array of entities by the id key. Useful for compensating for the fact that different DBs
  62. * return arrays in different orders.
  63. */
  64. export function sortById<T extends { id: string | number }>(a: T, b: T): 1 | -1 {
  65. return a.id < b.id ? -1 : 1;
  66. }