overleaf/services/web/test/frontend/helpers/render-with-context.js
Alf Eaton 7c97f8ab6e Switch to new JSX runtime (#4225)
* Use new JSX runtime and update Babel Node target
* Update .eslintrc
* Remove React imports

GitOrigin-RevId: 559de0267f8f2934c56a860ea8701bb522aa861a
2021-06-24 02:06:59 +00:00

89 lines
2.4 KiB
JavaScript

// Disable prop type checks for test harnesses
/* eslint-disable react/prop-types */
import { render } from '@testing-library/react'
import sinon from 'sinon'
import { ApplicationProvider } from '../../../frontend/js/shared/context/application-context'
import { EditorProvider } from '../../../frontend/js/shared/context/editor-context'
import { LayoutProvider } from '../../../frontend/js/shared/context/layout-context'
import { ChatProvider } from '../../../frontend/js/features/chat/context/chat-context'
import { IdeProvider } from '../../../frontend/js/shared/context/ide-context'
import { get } from 'lodash'
export function EditorProviders({
user = { id: '123abd' },
projectId = 'project123',
socket = {
on: sinon.stub(),
removeListener: sinon.stub(),
},
isRestrictedTokenMember = false,
scope,
children,
}) {
window.user = user || window.user
window.gitBridgePublicBaseUrl = 'git.overleaf.test'
window.project_id = projectId != null ? projectId : window.project_id
window.isRestrictedTokenMember = isRestrictedTokenMember
const $scope = {
project: {
owner: {
_id: '124abd',
},
},
ui: {
chatOpen: true,
pdfLayout: 'flat',
},
$watch: (path, callback) => {
callback(get($scope, path))
return () => null
},
$applyAsync: () => {},
toggleHistory: () => {},
...scope,
}
window._ide = { $scope, socket }
return (
<ApplicationProvider>
<IdeProvider ide={window._ide}>
<EditorProvider settings={{}}>
<LayoutProvider>{children}</LayoutProvider>
</EditorProvider>
</IdeProvider>
</ApplicationProvider>
)
}
export function renderWithEditorContext(component, contextProps) {
const EditorProvidersWrapper = ({ children }) => (
<EditorProviders {...contextProps}>{children}</EditorProviders>
)
return render(component, { wrapper: EditorProvidersWrapper })
}
export function ChatProviders({ children, ...props }) {
return (
<EditorProviders {...props}>
<ChatProvider>{children}</ChatProvider>
</EditorProviders>
)
}
export function renderWithChatContext(component, props) {
const ChatProvidersWrapper = ({ children }) => (
<ChatProviders {...props}>{children}</ChatProviders>
)
return render(component, { wrapper: ChatProvidersWrapper })
}
export function cleanUpContext() {
delete window.user
delete window.project_id
delete window._ide
}