From 142ba2ba1891f55b7f4a7cd3cb1ba979af156a57 Mon Sep 17 00:00:00 2001 From: Max Stoiber Date: Fri, 5 May 2017 20:46:44 +0200 Subject: [PATCH] Add tests for plugins (#792) * Add tests for plugins The first test fails because the rules property of plugins doesn't work as expected. * Fix plugins tes --- test/index.js | 1 + test/plugins/fixtures/custom-rules/index.js | 28 ++++++++ test/plugins/fixtures/custom-rules/input.yaml | 8 +++ .../plugins/fixtures/custom-rules/output.html | 4 ++ test/plugins/index.js | 65 +++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100644 test/plugins/fixtures/custom-rules/index.js create mode 100644 test/plugins/fixtures/custom-rules/input.yaml create mode 100644 test/plugins/fixtures/custom-rules/output.html create mode 100644 test/plugins/index.js diff --git a/test/index.js b/test/index.js index 53154e9b1..78c199fdf 100644 --- a/test/index.js +++ b/test/index.js @@ -11,6 +11,7 @@ import 'babel-polyfill' * Tests. */ +import './plugins' import './rendering' import './schema' import './serializers' diff --git a/test/plugins/fixtures/custom-rules/index.js b/test/plugins/fixtures/custom-rules/index.js new file mode 100644 index 000000000..3bffc0643 --- /dev/null +++ b/test/plugins/fixtures/custom-rules/index.js @@ -0,0 +1,28 @@ + +import { Mark } from '../../../..' + +const BOLD = { + fontWeight: 'bold' +} + +function decorate(text, block) { + let { characters } = text + let second = characters.get(1) + const mark = Mark.create({ type: 'bold' }) + const marks = second.marks.add(mark) + second = second.merge({ marks }) + characters = characters.set(1, second) + return characters +} + +export const plugins = [{ + schema: { + marks: { + bold: BOLD + }, + rules: [{ + match: () => true, + decorate, + }] + } +}] diff --git a/test/plugins/fixtures/custom-rules/input.yaml b/test/plugins/fixtures/custom-rules/input.yaml new file mode 100644 index 000000000..9c6aefff6 --- /dev/null +++ b/test/plugins/fixtures/custom-rules/input.yaml @@ -0,0 +1,8 @@ + +nodes: + - kind: block + type: default + nodes: + - kind: text + ranges: + - text: word diff --git a/test/plugins/fixtures/custom-rules/output.html b/test/plugins/fixtures/custom-rules/output.html new file mode 100644 index 000000000..f8a474857 --- /dev/null +++ b/test/plugins/fixtures/custom-rules/output.html @@ -0,0 +1,4 @@ +
+
word +
+
diff --git a/test/plugins/index.js b/test/plugins/index.js new file mode 100644 index 000000000..b69c2c783 --- /dev/null +++ b/test/plugins/index.js @@ -0,0 +1,65 @@ + +import React from 'react' +import ReactDOM from 'react-dom/server' +import assert from 'assert' +import cheerio from 'cheerio' +import fs from 'fs-promise' +import readYaml from 'read-yaml-promise' +import { Editor, Raw } from '../..' +import { resolve } from 'path' + +/** + * Tests. + */ + +describe.only('plugins', () => { + const tests = fs.readdirSync(resolve(__dirname, './fixtures')) + + for (const test of tests) { + if (test[0] === '.') continue + + it(test, async () => { + const dir = resolve(__dirname, './fixtures', test) + const input = await readYaml(resolve(dir, 'input.yaml')) + const output = await fs.readFile(resolve(dir, 'output.html'), 'utf8') + const props = { + state: Raw.deserialize(input, { terse: true }), + onChange: () => {}, + ...require(dir) + } + + const string = ReactDOM.renderToStaticMarkup() + const expected = cheerio + .load(output) + .html() + .trim() + .replace(/\n/gm, '') + .replace(/>\s*<') + + assert.equal(clean(string), expected) + }) + } +}) + +/** + * Clean a renderer `html` string, removing dynamic attributes. + * + * @param {String} html + * @return {String} + */ + +function clean(html) { + const $ = cheerio.load(html) + + $('*').each((i, el) => { + $(el).removeAttr('data-key') + $(el).removeAttr('data-offset-key') + }) + + $.root().children().removeAttr('autocorrect') + $.root().children().removeAttr('spellcheck') + $.root().children().removeAttr('style') + $.root().children().removeAttr('data-gramm') + + return $.html() +}