All files / collaborative-prosemirror/src/__tests__ setup.ts

98% Statements 98/100
50% Branches 2/4
100% Functions 11/11
100% Lines 89/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 13412x 12x 12x 12x 12x 12x 12x 12x 12x   12x   12x 717x     12x 214x 214x 214x 214x 214x 214x 214x     214x     12x 209x 209x 209x 209x 209x 209x     209x 209x 209x 209x     209x 209x 209x 209x 209x 209x 209x     12x 4x 4x 4x 4x 29x 29x 29x     29x       12x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     12x   72x 72x 72x     72x 72x 72x     72x 72x 72x 72x     72x 286x   72x 72x 72x 72x 72x 72x 72x 72x 72x     72x   72x               22x 22x 22x 22x 22x     72x        
import {ModelWithExt as Model, ext} from 'json-joy/lib/json-crdt-extensions';
import {FromPm} from '../sync/FromPm';
import {ToPmNode} from '../sync/toPmNode';
import {Node} from 'prosemirror-model';
import {schema} from 'prosemirror-test-builder';
import {EditorState, TextSelection} from 'prosemirror-state';
import {EditorView} from 'prosemirror-view';
import {ProseMirrorFacade} from '../ProseMirrorFacade';
import {PeritextBinding} from '@jsonjoy.com/collaborative-peritext';
 
const activeCleanups = new Set<() => void>();
 
export const cleanupTestbeds = (): void => {
  for (const cleanup of [...activeCleanups]) cleanup();
};
 
export const assertCanConvert = (doc: Node) => {
  const viewRange = FromPm.convert(doc);
  const model = Model.create(ext.peritext.new(''));
  const api = model.s.toExt();
  api.txt.editor.import(0, viewRange);
  api.txt.refresh();
  const toPm = new ToPmNode(schema);
  const view = toPm.convert(api.txt.blocks).toJSON();
  // console.log(JSON.stringify(view, null, 2));
  // console.log(JSON.stringify(doc.toJSON(), null, 2));
  expect(view).toEqual(doc.toJSON());
};
 
export const assertCanMergeInto = (doc1: Node, doc2: Node) => {
  const toPm = new ToPmNode(schema);
  const model = Model.create(ext.peritext.new(''));
  const api = model.s.toExt();
  api.txt.editor.merge(FromPm.convert(doc1));
  api.txt.refresh();
  const view = toPm.convert(api.txt.blocks).toJSON();
  // logTree(view);
  // logTree(api.txt.editor.export());
  expect(Node.fromJSON(schema, view).toJSON()).toEqual(doc1.toJSON());
  api.txt.editor.merge(FromPm.convert(doc2));
  api.txt.refresh();
  const view2 = toPm.convert(api.txt.blocks).toJSON();
  // logTree(view2);
  // logTree(doc2.toJSON());
  expect(Node.fromJSON(schema, view2).toJSON()).toEqual(doc2.toJSON());
  const model2 = Model.create(ext.peritext.new(''));
  const api2 = model2.s.toExt();
  const viewRange2 = FromPm.convert(doc2);
  api2.txt.editor.merge(viewRange2);
  api2.txt.refresh();
  expect(api2.txt.editor.export()).toEqual(api.txt.editor.export());
};
 
export const assertCanMergeTrain = (docs: Node[]) => {
  const toPm = new ToPmNode(schema);
  const model = Model.create(ext.peritext.new(''));
  const api = model.s.toExt();
  for (const doc of docs) {
    api.txt.editor.merge(FromPm.convert(doc));
    api.txt.refresh();
    const view = toPm.convert(api.txt.blocks).toJSON();
    // logTree(view);
    // logTree(api.txt.editor.export());
    expect(Node.fromJSON(schema, view).toJSON()).toEqual(doc.toJSON());
  }
};
 
export const assertEmptyMerge = (doc: Node) => {
  const model = Model.create(ext.peritext.new(''));
  const api = model.s.toExt();
  const patch1 = api.txt.editor.merge(FromPm.convert(doc));
  expect(patch1).not.toEqual([void 0, void 0, void 0]);
  api.txt.refresh();
  const patch2 = api.txt.editor.merge(FromPm.convert(doc));
  expect(patch2).toEqual([void 0, void 0, void 0]);
  api.txt.refresh();
  const patch3 = api.txt.editor.merge(FromPm.convert(doc));
  expect(patch3).toEqual([void 0, void 0, void 0]);
};
 
export const setup = (pmDoc: Node) => {
  // 1. Create Peritext model
  const model = Model.create(ext.peritext.new(''));
  const api = model.s.toExt();
  const txt = api.txt;
 
  // 1.2 Set the PM doc as the initial content of the Peritext model.
  const viewRange = FromPm.convert(pmDoc);
  txt.editor.import(0, viewRange);
  txt.refresh();
 
  // 2. Create a real ProseMirror EditorView in jsdom
  const place = document.createElement('div');
  document.body.appendChild(place);
  const state = EditorState.create({doc: pmDoc});
  const view = new EditorView(place, {state});
 
  // 3. Wrap with ProseMirrorFacade and bind to the Peritext model
  const facade = new ProseMirrorFacade(view, () => api);
  const unbind = PeritextBinding.bind(() => api, facade);
 
  let cleaned = false;
  const cleanup = () => {
    Iif (cleaned) return;
    cleaned = true;
    activeCleanups.delete(cleanup);
    unbind();
    facade.dispose();
    view.destroy();
    Eif (place.parentNode) place.parentNode.removeChild(place);
  };
 
  activeCleanups.add(cleanup);
 
  return {
    facade,
    view,
    api,
    txt,
    cleanup,
    unbind,
    getSelectionAt: (anchor: number, head: number) => {
      const {doc} = view.state;
      const sel = TextSelection.create(doc, anchor, head);
      const tr = view.state.tr.setSelection(sel);
      view.dispatch(tr);
      return facade.getSelection(api);
    },
    [Symbol.dispose]() {
      cleanup();
    },
  };
};