Commit fc5d4866 authored by derek-knox's avatar derek-knox

Apply feedback - v2

Add 'Uneditable' to render helper methods,
use alias, and simplify the render_utils_spec
parent 473da192
...@@ -34,7 +34,7 @@ export const buildUneditableCloseTokens = (token, tagType = TAG_TYPES.block) => ...@@ -34,7 +34,7 @@ export const buildUneditableCloseTokens = (token, tagType = TAG_TYPES.block) =>
export const buildTextToken = content => buildToken('text', null, { content }); export const buildTextToken = content => buildToken('text', null, { content });
export const buildUneditableTokens = token => { export const buildUneditableBlockTokens = token => {
return [...buildUneditableOpenTokens(token), buildUneditableCloseToken()]; return [...buildUneditableOpenTokens(token), buildUneditableCloseToken()];
}; };
......
import { renderDefaultBlock } from './render_utils'; import { renderUneditableLeaf as render } from './render_utils';
const embeddedRubyRegex = /(^<%.+%>$)/; const embeddedRubyRegex = /(^<%.+%>$)/;
...@@ -6,6 +6,4 @@ const canRender = ({ literal }) => { ...@@ -6,6 +6,4 @@ const canRender = ({ literal }) => {
return embeddedRubyRegex.test(literal); return embeddedRubyRegex.test(literal);
}; };
const render = renderDefaultBlock;
export default { canRender, render }; export default { canRender, render };
import { renderEnterExitBlock } from './render_utils'; import { renderUneditableBranch as render } from './render_utils';
const identifierRegex = /(^\[.+\]: .+)/; const identifierRegex = /(^\[.+\]: .+)/;
...@@ -10,6 +10,4 @@ const canRender = (node, context) => { ...@@ -10,6 +10,4 @@ const canRender = (node, context) => {
return isIdentifier(context.getChildrenText(node)); return isIdentifier(context.getChildrenText(node));
}; };
const render = renderEnterExitBlock;
export default { canRender, render }; export default { canRender, render };
import { renderEnterExitBlock } from './render_utils'; import { renderUneditableBranch as render } from './render_utils';
const isKramdownTOC = ({ type, literal }) => type === 'text' && literal === 'TOC'; const isKramdownTOC = ({ type, literal }) => type === 'text' && literal === 'TOC';
...@@ -21,6 +21,4 @@ const canRender = node => { ...@@ -21,6 +21,4 @@ const canRender = node => {
return false; return false;
}; };
const render = renderEnterExitBlock;
export default { canRender, render }; export default { canRender, render };
import { renderDefaultBlock } from './render_utils'; import { renderUneditableLeaf as render } from './render_utils';
const kramdownRegex = /(^{:.+}$)/; const kramdownRegex = /(^{:.+}$)/;
...@@ -6,6 +6,4 @@ const canRender = ({ literal }) => { ...@@ -6,6 +6,4 @@ const canRender = ({ literal }) => {
return kramdownRegex.test(literal); return kramdownRegex.test(literal);
}; };
const render = renderDefaultBlock;
export default { canRender, render }; export default { canRender, render };
import { import {
buildUneditableTokens, buildUneditableBlockTokens,
buildUneditableOpenTokens, buildUneditableOpenTokens,
buildUneditableCloseToken, buildUneditableCloseToken,
} from './build_uneditable_token'; } from './build_uneditable_token';
export const renderDefaultBlock = (_, { origin }) => buildUneditableTokens(origin()); export const renderUneditableLeaf = (_, { origin }) => buildUneditableBlockTokens(origin());
export const renderEnterExitBlock = (_, { entering, origin }) => export const renderUneditableBranch = (_, { entering, origin }) =>
entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken(); entering ? buildUneditableOpenTokens(origin()) : buildUneditableCloseToken();
...@@ -3,7 +3,7 @@ import { ...@@ -3,7 +3,7 @@ import {
buildUneditableOpenTokens, buildUneditableOpenTokens,
buildUneditableCloseToken, buildUneditableCloseToken,
buildUneditableCloseTokens, buildUneditableCloseTokens,
buildUneditableTokens, buildUneditableBlockTokens,
buildUneditableInlineTokens, buildUneditableInlineTokens,
buildUneditableHtmlAsTextTokens, buildUneditableHtmlAsTextTokens,
} from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token'; } from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token';
...@@ -51,9 +51,9 @@ describe('Build Uneditable Token renderer helper', () => { ...@@ -51,9 +51,9 @@ describe('Build Uneditable Token renderer helper', () => {
}); });
}); });
describe('buildUneditableTokens', () => { describe('buildUneditableBlockTokens', () => {
it('returns a 3-item array of tokens with the originToken wrapped in the middle of block tokens', () => { it('returns a 3-item array of tokens with the originToken wrapped in the middle of block tokens', () => {
const result = buildUneditableTokens(originToken); const result = buildUneditableBlockTokens(originToken);
expect(result).toHaveLength(3); expect(result).toHaveLength(3);
expect(result).toStrictEqual(uneditableTokens); expect(result).toStrictEqual(uneditableTokens);
......
import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_embedded_ruby_text'; import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_embedded_ruby_text';
import { buildUneditableTokens } from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token'; import { renderUneditableLeaf } from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
import { buildMockTextNode, normalTextNode } from './mock_data'; import { buildMockTextNode, normalTextNode } from './mock_data';
...@@ -17,14 +17,8 @@ describe('Render Embedded Ruby Text renderer', () => { ...@@ -17,14 +17,8 @@ describe('Render Embedded Ruby Text renderer', () => {
}); });
describe('render', () => { describe('render', () => {
const origin = jest.fn(); it('should delegate rendering to the renderUneditableLeaf util', () => {
expect(renderer.render).toBe(renderUneditableLeaf);
it('should return uneditable tokens', () => {
const context = { origin };
expect(renderer.render(embeddedRubyTextNode, context)).toStrictEqual(
buildUneditableTokens(origin()),
);
}); });
}); });
}); });
import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_identifier_paragraph'; import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_identifier_paragraph';
import { import { renderUneditableBranch } from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
buildUneditableOpenTokens,
buildUneditableCloseToken,
} from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token';
import { buildMockTextNode } from './mock_data'; import { buildMockTextNode } from './mock_data';
...@@ -40,26 +37,8 @@ describe('Render Identifier Paragraph renderer', () => { ...@@ -40,26 +37,8 @@ describe('Render Identifier Paragraph renderer', () => {
}); });
describe('render', () => { describe('render', () => {
let origin; it('should delegate rendering to the renderUneditableBranch util', () => {
expect(renderer.render).toBe(renderUneditableBranch);
beforeEach(() => {
origin = jest.fn();
});
it('should return uneditable open tokens when entering', () => {
const context = { entering: true, origin };
expect(renderer.render(identifierParagraphNode, context)).toStrictEqual(
buildUneditableOpenTokens(origin()),
);
});
it('should return an uneditable close tokens when exiting', () => {
const context = { entering: false, origin };
expect(renderer.render(identifierParagraphNode, context)).toStrictEqual(
buildUneditableCloseToken(origin()),
);
}); });
}); });
}); });
import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_kramdown_list'; import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_kramdown_list';
import { import { renderUneditableBranch } from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
buildUneditableOpenTokens,
buildUneditableCloseToken,
} from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token';
import { buildMockTextNode } from './mock_data'; import { buildMockTextNode } from './mock_data';
...@@ -34,26 +31,8 @@ describe('Render Kramdown List renderer', () => { ...@@ -34,26 +31,8 @@ describe('Render Kramdown List renderer', () => {
}); });
describe('render', () => { describe('render', () => {
let origin; it('should delegate rendering to the renderUneditableBranch util', () => {
expect(renderer.render).toBe(renderUneditableBranch);
beforeEach(() => {
origin = jest.fn();
});
it('should return uneditable open tokens when entering', () => {
const context = { entering: true, origin };
expect(renderer.render(kramdownListNode, context)).toStrictEqual(
buildUneditableOpenTokens(origin()),
);
});
it('should return an uneditable close tokens when exiting', () => {
const context = { entering: false, origin };
expect(renderer.render(kramdownListNode, context)).toStrictEqual(
buildUneditableCloseToken(origin()),
);
}); });
}); });
}); });
import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_kramdown_text'; import renderer from '~/vue_shared/components/rich_content_editor/services/renderers/render_kramdown_text';
import { buildUneditableTokens } from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token'; import { renderUneditableLeaf } from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
import { buildMockTextNode, normalTextNode } from './mock_data'; import { buildMockTextNode, normalTextNode } from './mock_data';
...@@ -17,14 +17,8 @@ describe('Render Kramdown Text renderer', () => { ...@@ -17,14 +17,8 @@ describe('Render Kramdown Text renderer', () => {
}); });
describe('render', () => { describe('render', () => {
const origin = jest.fn(); it('should delegate rendering to the renderUneditableLeaf util', () => {
expect(renderer.render).toBe(renderUneditableLeaf);
it('should return uneditable tokens', () => {
const context = { origin };
expect(renderer.render(kramdownTextNode, context)).toStrictEqual(
buildUneditableTokens(origin()),
);
}); });
}); });
}); });
import { import {
renderDefaultBlock, renderUneditableLeaf,
renderEnterExitBlock, renderUneditableBranch,
} from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils'; } from '~/vue_shared/components/rich_content_editor/services/renderers/render_utils';
import { uneditableCloseToken } from './mock_data'; import {
buildUneditableBlockTokens,
buildUneditableOpenTokens,
} from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token';
import { originToken, uneditableCloseToken } from './mock_data';
describe('Render utils', () => { describe('Render utils', () => {
describe('renderDefaultBlock', () => { describe('renderUneditableLeaf', () => {
it('should...', () => { it('should return uneditable block tokens around an origin token', () => {
const context = { origin: jest.fn() }; const context = { origin: jest.fn().mockReturnValueOnce(originToken) };
const result = renderDefaultBlock({}, context); const result = renderUneditableLeaf({}, context);
expect(context.origin).toHaveBeenCalled(); expect(result).toStrictEqual(buildUneditableBlockTokens(originToken));
expect(result).toHaveLength(3);
expect(result[2]).toStrictEqual(uneditableCloseToken);
}); });
}); });
describe('renderEnterExitBlock', () => { describe('renderUneditableBranch', () => {
let origin; let origin;
beforeEach(() => { beforeEach(() => {
origin = jest.fn(); origin = jest.fn().mockReturnValueOnce(originToken);
}); });
it('should return two tokens as an array when entering', () => { it('should return uneditable block open token followed by the origin token when entering', () => {
const context = { entering: true, origin }; const context = { entering: true, origin };
const result = renderEnterExitBlock({}, context); const result = renderUneditableBranch({}, context);
expect(context.origin).toHaveBeenCalled(); expect(result).toStrictEqual(buildUneditableOpenTokens(originToken));
expect(result).toHaveLength(2);
}); });
it('should return a single closing token as an object when exiting', () => { it('should return uneditable block closing token when exiting', () => {
const context = { entering: false, origin }; const context = { entering: false, origin };
const result = renderEnterExitBlock({}, context); const result = renderUneditableBranch({}, context);
expect(context.origin).not.toHaveBeenCalled();
expect(result).toStrictEqual(uneditableCloseToken); 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