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

Commit

Permalink
Add a suite of initial value tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tgecho committed Jan 30, 2016
1 parent 2a97f16 commit 96fb252
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
29 changes: 29 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const webpackConfig = require('./webpack.config')

module.exports = function(config) {
config.set({
frameworks: ['jasmine'],

plugins: [
require('karma-jasmine'),
require('karma-webpack'),
require('karma-phantomjs-launcher'),
],

files: [
'test/*_tests.js',
'test/**/*_tests.js'
],

preprocessors: {
'test/*_tests.js': ['webpack'],
'test/**/*_tests.js': ['webpack']
},

webpack: webpackConfig,

webpackMiddleware: {
noInfo: true
},
});
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A React component for ProseMirror",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "karma start --single-run --browsers PhantomJS",
"watch-test": "karma start",
"demo": "webpack-dev-server --inline --host 0.0.0.0",
"build-demo": "webpack",
"dist": "babel index.js -d dist",
Expand Down Expand Up @@ -43,7 +44,14 @@
"babel-preset-stage-2": "^6.1.2",
"eslint": "^1.9.0",
"eslint-plugin-react": "^3.8.0",
"jasmine-core": "^2.4.1",
"json-loader": "^0.5.3",
"karma": "^0.13.19",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^1.0.0",
"karma-webpack": "^1.7.0",
"phantomjs-prebuilt": "^2.1.3",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.4",
"webpack-dev-server": "^1.12.1"
}
Expand Down
116 changes: 116 additions & 0 deletions test/initial_values_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react'
import RTest from 'react-addons-test-utils'

import ProseMirror from '../index'
import 'prosemirror/dist/markdown'


function pmForProps(props={}) {
/*
Renders the ProseMirror component with the props you pass it and returns the
generated instance of the ProseMirror editor for inspection.
Example:
const pm = pmForProps()
pm.getContent('html')
*/
return RTest.renderIntoDocument(<ProseMirror {...props} />).pm
}

function testValues(pm, values) {
/*
Assert that the content of the ProseMirror editor matches with your
expectations across one or more formats.
Example:
testValues(pm, {text: 'Hello World!'})
*/
for (const key in values) {
expect(pm.getContent(key)).toEqual(values[key])
}
}

function valueProps(value) {
/*
Return variations on value for each way to set the initial value of the
ProseMirror component.
*/
return [
{value: value},
{valueLink: {value: value, requestChange(){}}},
{defaultValue: value},
]
}


;['text', 'markdown', 'html'].forEach(inputFormat => {

/*
Checking to ensure blank/undefined values all end up resulting in a empty document.
*/

;['', undefined, null].forEach(value => {
valueProps(value).forEach(valueProps => {

const pmProps = {
...valueProps,
options: {docFormat: inputFormat},
}

it(`Blank value: ${JSON.stringify(pmProps)}`, () => {
const pm = pmForProps(pmProps)
testValues(pm, {
'text': '',
'markdown': '',
'html': '<p></p>',
'json': {type: 'doc', content:[{type: 'paragraph'}]},
})
})
})
})

/*
Checking to ensure blank/undefined values all end up resulting in a empty document.
*/

;['a string'].forEach(value => {
valueProps(value).forEach(valueProps => {

const pmProps = {
...valueProps,
options: {docFormat: inputFormat},
}

it(`String value: ${JSON.stringify(pmProps)}`, () => {
const pm = pmForProps(pmProps)
testValues(pm, {
'text': `${value}`,
'markdown': `${value}`,
'html': `<p>${value}</p>`,
'json': {type: 'doc', content:[{type: 'paragraph', content: [{type: 'text', text: value.toString()}]}]},
})
})
})
})
})

/*
Invalid values will be passed into ProseMirror and may result in an error.
*/

;['text', 'markdown'].forEach(inputFormat => {

;[0, 1].forEach(value => {
valueProps(value).forEach(valueProps => {

const pmProps = {
...valueProps,
options: {docFormat: inputFormat},
}

it(`Invalid value: ${JSON.stringify(pmProps)}`, () => {
expect(() => pmForProps(pmProps)).toThrow()
})
})
})
})
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
module: {
loaders: [
{test: /\.json?$/, loader: 'json'},
{test: /\.jsx?$/, loader: 'babel'}
{test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel'}
]
},
resolve: {
Expand Down

0 comments on commit 96fb252

Please sign in to comment.