Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/string context #40

Merged
merged 2 commits into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/test/code-snippet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ class CodeSnippet extends Component {
render() {
const { className, code, lang, label, showLabel, useInlineDiffs } = this.props;
const isDiff = lang === 'diff';
const cxName = cx(className, lang, {
hljs: !this.shouldHighlight(),
const shouldHighlight = this.shouldHighlight();
const cxName = cx(className, {
[lang]: shouldHighlight,
hljs: !shouldHighlight,
'code-diff': isDiff,
'inline-diff': isDiff && useInlineDiffs
});
Expand Down
9 changes: 7 additions & 2 deletions src/components/test/context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ class TestContext extends Component {
return this.renderLink(content, title);
}

// Default
const code = isString(content) ? content : JSON.stringify(content, null, 2);
// Simple string
if (isString(content)) {
return <CodeSnippet className={ cx('code-snippet') } code={ content } highlight={ false } />;
}

// All other types (primitives, objects, arrays...)
const code = JSON.stringify(content, null, 2);
return (
<CodeSnippet className={ cx('code-snippet') } code={ code } highlight={ highlight } />
);
Expand Down
11 changes: 8 additions & 3 deletions test/spec/components/test/code-snippet.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe('<CodeSnippet />', () => {
const props = {
code: 'function(){console.log(\'sample code\');}'
};
getInstance(props);
const wrapper = getInstance(props);
expect(wrapper.hasClass('javascript')).to.equal(true);
expect(document.querySelectorAll('.hljs-keyword').length).to.equal(1);
});

Expand All @@ -49,6 +50,7 @@ describe('<CodeSnippet />', () => {
lang: 'diff'
};
const wrapper = getInstance(props);
expect(wrapper.hasClass('diff')).to.equal(true);
expect(wrapper.hasClass('inline-diff')).to.equal(false);
expect(document.querySelectorAll('.test-code-diff-expected').length).to.equal(1);
expect(document.querySelectorAll('.test-code-diff-actual').length).to.equal(1);
Expand All @@ -67,6 +69,7 @@ describe('<CodeSnippet />', () => {
lang: 'diff'
};
const wrapper = getInstance(props, { useInlineDiffs: true });
expect(wrapper.hasClass('diff')).to.equal(false);
expect(wrapper.hasClass('inline-diff')).to.equal(true);
expect(document.querySelectorAll('.test-code-diff-expected').length).to.equal(2);
expect(document.querySelectorAll('.test-code-diff-actual').length).to.equal(2);
Expand All @@ -75,9 +78,11 @@ describe('<CodeSnippet />', () => {
it('does not highlight when prop is false', () => {
const props = {
code: 'function(){console.log(\'sample code\');}',
highlight: false
highlight: false,
lang: 'piglatin'
};
getInstance(props);
const wrapper = getInstance(props);
expect(wrapper.hasClass('piglatin')).to.equal(false);
expect(document.querySelectorAll('.hljs-keyword').length).to.equal(0);
});

Expand Down
239 changes: 124 additions & 115 deletions test/spec/components/test/context.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,143 +21,152 @@ describe('<TestContext />', () => {
};
};

it('renders context when value is undefined', () => {
const context = {
title: 'sample context',
value: 'undefined'
};
const { wrapper, ctxItems } = getInstance({
context: JSON.stringify(context),
className: 'test'
describe('when context is a string', () => {
it('renders simple string', () => {
const context = 'sample context';
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(ctxItems).to.have.lengthOf(1);
});

it('renders context with string', () => {
const context = 'sample context';
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders url with protocol', () => {
const context = 'http://test.url.com/somepath';
const { wrapper, snippet, link } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(link).to.have.lengthOf(1);
link.simulate('click', { stopPropagation: noop });
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
});

it('renders context with string, url with protocol', () => {
const context = 'http://test.url.com/somepath';
const { wrapper, snippet, link } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders url without protocol', () => {
const context = 'test.url.com/somepath';
const { wrapper, snippet, link } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(link).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(link).to.have.lengthOf(1);
link.simulate('click', { stopPropagation: noop });
});

it('renders context with string, url without protocol', () => {
const context = 'test.url.com/somepath';
const { wrapper, snippet, link } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders image url with protocol', () => {
const context = 'http://test.url.com/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
img.simulate('click', { stopPropagation: noop });
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(link).to.have.lengthOf(1);
});

it('renders context with string, image url with protocol', () => {
const context = 'http://test.url.com/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders image url without protocol', () => {
const context = 'test.url.com/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
img.simulate('click', { stopPropagation: noop });
});

it('renders context with string, image url without protocol', () => {
const context = 'test.url.com/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders local image', () => {
const context = '/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});

it('renders context with string, local image', () => {
const context = '/testimage.png';
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
describe('when context is an object', () => {
it('renders undefined value', () => {
const context = {
title: 'sample context',
value: 'undefined'
};
const { wrapper, ctxItems } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(ctxItems).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});

it('renders context with object, string value', () => {
const context = {
title: 'sample context',
value: 'context string'
};
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'

it('renders string value', () => {
const context = {
title: 'sample context',
value: 'context string'
};
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
expect(snippet.prop('highlight')).to.equal(false);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
});

it('renders context with object, image url value', () => {
const context = {
title: 'sample context',
value: 'http://test.url.com/testimage.png'
};
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders image url value', () => {
const context = {
title: 'sample context',
value: 'http://test.url.com/testimage.png'
};
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(0);
expect(img).to.have.lengthOf(1);
});

it('renders context with object, object value', () => {
const context = {
title: 'sample context',
value: { testing: true }
};
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'
it('renders object value', () => {
const context = {
title: 'sample context',
value: { testing: true }
};
const { wrapper, snippet } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
expect(snippet.prop('highlight')).to.equal(true);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
});

it('renders context with array', () => {
const context = [
{
title: 'sample context a',
value: 'http://test.url.com/testimage.png'
},
{
title: 'sample context b',
value: { testing: true }
}
];
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
describe('when context is an array', () => {
it('renders', () => {
const context = [
{
title: 'sample context a',
value: 'http://test.url.com/testimage.png'
},
{
title: 'sample context b',
value: { testing: true }
}
];
const { wrapper, snippet, img } = getInstance({
context: JSON.stringify(context),
className: 'test'
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
expect(img).to.have.lengthOf(1);
});
expect(wrapper).to.have.className('test');
expect(snippet).to.have.lengthOf(1);
expect(img).to.have.lengthOf(1);
});
});