Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Commit

Permalink
Slightly better tests for Preview
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Mar 11, 2016
1 parent ad63226 commit a98e517
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
34 changes: 19 additions & 15 deletions app/components/Preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@ export default class Preview extends Component {
super(props, context);

this.requestAnimationId = false;
this.injectDependencies = this.injectDependencies.bind(this);
}

componentWillMount() {
PreviewLoader().then((deps) => {
this.marked = deps.marked.setOptions({
sanitize: false,
highlight: function (code) {
return deps.hljs.highlightAuto(code).value;
}
});
injectDependencies(marked, hljs, emojione) {
this.marked = marked.setOptions({
sanitize: false,
highlight: function (code) {
return hljs.highlightAuto(code).value;
}
});

this.emojione = deps.emojione;
this.emojione.ascii = true;
this.emojione = emojione;
this.emojione.ascii = true;
}

componentWillMount() {
PreviewLoader().then((deps) => {
this.injectDependencies(deps.marked, deps.hljs, deps.emojione);
this.forceUpdate();
});
}
Expand Down Expand Up @@ -51,19 +55,19 @@ export default class Preview extends Component {
return this.props.raw !== nextProps.raw;
}

getHTML(marked, emojione) {
getHTML() {
let html;

if (!marked) {
if (!this.marked) {
html = [
'<div class="preview-loader">',
'<p>Loading all the rendering stuff...</p>',
'<i class="fa fa-spinner fa-spin"></i>',
'</div>'
].join('');
} else {
html = marked(this.props.raw.toString());
html = emojione.toImage(html);
html = this.marked(this.props.raw.toString());
html = this.emojione.toImage(html);
}

return {
Expand All @@ -77,7 +81,7 @@ export default class Preview extends Component {
<div
ref="rendered"
className="rendered"
dangerouslySetInnerHTML={this.getHTML(this.marked, this.emojione)} />
dangerouslySetInnerHTML={this.getHTML()} />
</div>
);
}
Expand Down
27 changes: 10 additions & 17 deletions app/components/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ import Preview from '../Preview';

describe('<Preview />', () => {

before(() => {
marked.setOptions({
sanitize: false,
highlight: function (code) {
return hljs.highlightAuto(code).value;
}
});
});

it('renders a block with preview css class', () => {
const wrapper = shallow(<Preview raw={""} pos={0} />);
expect(wrapper.find('.preview')).to.have.length(1);
Expand All @@ -35,32 +26,34 @@ describe('<Preview />', () => {

it('renders content', () => {
const wrapper = shallow(<Preview raw={"foo"} pos={0} />);
const html = wrapper.instance().getHTML(marked, emojione).__html;
wrapper.instance().injectDependencies(marked, hljs, emojione);

expect(html).to.contain('foo');
expect(wrapper.instance().getHTML().__html).to.contain('foo');
});

it('converts markdown into HTML', () => {
const wrapper = shallow(<Preview raw={"*italic*"} pos={0} />);
const html = wrapper.instance().getHTML(marked, emojione).__html;
wrapper.instance().injectDependencies(marked, hljs, emojione);

expect(html).to.contain('<em>italic</em>');
expect(wrapper.instance().getHTML().__html).to.contain('<em>italic</em>');
});

it('converts Emoji', () => {
const wrapper = shallow(<Preview raw={":smile:"} pos={0} />);
const html = wrapper.instance().getHTML(marked, emojione).__html;

expect(html).to.contain(
wrapper.instance().injectDependencies(marked, hljs, emojione);

expect(wrapper.instance().getHTML().__html).to.contain(
'<img class="emojione" alt="😄" src="//cdn.jsdelivr.net/emojione/assets/png/1f604.png?v=2.1.1"/>'
);
});

it('highlights code blocks', () => {
const wrapper = shallow(<Preview raw={"```python\nprint()```"} pos={0} />);
const html = wrapper.instance().getHTML(marked, emojione).__html;

expect(html).to.contain(
wrapper.instance().injectDependencies(marked, hljs, emojione);

expect(wrapper.instance().getHTML().__html).to.contain(
[
'<pre><code class="lang-python">',
'<span class="hljs-function">',
Expand Down

0 comments on commit a98e517

Please sign in to comment.