2021-04-23 05:01:16 -04:00
|
|
|
// Disable prop type checks for test harnesses
|
|
|
|
/* eslint-disable react/prop-types */
|
|
|
|
|
2020-12-14 06:44:10 -05:00
|
|
|
import { render } from '@testing-library/react'
|
2021-12-14 08:24:53 -05:00
|
|
|
import { renderHook } from '@testing-library/react-hooks'
|
2021-04-23 05:01:16 -04:00
|
|
|
import sinon from 'sinon'
|
2021-06-25 04:14:07 -04:00
|
|
|
import { UserProvider } from '../../../frontend/js/shared/context/user-context'
|
2020-12-14 06:44:10 -05:00
|
|
|
import { EditorProvider } from '../../../frontend/js/shared/context/editor-context'
|
2021-04-22 05:27:29 -04:00
|
|
|
import { LayoutProvider } from '../../../frontend/js/shared/context/layout-context'
|
2021-11-15 11:33:57 -05:00
|
|
|
import { DetachProvider } from '../../../frontend/js/shared/context/detach-context'
|
2021-04-23 05:01:16 -04:00
|
|
|
import { ChatProvider } from '../../../frontend/js/features/chat/context/chat-context'
|
2021-06-16 05:32:38 -04:00
|
|
|
import { IdeProvider } from '../../../frontend/js/shared/context/ide-context'
|
|
|
|
import { get } from 'lodash'
|
2021-06-25 04:13:17 -04:00
|
|
|
import { ProjectProvider } from '../../../frontend/js/shared/context/project-context'
|
2021-08-19 11:42:49 -04:00
|
|
|
import { SplitTestProvider } from '../../../frontend/js/shared/context/split-test-context'
|
2021-09-30 07:29:25 -04:00
|
|
|
import { CompileProvider } from '../../../frontend/js/shared/context/compile-context'
|
2022-01-10 10:47:01 -05:00
|
|
|
import { FileTreeDataProvider } from '../../../frontend/js/shared/context/file-tree-data-context'
|
2020-12-14 06:44:10 -05:00
|
|
|
|
2021-11-08 07:54:35 -05:00
|
|
|
// these constants can be imported in tests instead of
|
|
|
|
// using magic strings
|
|
|
|
export const PROJECT_ID = 'project123'
|
|
|
|
export const PROJECT_NAME = 'project-name'
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
export function EditorProviders({
|
2021-10-06 04:33:24 -04:00
|
|
|
user = { id: '123abd', email: 'testuser@example.com' },
|
2021-11-08 07:54:35 -05:00
|
|
|
projectId = PROJECT_ID,
|
2022-01-10 10:46:46 -05:00
|
|
|
rootDocId = '_root_doc_id',
|
2021-04-23 05:01:16 -04:00
|
|
|
socket = {
|
|
|
|
on: sinon.stub(),
|
2021-04-27 03:52:58 -04:00
|
|
|
removeListener: sinon.stub(),
|
2021-04-23 05:01:16 -04:00
|
|
|
},
|
2021-06-03 09:44:23 -04:00
|
|
|
isRestrictedTokenMember = false,
|
2021-09-14 04:54:08 -04:00
|
|
|
clsiServerId = '1234',
|
2021-06-16 05:32:38 -04:00
|
|
|
scope,
|
2022-01-10 10:46:46 -05:00
|
|
|
features = {
|
|
|
|
referencesSearch: true,
|
|
|
|
},
|
|
|
|
permissionsLevel = 'owner',
|
2021-04-27 03:52:58 -04:00
|
|
|
children,
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder = [
|
2022-01-10 10:46:46 -05:00
|
|
|
{
|
|
|
|
_id: 'root-folder-id',
|
|
|
|
name: 'rootFolder',
|
|
|
|
docs: [],
|
|
|
|
folders: [],
|
|
|
|
fileRefs: [],
|
|
|
|
},
|
|
|
|
],
|
2021-11-30 09:54:14 -05:00
|
|
|
ui = { view: null, pdfLayout: 'flat', chatOpen: true },
|
2022-01-10 10:46:05 -05:00
|
|
|
fileTreeManager = {
|
|
|
|
findEntityById: () => null,
|
|
|
|
findEntityByPath: () => null,
|
|
|
|
getEntityPath: () => '',
|
|
|
|
getRootDocDirname: () => '',
|
|
|
|
},
|
|
|
|
editorManager = {
|
|
|
|
getCurrentDocId: () => 'foo',
|
|
|
|
getCurrentDocValue: () => {},
|
|
|
|
openDoc: sinon.stub(),
|
|
|
|
},
|
2021-04-23 05:01:16 -04:00
|
|
|
}) {
|
2020-12-14 06:44:10 -05:00
|
|
|
window.user = user || window.user
|
2021-03-31 11:46:43 -04:00
|
|
|
window.gitBridgePublicBaseUrl = 'git.overleaf.test'
|
2020-12-14 06:44:10 -05:00
|
|
|
window.project_id = projectId != null ? projectId : window.project_id
|
2021-06-03 09:44:23 -04:00
|
|
|
window.isRestrictedTokenMember = isRestrictedTokenMember
|
2021-04-23 05:01:16 -04:00
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
const $scope = {
|
2021-06-25 04:14:07 -04:00
|
|
|
user: window.user,
|
2021-06-16 05:32:38 -04:00
|
|
|
project: {
|
2021-06-25 04:13:17 -04:00
|
|
|
_id: window.project_id,
|
2021-11-08 07:54:35 -05:00
|
|
|
name: PROJECT_NAME,
|
2021-06-16 05:32:38 -04:00
|
|
|
owner: {
|
|
|
|
_id: '124abd',
|
2021-06-25 04:13:17 -04:00
|
|
|
email: 'owner@example.com',
|
2021-02-23 05:17:41 -05:00
|
|
|
},
|
2022-01-10 10:46:46 -05:00
|
|
|
features,
|
|
|
|
rootDoc_id: rootDocId,
|
2022-01-10 10:47:01 -05:00
|
|
|
rootFolder,
|
2021-01-14 10:16:54 -05:00
|
|
|
},
|
2021-11-30 09:54:14 -05:00
|
|
|
ui,
|
2021-06-16 05:32:38 -04:00
|
|
|
$watch: (path, callback) => {
|
|
|
|
callback(get($scope, path))
|
|
|
|
return () => null
|
|
|
|
},
|
2021-10-21 06:31:51 -04:00
|
|
|
$applyAsync: sinon.stub(),
|
|
|
|
toggleHistory: sinon.stub(),
|
2022-01-10 10:46:46 -05:00
|
|
|
permissionsLevel,
|
2021-06-16 05:32:38 -04:00
|
|
|
...scope,
|
2021-01-14 10:16:54 -05:00
|
|
|
}
|
2021-06-16 05:32:38 -04:00
|
|
|
|
2021-11-16 05:27:24 -05:00
|
|
|
const metadataManager = {
|
|
|
|
metadata: {
|
|
|
|
state: {
|
|
|
|
documents: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:29:25 -04:00
|
|
|
window._ide = {
|
|
|
|
$scope,
|
|
|
|
socket,
|
|
|
|
clsiServerId,
|
2021-10-21 06:31:51 -04:00
|
|
|
editorManager,
|
2021-09-30 07:29:25 -04:00
|
|
|
fileTreeManager,
|
2021-11-16 05:27:24 -05:00
|
|
|
metadataManager,
|
2021-09-30 07:29:25 -04:00
|
|
|
}
|
2021-06-16 05:32:38 -04:00
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
return (
|
2021-08-19 11:42:49 -04:00
|
|
|
<SplitTestProvider>
|
|
|
|
<IdeProvider ide={window._ide}>
|
|
|
|
<UserProvider>
|
|
|
|
<ProjectProvider>
|
2022-01-10 10:47:01 -05:00
|
|
|
<FileTreeDataProvider>
|
|
|
|
<EditorProvider settings={{}}>
|
|
|
|
<DetachProvider>
|
|
|
|
<LayoutProvider>
|
|
|
|
<CompileProvider>{children}</CompileProvider>
|
|
|
|
</LayoutProvider>
|
|
|
|
</DetachProvider>
|
|
|
|
</EditorProvider>
|
|
|
|
</FileTreeDataProvider>
|
2021-08-19 11:42:49 -04:00
|
|
|
</ProjectProvider>
|
|
|
|
</UserProvider>
|
|
|
|
</IdeProvider>
|
|
|
|
</SplitTestProvider>
|
2020-12-14 06:44:10 -05:00
|
|
|
)
|
|
|
|
}
|
2021-02-09 10:37:48 -05:00
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
export function renderWithEditorContext(
|
|
|
|
component,
|
|
|
|
contextProps,
|
|
|
|
renderOptions = {}
|
|
|
|
) {
|
2021-06-16 05:32:38 -04:00
|
|
|
const EditorProvidersWrapper = ({ children }) => (
|
|
|
|
<EditorProviders {...contextProps}>{children}</EditorProviders>
|
|
|
|
)
|
|
|
|
|
2022-01-10 10:46:46 -05:00
|
|
|
return render(component, {
|
|
|
|
wrapper: EditorProvidersWrapper,
|
|
|
|
...renderOptions,
|
|
|
|
})
|
2021-04-23 05:01:16 -04:00
|
|
|
}
|
|
|
|
|
2021-12-14 08:24:53 -05:00
|
|
|
export function renderHookWithEditorContext(hook, contextProps) {
|
|
|
|
const EditorProvidersWrapper = ({ children }) => (
|
|
|
|
<EditorProviders {...contextProps}>{children}</EditorProviders>
|
|
|
|
)
|
|
|
|
|
|
|
|
return renderHook(hook, { wrapper: EditorProvidersWrapper })
|
|
|
|
}
|
|
|
|
|
2021-04-23 05:01:16 -04:00
|
|
|
export function ChatProviders({ children, ...props }) {
|
|
|
|
return (
|
|
|
|
<EditorProviders {...props}>
|
|
|
|
<ChatProvider>{children}</ChatProvider>
|
|
|
|
</EditorProviders>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-03 09:44:23 -04:00
|
|
|
export function renderWithChatContext(component, props) {
|
2021-06-16 05:32:38 -04:00
|
|
|
const ChatProvidersWrapper = ({ children }) => (
|
|
|
|
<ChatProviders {...props}>{children}</ChatProviders>
|
|
|
|
)
|
|
|
|
|
|
|
|
return render(component, { wrapper: ChatProvidersWrapper })
|
2021-02-09 10:37:48 -05:00
|
|
|
}
|
2021-02-17 05:05:33 -05:00
|
|
|
|
|
|
|
export function cleanUpContext() {
|
|
|
|
delete window.user
|
|
|
|
delete window.project_id
|
|
|
|
delete window._ide
|
|
|
|
}
|