From ee29d499e517cf38294731da3df6ce428643a087 Mon Sep 17 00:00:00 2001 From: Nikolay Kostyurin Date: Mon, 24 Sep 2018 00:39:13 +0200 Subject: [PATCH] chore(core): string node walk plugin test --- packages/bbob-core/README.md | 8 +++--- packages/bbob-core/src/index.js | 4 +++ packages/bbob-core/test/index.test.js | 35 ++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/packages/bbob-core/README.md b/packages/bbob-core/README.md index 8953d30..167c391 100644 --- a/packages/bbob-core/README.md +++ b/packages/bbob-core/README.md @@ -3,12 +3,12 @@ ## Usage ```js -const bbob = require('@bbob/core'); -const render = require('@bbob/html'); -const presetHTML5 = require('@bbob/preset-html5'); +import bbob from '@bbob/core' +import { render } from '@bbob/html' +import presetHTML5 from '@bbob/preset-html5' const code = `[i]Text[/i]`; -const html = bbob([presetHTML5()]).process(code, { render }).html; +const html = bbob(presetHTML5()).process(code, { render }).html; console.log(html); // Text ``` diff --git a/packages/bbob-core/src/index.js b/packages/bbob-core/src/index.js index b7eebe8..27aca8e 100644 --- a/packages/bbob-core/src/index.js +++ b/packages/bbob-core/src/index.js @@ -19,6 +19,10 @@ export default function bbob(plugs) { const parseFn = options.parser || parse; const renderFn = options.render; + if (typeof parseFn !== 'function') { + throw new Error('"parser" is not a function, please pass to "process(input, { parser })" right function'); + } + let tree = options.skipParse ? input || [] : parseFn(input, options); diff --git a/packages/bbob-core/test/index.test.js b/packages/bbob-core/test/index.test.js index 74db909..b48dbda 100644 --- a/packages/bbob-core/test/index.test.js +++ b/packages/bbob-core/test/index.test.js @@ -1,4 +1,5 @@ import render from '@bbob/html' +import { TagNode } from '@bbob/parser' import core from '../src' const stringify = val => JSON.stringify(val); @@ -21,7 +22,7 @@ describe('@bbob/core', () => { ])) }); - test('plugin walk api', () => { + test('plugin walk api node', () => { const testPlugin = () => (tree) => tree.walk(node => { if (node.tag === 'mytag') { node.attrs = { @@ -55,6 +56,38 @@ describe('@bbob/core', () => { ])); }); + test('plugin walk api string', () => { + const testPlugin = () => (tree) => tree.walk(node => { + if (node === ':)') { + return TagNode.create('test-tag') + } + + return node + }); + + const res = process([testPlugin()], '[mytag]Large Text :)[/mytag]'); + const ast = res.tree; + + expect(ast).toBeInstanceOf(Array); + expect(ast.walk).toBeInstanceOf(Function); + expect(stringify(ast)).toEqual(stringify([ + { + tag: 'mytag', + attrs: {}, + content: [ + 'Large', + ' ', + 'Text', + { + tag: 'test-tag', + attrs: {}, + content: [], + } + ] + } + ])); + }); + test('plugin match api', () => { const testPlugin = () => (tree) => tree.match([{ tag: 'mytag1' }, { tag: 'mytag2' }], node => { if (node.attrs) {