1 |
|
2 |
|
3 | import expect from 'expect';
|
4 | import fs from 'fs';
|
5 | import path from 'path';
|
6 | import Slate from 'slate';
|
7 | import hyperprint from 'slate-hyperprint';
|
8 | import EditCode from '../lib';
|
9 |
|
10 | const PLUGIN = EditCode();
|
11 | const SCHEMA = Slate.Schema.create({
|
12 | plugins: [PLUGIN]
|
13 | });
|
14 |
|
15 | function 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 |
|
26 | describe('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 | });
|