Testing input-rules

Hi there, I am looking for help understanding how to test input rules.

I am starting by trying to write a test for the emDash rule already provided by the prosemirror-inputrules package.

My test:

describe('inputRules', () => {
  const rules = inputRules({rules: [emDash]});
  const plugins = [rules];

  const paragraphNode = mySchema.node('paragraph', null, []);
  const doc = mySchema.node('doc', null, [paragraphNode]);
  
  const state = EditorState.create({
    doc,
    plugins,
    selection: TextSelection.create(doc,1, 1),
  });

  it('converts two hyphens into emdash', () => {
    const newState = state.apply(state.tr.insertText('-').insertText('-'));
    const c = newState.doc.textContent;

    // The below fails - c equals "--" but I am expecting emdash
    expect(c).toEqual(expect.stringMatching(/$emDash/));
  });
});

How can I test the emDash rule?

You will need to create a view to run input rules. Then you should be able to run view.someProp("handleTextInput", f => f(view, 1, 1, "-")) to trigger the input rule.

2 Likes

Very helpful, thanks!