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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. window.metaAttributesCache.set('ol-user', {})
  32. if (!options?.recurlyNotLoaded) {
  33. // @ts-ignore
  34. global.recurly = {
  35. configure: () => {},
  36. Pricing: {
  37. Subscription: () => {
  38. return {
  39. plan: (planCode: string) => {
  40. let plan
  41. const isGroupPlan = planCode.includes('group')
  42. if (isGroupPlan) {
  43. const [, planType, size, usage] = planCode.split('_')
  44. const currencyCode = options?.currencyCode || 'USD'
  45. plan = _.get(groupPriceByUsageTypeAndSize, [
  46. usage,
  47. planType,
  48. currencyCode,
  49. size,
  50. ])
  51. } else {
  52. plan = plans.find(p => p.planCode === planCode)
  53. }
  54. const response = {
  55. next: {
  56. total: plan?.price_in_cents
  57. ? plan.price_in_cents / 100
  58. : undefined,
  59. },
  60. }
  61. return {
  62. currency: () => {
  63. return {
  64. catch: () => {
  65. return {
  66. done: (callback: (response: object) => void) => {
  67. if (!options?.queryingRecurly) {
  68. return callback(response)
  69. }
  70. },
  71. }
  72. },
  73. }
  74. },
  75. }
  76. },
  77. }
  78. },
  79. },
  80. }
  81. }
  82. return render(component, {
  83. wrapper: SubscriptionDashboardProviderWrapper,
  84. })
  85. }
  86. export function cleanUpContext() {
  87. // @ts-ignore
  88. delete global.recurly
  89. fetchMock.reset()
  90. }