helpers.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
  2. import { fireEvent, getByText, render as originalRender, screen } from '@testing-library/react'
  3. import type React from 'react'
  4. import { useState } from 'react'
  5. import { TooltipProvider } from 'ui'
  6. import { CommandProvider } from 'ui-patterns/CommandMenu'
  7. import { ProjectInfoInfinite } from '@/data/projects/projects-infinite-query'
  8. import type { Organization } from '@/types'
  9. interface SelectorOptions {
  10. container?: HTMLElement
  11. }
  12. /**
  13. * Returns the toggle button given a text matcher
  14. *
  15. * Defaults to screen if container option is not provided
  16. */
  17. export const getToggleByText = (
  18. text: string | RegExp,
  19. options: SelectorOptions = {}
  20. ): HTMLElement | null => {
  21. const container = options?.container
  22. let textNode
  23. if (container) {
  24. textNode = getByText(container as HTMLElement, text)
  25. } else {
  26. textNode = screen.getByText(text)
  27. }
  28. if (textNode && textNode.parentElement) {
  29. return textNode.parentElement.querySelector('button')
  30. } else {
  31. return textNode
  32. }
  33. }
  34. export const clickDropdown = (elem: HTMLElement) => {
  35. fireEvent.pointerDown(
  36. elem,
  37. new window.PointerEvent('pointerdown', {
  38. ctrlKey: false,
  39. button: 0,
  40. })
  41. )
  42. }
  43. export const createMockOrganization = (details: Partial<Organization>): Organization => {
  44. const base: Organization = {
  45. id: 1,
  46. name: 'Organization 1',
  47. slug: 'abcdefghijklmnopqrst',
  48. plan: { id: 'free', name: 'Free' },
  49. managed_by: 'briven',
  50. is_owner: true,
  51. billing_email: 'billing@example.com',
  52. billing_partner: null,
  53. integration_source: null,
  54. usage_billing_enabled: false,
  55. stripe_customer_id: 'stripe-1',
  56. subscription_id: 'subscription-1',
  57. organization_requires_mfa: false,
  58. opt_in_tags: [],
  59. restriction_status: null,
  60. restriction_data: null,
  61. organization_missing_address: false,
  62. organization_missing_tax_id: false,
  63. }
  64. return Object.assign(base, details)
  65. }
  66. export const createMockProject = (details: Partial<ProjectInfoInfinite>): ProjectInfoInfinite => {
  67. const base: ProjectInfoInfinite = {
  68. id: 1,
  69. ref: 'abcdefghijklmnopqrst',
  70. name: 'Project 1',
  71. status: 'ACTIVE_HEALTHY',
  72. organization_id: 1,
  73. cloud_provider: 'AWS',
  74. region: 'us-east-1',
  75. inserted_at: new Date().toISOString(),
  76. subscription_id: 'subscription-1',
  77. is_branch_enabled: false,
  78. is_physical_backups_enabled: false,
  79. organization_slug: 'slug',
  80. preview_branch_refs: [],
  81. }
  82. return Object.assign(base, details)
  83. }
  84. /**
  85. * A custom render function for react testing library
  86. * https://testing-library.com/docs/react-testing-library/setup/#custom-render
  87. */
  88. const ReactQueryTestConfig: React.FC<React.PropsWithChildren> = ({ children }) => {
  89. const [queryClient] = useState(
  90. () =>
  91. new QueryClient({
  92. defaultOptions: {
  93. queries: {
  94. retry: false,
  95. },
  96. },
  97. })
  98. )
  99. return (
  100. <TooltipProvider>
  101. <QueryClientProvider client={queryClient}>
  102. <CommandProvider openKey="">{children}</CommandProvider>
  103. </QueryClientProvider>
  104. </TooltipProvider>
  105. )
  106. }
  107. type renderParams = Parameters<typeof originalRender>
  108. export const render = ((ui: renderParams[0], options: renderParams[1]) =>
  109. originalRender(ui, {
  110. wrapper: ReactQueryTestConfig,
  111. ...options,
  112. })) as typeof originalRender