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 = /(^<%.+%>$)/; const embeddedRubyRegex = /(^<%.+%>$)/;
...@@ -6,8 +6,6 @@ const canRender = ({ literal }) => { ...@@ -6,8 +6,6 @@ const canRender = ({ literal }) => {
return embeddedRubyRegex.test(literal); return embeddedRubyRegex.test(literal);
}; };
const render = (_, { origin }) => { const render = renderDefaultBlock;
return buildUneditableTokens(origin());
};
export default { canRender, render }; export default { canRender, render };
import { buildUneditableOpenTokens, buildUneditableCloseToken } from './build_uneditable_token'; import { renderEnterExitBlock } from './render_utils';
const identifierRegex = /(^\[.+\]: .+)/; const identifierRegex = /(^\[.+\]: .+)/;
...@@ -10,7 +10,6 @@ const canRender = (node, context) => { ...@@ -10,7 +10,6 @@ const canRender = (node, context) => {
return isIdentifier(context.getChildrenText(node)); return isIdentifier(context.getChildrenText(node));
}; };
const render = (_, { entering, origin }) => const render = renderEnterExitBlock;
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
export default { canRender, render }; export default { canRender, render };
import { buildUneditableOpenTokens, buildUneditableCloseToken } from './build_uneditable_token'; import { renderEnterExitBlock } from './render_utils';
const isKramdownTOC = ({ type, literal }) => type === 'text' && literal === 'TOC'; const isKramdownTOC = ({ type, literal }) => type === 'text' && literal === 'TOC';
...@@ -21,7 +21,6 @@ const canRender = node => { ...@@ -21,7 +21,6 @@ const canRender = node => {
return false; return false;
}; };
const render = (_, { entering, origin }) => const render = renderEnterExitBlock;
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
export default { canRender, render }; export default { canRender, render };
import { buildUneditableTokens } from './build_uneditable_token'; import { renderDefaultBlock } from './render_utils';
const kramdownRegex = /(^{:.+}$)/; const kramdownRegex = /(^{:.+}$)/;
...@@ -6,8 +6,6 @@ const canRender = ({ literal }) => { ...@@ -6,8 +6,6 @@ const canRender = ({ literal }) => {
return kramdownRegex.test(literal); return kramdownRegex.test(literal);
}; };
const render = (_, { origin }) => { const render = renderDefaultBlock;
return buildUneditableTokens(origin());
};
export default { canRender, render }; 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', () => { ...@@ -34,7 +34,11 @@ describe('Render Kramdown List renderer', () => {
}); });
describe('render', () => { describe('render', () => {
const origin = jest.fn(); let origin;
beforeEach(() => {
origin = jest.fn();
});
it('should return uneditable open tokens when entering', () => { it('should return uneditable open tokens when entering', () => {
const context = { entering: true, origin }; 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