1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Chore: Mobile: CodeMirror: Tests: Ensure full document is parsed, don't rely on timeout (#7405)

This commit is contained in:
Henry Heino 2022-12-07 13:28:17 -08:00 committed by GitHub
parent da01dc882b
commit 4860253bff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 17 deletions

View File

@ -894,9 +894,6 @@ packages/app-mobile/components/NoteBodyViewer/hooks/useSource.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js.map
@ -927,6 +924,12 @@ packages/app-mobile/components/NoteEditor/CodeMirror/markdownReformatter.test.js
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/theme.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/theme.js
packages/app-mobile/components/NoteEditor/CodeMirror/theme.js.map

9
.gitignore vendored
View File

@ -882,9 +882,6 @@ packages/app-mobile/components/NoteBodyViewer/hooks/useSource.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js
packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js
packages/app-mobile/components/NoteEditor/CodeMirror/createEditor.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js
packages/app-mobile/components/NoteEditor/CodeMirror/decoratorExtension.js.map
@ -915,6 +912,12 @@ packages/app-mobile/components/NoteEditor/CodeMirror/markdownReformatter.test.js
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js
packages/app-mobile/components/NoteEditor/CodeMirror/syntaxHighlightingLanguages.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/createEditor.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js
packages/app-mobile/components/NoteEditor/CodeMirror/testUtil/forceFullParse.js.map
packages/app-mobile/components/NoteEditor/CodeMirror/theme.d.ts
packages/app-mobile/components/NoteEditor/CodeMirror/theme.js
packages/app-mobile/components/NoteEditor/CodeMirror/theme.js.map

View File

@ -3,7 +3,7 @@
*/
import { EditorSelection } from '@codemirror/state';
import { ListType } from '../types';
import createEditor from './createEditor';
import createEditor from './testUtil/createEditor';
import { toggleList } from './markdownCommands';
describe('markdownCommands.bulletedVsChecklist', () => {

View File

@ -7,7 +7,7 @@ import {
increaseIndent, toggleList,
} from './markdownCommands';
import { ListType } from '../types';
import createEditor from './createEditor';
import createEditor from './testUtil/createEditor';
describe('markdownCommands.toggleList', () => {
it('should remove the same type of list', () => {

View File

@ -1,15 +1,14 @@
import { ensureSyntaxTree } from '@codemirror/language';
import { markdown } from '@codemirror/lang-markdown';
import { syntaxTree } from '@codemirror/language';
import { SyntaxNode } from '@lezer/common';
import { EditorState } from '@codemirror/state';
import { blockMathTagName, inlineMathContentTagName, inlineMathTagName, MarkdownMathExtension } from './markdownMathParser';
import { GFM as GithubFlavoredMarkdownExt } from '@lezer/markdown';
import { markdown } from '@codemirror/lang-markdown';
const syntaxTreeCreateTimeout = 100; // ms
import forceFullParse from './testUtil/forceFullParse';
// Creates an EditorState with math and markdown extensions
const createEditorState = (initialText: string): EditorState => {
return EditorState.create({
const editorState = EditorState.create({
doc: initialText,
extensions: [
markdown({
@ -17,13 +16,16 @@ const createEditorState = (initialText: string): EditorState => {
}),
],
});
forceFullParse(editorState);
return editorState;
};
// Returns a list of all nodes with the given name in the given editor's syntax tree.
// Attempts to create the syntax tree if it doesn't exist.
const findNodesWithName = (editor: EditorState, nodeName: string) => {
const result: SyntaxNode[] = [];
ensureSyntaxTree(editor, syntaxTreeCreateTimeout)?.iterate({
syntaxTree(editor).iterate({
enter: (node) => {
if (node.name === nodeName) {
result.push(node.node);

View File

@ -1,9 +1,10 @@
import { markdown } from '@codemirror/lang-markdown';
import { GFM as GithubFlavoredMarkdownExt } from '@lezer/markdown';
import { forceParsing, indentUnit } from '@codemirror/language';
import { indentUnit } from '@codemirror/language';
import { SelectionRange, EditorSelection, EditorState } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { MarkdownMathExtension } from './markdownMathParser';
import { MarkdownMathExtension } from '../markdownMathParser';
import forceFullParse from './forceFullParse';
// Creates and returns a minimal editor with markdown extensions
const createEditor = (initialText: string, initialSelection: SelectionRange): EditorView => {
@ -19,7 +20,7 @@ const createEditor = (initialText: string, initialSelection: SelectionRange): Ed
],
});
forceParsing(editor);
forceFullParse(editor.state);
return editor;
};

View File

@ -0,0 +1,18 @@
import { ensureSyntaxTree, syntaxTreeAvailable } from '@codemirror/language';
import { EditorState } from '@codemirror/state';
// Forces a full parse of a CodeMirror editor. This is intended for unit testing.
// If not in a unit-test consider using ensureSyntaxTree or forceParsing.
// This will throw if no language is configured for the editor.
const forceFullParse = (editorState: EditorState) => {
const timeout = 3000; // ms
ensureSyntaxTree(editorState, editorState.doc.length, timeout);
if (!syntaxTreeAvailable(editorState)) {
throw new Error(
`Unable to generate a syntax tree in ${timeout}. Is the editor configured to parse a language?`
);
}
};
export default forceFullParse;