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

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