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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { render } from '@testing-library/react'
  2. import _ from 'lodash'
  3. import { SubscriptionDashboardProvider } from '../../../../../frontend/js/features/subscription/context/subscription-dashboard-context'
  4. import { groupPriceByUsageTypeAndSize, plans } from '../fixtures/plans'
  5. import fetchMock from 'fetch-mock'
  6. import { SplitTestProvider } from '@/shared/context/split-test-context'
  7. export function renderWithSubscriptionDashContext(
  8. component: React.ReactElement,
  9. options?: {
  10. metaTags?: {
  11. name: string
  12. value: string | object | Array<object> | boolean
  13. }[]
  14. recurlyNotLoaded?: boolean
  15. queryingRecurly?: boolean
  16. currencyCode?: string
  17. }
  18. ) {
  19. const SubscriptionDashboardProviderWrapper = ({
  20. children,
  21. }: {
  22. children: React.ReactNode
  23. }) => (
  24. <SplitTestProvider>
  25. <SubscriptionDashboardProvider>{children}</SubscriptionDashboardProvider>
  26. </SplitTestProvider>
  27. )
  28. options?.metaTags?.forEach(tag =>
  29. window.metaAttributesCache.set(tag.name, tag.value)
  30. )
  31. if (!options?.recurlyNotLoaded) {
  32. // @ts-ignore
  33. global.recurly = {
  34. configure: () => {},
  35. Pricing: {
  36. Subscription: () => {
  37. return {
  38. plan: (planCode: string) => {
  39. let plan
  40. const isGroupPlan = planCode.includes('group')
  41. if (isGroupPlan) {
  42. const [, planType, size, usage] = planCode.split('_')
  43. const currencyCode = options?.currencyCode || 'USD'
  44. plan = _.get(groupPriceByUsageTypeAndSize, [
  45. usage,
  46. planType,
  47. currencyCode,
  48. size,
  49. ])
  50. } else {
  51. plan = plans.find(p => p.planCode === planCode)
  52. }
  53. const response = {
  54. next: {
  55. total: plan?.price_in_cents
  56. ? plan.price_in_cents / 100
  57. : undefined,
  58. },
  59. }
  60. return {
  61. currency: () => {
  62. return {
  63. catch: () => {
  64. return {
  65. done: (callback: (response: object) => void) => {
  66. if (!options?.queryingRecurly) {
  67. return callback(response)
  68. }
  69. },
  70. }
  71. },
  72. }
  73. },
  74. }
  75. },
  76. }
  77. },
  78. },
  79. }
  80. }
  81. return render(component, {
  82. wrapper: SubscriptionDashboardProviderWrapper,
  83. })
  84. }
  85. export function cleanUpContext() {
  86. // @ts-ignore
  87. delete global.recurly
  88. fetchMock.reset()
  89. }