render-with-context.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Disable prop type checks for test harnesses
  2. /* eslint-disable react/prop-types */
  3. import React from 'react'
  4. import { render } from '@testing-library/react'
  5. import sinon from 'sinon'
  6. import { ApplicationProvider } from '../../../frontend/js/shared/context/application-context'
  7. import { EditorProvider } from '../../../frontend/js/shared/context/editor-context'
  8. import { LayoutProvider } from '../../../frontend/js/shared/context/layout-context'
  9. import { ChatProvider } from '../../../frontend/js/features/chat/context/chat-context'
  10. export function EditorProviders({
  11. user = { id: '123abd' },
  12. projectId = 'project123',
  13. socket = {
  14. on: sinon.stub(),
  15. removeListener: sinon.stub(),
  16. },
  17. children,
  18. }) {
  19. window.user = user || window.user
  20. window.ExposedSettings.appName = 'test'
  21. window.gitBridgePublicBaseUrl = 'git.overleaf.test'
  22. window.project_id = projectId != null ? projectId : window.project_id
  23. window._ide = {
  24. $scope: {
  25. project: {
  26. owner: {
  27. _id: '124abd',
  28. },
  29. },
  30. ui: {
  31. chatOpen: true,
  32. pdfLayout: 'flat',
  33. },
  34. $watch: () => {},
  35. },
  36. socket,
  37. }
  38. return (
  39. <ApplicationProvider>
  40. <EditorProvider ide={window._ide} settings={{}}>
  41. <LayoutProvider $scope={window._ide.$scope}>{children}</LayoutProvider>
  42. </EditorProvider>
  43. </ApplicationProvider>
  44. )
  45. }
  46. export function renderWithEditorContext(children, props) {
  47. return render(<EditorProviders {...props}>{children}</EditorProviders>)
  48. }
  49. export function ChatProviders({ children, ...props }) {
  50. return (
  51. <EditorProviders {...props}>
  52. <ChatProvider>{children}</ChatProvider>
  53. </EditorProviders>
  54. )
  55. }
  56. export function renderWithChatContext(children, props) {
  57. return render(<ChatProviders {...props}>{children}</ChatProviders>)
  58. }
  59. export function cleanUpContext() {
  60. delete window.user
  61. delete window.project_id
  62. delete window._ide
  63. }