All files / src/tests test-utils.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 6/6
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                            65x 65x       29x 29x         29x   60x               6x 10x                                 10x            
import TestRenderer from 'react-test-renderer'
import React from 'react'
import { BrowserRouter } from 'react-router-dom'
import { render, type RenderOptions } from '@testing-library/react'
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'
 
/**
 * Monte un hook et expose son résultat.
 */
export function renderHook<T>(hook: () => T) {
  let result!: T
 
  function HookWrapper() {
    result = hook()
    return null
  }
 
  // on remplace <HookWrapper /> par React.createElement
  TestRenderer.act(() => {
    TestRenderer.create(
      React.createElement(HookWrapper, null)
    )
  })
 
  return {
    /** getter pour accéder au résultat du hook */
    result: () => result,
  }
}
 
/**
 * A "wrapper" component that instantiates BrowserRouter + LocalizationProvider,
 * but written without any JSX so you never hit that erasableSyntaxOnly error.
 */
const AllProviders: React.FC<{ children?: React.ReactNode }> = ({ children }) =>
  React.createElement(
    BrowserRouter,
    null,
    React.createElement(
      LocalizationProvider,
      { dateAdapter: AdapterDayjs },
      children
    )
  )
 
/**
 * A custom render function for RTL that uses our AllProviders wrapper.
 */
function renderWithProviders(
  ui: React.ReactElement,
  options?: Omit<RenderOptions, 'wrapper'>
) {
  return render(ui, { wrapper: AllProviders, ...options })
}
 
// re-export everything from RTL so you can import both
export * from '@testing-library/react'
export { renderWithProviders }