test-order-utils.ts 2.5 KB

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