Skip to content

Commit

Permalink
fix(text editor): update menu command tests
Browse files Browse the repository at this point in the history
  • Loading branch information
john-traas committed May 20, 2024
1 parent 8b40f3a commit 9fbd2ae
Showing 1 changed file with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,65 @@
import { Schema } from 'prosemirror-model';
/* eslint-disable multiline-ternary */
import { EditorState } from 'prosemirror-state';
import { Schema, DOMParser } from 'prosemirror-model';
import { addListNodes } from 'prosemirror-schema-list';
import { schema } from 'prosemirror-schema-basic';

import { schema as basicSchema } from 'prosemirror-schema-basic';
import { keymap } from 'prosemirror-keymap';
import { exampleSetup } from 'prosemirror-example-setup';
import { EditorView } from 'prosemirror-view';
import { MenuCommandFactory } from './menu-commands';
import { EditorMenuTypes } from './types';

describe('MenuCommandFactory', () => {
let mySchema: Schema;
let factory: MenuCommandFactory;
let state: EditorState;
let view: EditorView;
let buildKeymap: Function;

beforeEach(() => {
mySchema = new Schema({
nodes: addListNodes(schema.spec.nodes, 'paragraph block*', 'block'),
marks: schema.spec.marks,
nodes: addListNodes(
basicSchema.spec.nodes,
'paragraph block*',
'block',
),
marks: basicSchema.spec.marks,
});

factory = new MenuCommandFactory(mySchema);

buildKeymap = () => {
return {
'Mod-B': factory.getCommand(EditorMenuTypes.Bold),
'Mod-I': factory.getCommand(EditorMenuTypes.Italic),
'Mod-Shift-1': factory.getCommand(EditorMenuTypes.HeaderLevel1),
'Mod-Shift-2': factory.getCommand(EditorMenuTypes.HeaderLevel2),
'Mod-Shift-3': factory.getCommand(EditorMenuTypes.HeaderLevel3),
};
};

const doc = DOMParser.fromSchema(mySchema).parse(
document.createElement('div'),
);

state = EditorState.create({
doc: doc,
plugins: [
...exampleSetup({ schema: mySchema, menuBar: false }),
keymap(buildKeymap()),
],
});

document.body.innerHTML = '<div id="editor"></div>';
const editorDiv = document.getElementById('editor');

view = new EditorView(editorDiv!, {
state: state,
dispatchTransaction: (tr) => {
state = state.apply(tr);
view.updateState(state);
},
});
});

it('getCommand returns a command function for each EditorMenuTypes', () => {
Expand All @@ -24,21 +69,21 @@ describe('MenuCommandFactory', () => {
});
});

it('getCommand throws an error when an unsupported EditorMenuTypes is passed', () => {
expect(() =>
factory.getCommand('unsupportedType' as any),
).toThrowError();
});

it('buildKeymap returns a keymap object', () => {
const keymap = factory.buildKeymap();
expect(typeof keymap).toBe('object');
expect(Object.keys(keymap)).toEqual([
const myKeymap = buildKeymap();
expect(typeof myKeymap).toBe('object');
expect(Object.keys(myKeymap)).toEqual([
'Mod-B',
'Mod-I',
'Mod-Shift-1',
'Mod-Shift-2',
'Mod-Shift-3',
]);
});

it('getCommand throws error for unsupported mark', () => {
expect(() => {
factory.getCommand('unsupported_mark' as EditorMenuTypes);
}).toThrow('The Mark "unsupported_mark" is not supported');
});
});

0 comments on commit 9fbd2ae

Please sign in to comment.