render-with-context.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Disable prop type checks for test harnesses
  2. /* eslint-disable react/prop-types */
  3. import { render } from '@testing-library/react'
  4. import { renderHook } from '@testing-library/react-hooks'
  5. import { ChatProvider } from '../../../frontend/js/features/chat/context/chat-context'
  6. import { EditorProviders } from './editor-providers'
  7. export function renderWithEditorContext(
  8. component,
  9. contextProps,
  10. renderOptions = {}
  11. ) {
  12. const EditorProvidersWrapper = ({ children }) => (
  13. <EditorProviders {...contextProps}>{children}</EditorProviders>
  14. )
  15. return render(component, {
  16. wrapper: EditorProvidersWrapper,
  17. ...renderOptions,
  18. })
  19. }
  20. export function renderHookWithEditorContext(hook, contextProps) {
  21. const EditorProvidersWrapper = ({ children }) => (
  22. <EditorProviders {...contextProps}>{children}</EditorProviders>
  23. )
  24. return renderHook(hook, { wrapper: EditorProvidersWrapper })
  25. }
  26. export function ChatProviders({ children, ...props }) {
  27. return (
  28. <EditorProviders {...props}>
  29. <ChatProvider>{children}</ChatProvider>
  30. </EditorProviders>
  31. )
  32. }
  33. export function renderWithChatContext(component, props) {
  34. const ChatProvidersWrapper = ({ children }) => (
  35. <ChatProviders {...props}>{children}</ChatProviders>
  36. )
  37. return render(component, { wrapper: ChatProvidersWrapper })
  38. }
  39. export function cleanUpContext() {
  40. delete window.user
  41. delete window.project_id
  42. delete window._ide
  43. }