render-with-subscription-dash-context.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { render } from '@testing-library/react'
  2. import { SubscriptionDashboardProvider } from '../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
  3. import { plans } from '../fixtures/plans'
  4. export function renderWithSubscriptionDashContext(
  5. component: React.ReactElement,
  6. options?: {
  7. metaTags?: { name: string; value: string | object | Array<object> }[]
  8. recurlyNotLoaded?: boolean
  9. queryingRecurly?: boolean
  10. }
  11. ) {
  12. const SubscriptionDashboardProviderWrapper = ({
  13. children,
  14. }: {
  15. children: React.ReactNode
  16. }) => (
  17. <SubscriptionDashboardProvider>{children}</SubscriptionDashboardProvider>
  18. )
  19. window.metaAttributesCache = new Map()
  20. options?.metaTags?.forEach(tag =>
  21. window.metaAttributesCache.set(tag.name, tag.value)
  22. )
  23. if (!options?.recurlyNotLoaded) {
  24. // @ts-ignore
  25. global.recurly = {
  26. configure: () => {},
  27. Pricing: {
  28. Subscription: () => {
  29. return {
  30. plan: (planCode: string) => {
  31. const plan = plans.find(p => p.planCode === planCode)
  32. const response = {
  33. next: {
  34. total: plan?.price_in_cents
  35. ? plan.price_in_cents / 100
  36. : undefined,
  37. },
  38. }
  39. return {
  40. currency: () => {
  41. return {
  42. catch: () => {
  43. return {
  44. done: (callback: (response: object) => void) => {
  45. if (!options?.queryingRecurly) {
  46. return callback(response)
  47. }
  48. },
  49. }
  50. },
  51. }
  52. },
  53. }
  54. },
  55. }
  56. },
  57. },
  58. }
  59. }
  60. return render(component, {
  61. wrapper: SubscriptionDashboardProviderWrapper,
  62. })
  63. }
  64. export function cleanUpContext() {
  65. // @ts-ignore
  66. delete global.recurly
  67. window.metaAttributesCache = new Map()
  68. }