2016-09-22 22:34:43 +02:00
/* eslint max-len:0 */
2016-09-22 17:36:02 +02:00
import React from 'react' ;
2016-09-22 21:52:43 +02:00
import { shallow } from 'enzyme' ;
2016-09-22 17:36:02 +02:00
import markdownSyntax from 'markup-it/syntaxes/markdown' ;
import htmlSyntax from 'markup-it/syntaxes/html' ;
import MarkitupReactRenderer from '../MarkitupReactRenderer' ;
describe ( 'MarkitupReactRenderer' , ( ) => {
2016-09-22 22:34:43 +02:00
describe ( 'basics' , ( ) => {
it ( 'should re-render properly after a value and syntax update' , ( ) => {
const component = shallow (
< MarkitupReactRenderer
value = "# Title"
syntax = { markdownSyntax }
/ >
) ;
const tree1 = component . html ( ) ;
component . setProps ( {
value : '<h1>Title</h1>' ,
syntax : htmlSyntax
} ) ;
const tree2 = component . html ( ) ;
expect ( tree1 ) . toEqual ( tree2 ) ;
2016-09-22 21:52:43 +02:00
} ) ;
2016-09-22 22:34:43 +02:00
it ( 'should not update the parser if syntax didn\'t change' , ( ) => {
const component = shallow (
< MarkitupReactRenderer
value = "# Title"
syntax = { markdownSyntax }
/ >
) ;
const syntax1 = component . instance ( ) . props . syntax ;
component . setProps ( {
value : '## Title' ,
} ) ;
const syntax2 = component . instance ( ) . props . syntax ;
expect ( syntax1 ) . toEqual ( syntax2 ) ;
2016-09-22 21:52:43 +02:00
} ) ;
} ) ;
2016-09-22 22:34:43 +02:00
describe ( 'Markdown rendering' , ( ) => {
describe ( 'General' , ( ) => {
it ( 'should render markdown' , ( ) => {
const value = `
2016-09-22 17:36:02 +02:00
# H1
Text with * * bold * * & _em _ elements
# # H2
* ul item 1
* ul item 2
# # # H3
1. ol item 1
1. ol item 2
1. ol item 3
# # # # H4
[ link title ] ( http : //google.com)
# # # # # H5
! [ alt text ] ( https : //pbs.twimg.com/profile_images/678903331176214528/TQTdqGwD.jpg)
# # # # # # H6
2016-09-22 21:52:43 +02:00
` ;
2016-09-22 22:34:43 +02:00
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
expect ( tree ) . toMatchSnapshot ( ) ;
} ) ;
} ) ;
describe ( 'Links' , ( ) => {
it ( 'should render links' , ( ) => {
const value = `
I get 10 times more traffic from [ Google ] [ 1 ] than from [ Yahoo ] [ 2 ] or [ MSN ] [ 3 ] .
[ 1 ] : http : //google.com/ "Google"
[ 2 ] : http : //search.yahoo.com/ "Yahoo Search"
[ 3 ] : http : //search.msn.com/ "MSN Search"
` ;
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
const expected = '<article><p>I get 10 times more traffic from <a href="http://google.com/" title="Google">Google</a> than from <a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p></article>' ;
expect ( tree ) . toEqual ( expected ) ;
} ) ;
} ) ;
describe ( 'Code' , ( ) => {
it ( 'should render code' , ( ) => {
const value = 'Use the `printf()` function.' ;
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
const expected = '<article><p>Use the <code>printf()</code> function.</p></article>' ;
expect ( tree ) . toEqual ( expected ) ;
} ) ;
it ( 'should render code 2' , ( ) => {
const value = '``There is a literal backtick (`) here.``' ;
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
const expected = '<article><p><code>There is a literal backtick (`) here.</code></p></article>' ;
expect ( tree ) . toEqual ( expected ) ;
} ) ;
} ) ;
2016-09-22 21:52:43 +02:00
2016-09-22 22:34:43 +02:00
describe ( 'HTML' , ( ) => {
it ( 'should render HTML as is using Markdown' , ( ) => {
const value = `
2016-09-22 21:52:43 +02:00
# Title
2016-09-22 17:36:02 +02:00
2016-09-22 21:52:43 +02:00
< dl >
< dt > Test HTML content < / d t >
< dd > Testing HTML in Markdown < / d d >
< / d l >
2016-09-22 17:36:02 +02:00
` ;
2016-09-22 22:34:43 +02:00
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
expect ( tree ) . toMatchSnapshot ( ) ;
} ) ;
} ) ;
2016-09-22 17:36:02 +02:00
} ) ;
2016-09-22 22:34:43 +02:00
describe ( 'custom elements' , ( ) => {
it ( 'should support custom syntax' , ( ) => {
const value = '' ;
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { markdownSyntax }
/ >
) ;
const tree = component . html ( ) ;
expect ( tree ) . toMatchSnapshot ( ) ;
} ) ;
2016-09-22 17:36:02 +02:00
} ) ;
2016-09-22 22:34:43 +02:00
describe ( 'HTML rendering' , ( ) => {
it ( 'should render HTML' , ( ) => {
const value = '<p class="test class">Paragraph with <em>inline</em> element</p>' ;
const component = shallow (
< MarkitupReactRenderer
value = { value }
syntax = { htmlSyntax }
/ >
) ;
const tree = component . html ( ) ;
expect ( tree ) . toMatchSnapshot ( ) ;
} ) ;
2016-09-22 17:36:02 +02:00
} ) ;
} ) ;