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

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