UNPKG

1.66 kBJavaScriptView Raw
1/* eslint-disable global-require */
2/* eslint-disable import/no-dynamic-require */
3import expect from 'expect';
4import fs from 'fs';
5import path from 'path';
6import Slate from 'slate';
7import hyperprint from 'slate-hyperprint';
8import EditCode from '../lib';
9
10const PLUGIN = EditCode();
11const SCHEMA = Slate.Schema.create({
12 plugins: [PLUGIN]
13});
14
15function deserializeValue(value) {
16 return Slate.Value.fromJSON(
17 {
18 document: value.document,
19 selection: value.selection,
20 schema: SCHEMA
21 },
22 { normalize: false }
23 );
24}
25
26describe('slate-edit-code', () => {
27 const tests = fs.readdirSync(__dirname);
28
29 tests.forEach(test => {
30 if (test[0] === '.' || path.extname(test).length > 0) return;
31
32 it(test, () => {
33 Slate.resetKeyGenerator();
34 const dir = path.resolve(__dirname, test);
35 const input = require(path.resolve(dir, 'input.js')).default;
36 const expectedPath = path.resolve(dir, 'expected.js');
37 const expected =
38 fs.existsSync(expectedPath) && require(expectedPath).default;
39
40 const runChange = require(path.resolve(dir, 'change.js')).default;
41
42 const valueInput = deserializeValue(input);
43
44 const newChange = runChange(PLUGIN, valueInput.change());
45
46 if (expected) {
47 const newDoc = hyperprint(newChange.value.document, {
48 strict: true
49 });
50 expect(newDoc).toEqual(
51 hyperprint(expected.document, { strict: true })
52 );
53 }
54 });
55 });
56});