test-order-utils.ts 2.6 KB

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