2016-07-13 18:56:03 -07:00
|
|
|
|
|
|
|
import React from 'react'
|
|
|
|
import ReactDOM from 'react-dom/server'
|
|
|
|
import assert from 'assert'
|
|
|
|
import cheerio from 'cheerio'
|
|
|
|
import fs from 'fs'
|
|
|
|
import readMetadata from 'read-metadata'
|
|
|
|
import { Editor, Raw } from '../..'
|
|
|
|
import { resolve } from 'path'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
describe('rendering', () => {
|
|
|
|
const tests = fs.readdirSync(resolve(__dirname, './fixtures'))
|
|
|
|
|
|
|
|
for (const test of tests) {
|
2016-07-29 20:25:07 +02:00
|
|
|
if (test[0] === '.') continue
|
|
|
|
|
2016-07-13 18:56:03 -07:00
|
|
|
it(test, () => {
|
|
|
|
const dir = resolve(__dirname, './fixtures', test)
|
|
|
|
const input = readMetadata.sync(resolve(dir, 'input.yaml'))
|
|
|
|
const output = fs.readFileSync(resolve(dir, 'output.html'), 'utf8')
|
|
|
|
const props = {
|
2016-07-25 13:29:29 -07:00
|
|
|
state: Raw.deserialize(input, { terse: true }),
|
2016-07-13 18:56:03 -07:00
|
|
|
onChange: () => {},
|
|
|
|
...require(dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
const string = ReactDOM.renderToStaticMarkup(<Editor {...props} />)
|
|
|
|
const expected = cheerio
|
|
|
|
.load(output)
|
|
|
|
.html()
|
|
|
|
.trim()
|
|
|
|
.replace(/\n/gm, '')
|
|
|
|
.replace(/>\s*</g, '><')
|
|
|
|
|
|
|
|
assert.equal(clean(string), expected)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean a renderer `html` string, removing dynamic attributes.
|
|
|
|
*
|
|
|
|
* @param {String} html
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
|
|
|
|
function clean(html) {
|
|
|
|
const $ = cheerio.load(html)
|
|
|
|
|
|
|
|
$('*').each((i, el) => {
|
|
|
|
$(el).removeAttr('data-key')
|
|
|
|
$(el).removeAttr('data-offset-key')
|
|
|
|
})
|
|
|
|
|
2016-07-22 20:21:50 -07:00
|
|
|
$.root().children().removeAttr('spellcheck')
|
2016-07-22 13:39:02 -07:00
|
|
|
$.root().children().removeAttr('style')
|
2016-07-14 15:06:44 -07:00
|
|
|
|
2016-07-13 18:56:03 -07:00
|
|
|
return $.html()
|
|
|
|
}
|