Commit 473da192 authored by derek-knox's avatar derek-knox

Add render_utils.js for DRY-sake

Minor refactoring so custom renderers are
more DRY. Also cleaned up an origin() call
parent 6b32bd5c
import { buildUneditableTokens } from './build_uneditable_token';
import { renderDefaultBlock } from './render_utils';
const embeddedRubyRegex = /(^<%.+%>$)/;
......@@ -6,8 +6,6 @@ const canRender = ({ literal }) => {
return embeddedRubyRegex.test(literal);
};
const render = (_, { origin }) => {
return buildUneditableTokens(origin());
};
const render = renderDefaultBlock;
export default { canRender, render };
import { buildUneditableOpenTokens, buildUneditableCloseToken } from './build_uneditable_token';
import { renderEnterExitBlock } from './render_utils';
const identifierRegex = /(^\[.+\]: .+)/;
......@@ -10,7 +10,6 @@ const canRender = (node, context) => {
return isIdentifier(context.getChildrenText(node));
};
const render = (_, { entering, origin }) =>
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
const render = renderEnterExitBlock;
export default { canRender, render };
import { buildUneditableOpenTokens, buildUneditableCloseToken } from './build_uneditable_token';
import { renderEnterExitBlock } from './render_utils';
const isKramdownTOC = ({ type, literal }) => type === 'text' && literal === 'TOC';
......@@ -21,7 +21,6 @@ const canRender = node => {
return false;
};
const render = (_, { entering, origin }) =>
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
const render = renderEnterExitBlock;
export default { canRender, render };
import { buildUneditableTokens } from './build_uneditable_token';
import { renderDefaultBlock } from './render_utils';
const kramdownRegex = /(^{:.+}$)/;
......@@ -6,8 +6,6 @@ const canRender = ({ literal }) => {
return kramdownRegex.test(literal);
};
const render = (_, { origin }) => {
return buildUneditableTokens(origin());
};
const render = renderDefaultBlock;
export default { canRender, render };
import {
buildUneditableTokens,
buildUneditableOpenTokens,
buildUneditableCloseToken,
} from './build_uneditable_token';
export const renderDefaultBlock = (_, { origin }) => buildUneditableTokens(origin());
export const renderEnterExitBlock = (_, { entering, origin }) =>
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
......@@ -34,7 +34,11 @@ describe('Render Kramdown List renderer', () => {
});
describe('render', () => {
const origin = jest.fn();
let origin;
beforeEach(() => {
origin = jest.fn();
});
it('should return uneditable open tokens when entering', () => {
const context = { entering: true, origin };
......
import {
renderDefaultBlock,
renderEnterExitBlock,
} from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
import { uneditableCloseToken } from './mock_data';
describe('Render utils', () => {
describe('renderDefaultBlock', () => {
it('should...', () => {
const context = { origin: jest.fn() };
const result = renderDefaultBlock({}, context);
expect(context.origin).toHaveBeenCalled();
expect(result).toHaveLength(3);
expect(result[2]).toStrictEqual(uneditableCloseToken);
});
});
describe('renderEnterExitBlock', () => {
let origin;
beforeEach(() => {
origin = jest.fn();
});
it('should return two tokens as an array when entering', () => {
const context = { entering: true, origin };
const result = renderEnterExitBlock({}, context);
expect(context.origin).toHaveBeenCalled();
expect(result).toHaveLength(2);
});
it('should return a single closing token as an object when exiting', () => {
const context = { entering: false, origin };
const result = renderEnterExitBlock({}, context);
expect(context.origin).not.toHaveBeenCalled();
expect(result).toStrictEqual(uneditableCloseToken);
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment