Skip to content

Commit

Permalink
Merge pull request #24 from vramana/newer-api
Browse files Browse the repository at this point in the history
[WIP] Refine API
  • Loading branch information
zackify committed Oct 2, 2015
2 parents 88529c9 + 288413b commit 19c6843
Show file tree
Hide file tree
Showing 24 changed files with 266 additions and 203 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stage": 0
}
9 changes: 7 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"parser": "babel-eslint",
"env": {
"es6": true
"es6": true,
"mocha": true,
"node": true
},
"ecmaFeatures": {
"blockBindings": true,
Expand All @@ -14,6 +16,9 @@
"max-len": 0,
"semi": 0,
"quotes": 0,
"semi": [2, "never"],
"no-unused-vars": 2,
"no-undef": 2,
"no-console": 0,
"no-trailing-spaces": 0,
"curly": 0,
Expand All @@ -27,7 +32,7 @@
"react/no-did-update-set-state": 1,
"react/no-multi-comp": 1,
"react/no-unknown-property": 1,
"react/prop-types": 1,
"react/prop-types": 0,
"react/react-in-jsx-scope": 1,
"react/self-closing-comp": 1,
"react/sort-comp": 1,
Expand Down
14 changes: 8 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
node_modules
legit-tests.js
dom.js
middleware.js
middleware
no-dom.js
tests.js
lib
/legit-tests.js
/dom.js
/middleware.js
/middleware
/no-dom.js
/tests.js
npm-debug.log
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,25 @@ Test(<TestComponent/>)

~~~

##mixin

Use `.mixin` if you want to add new middleware as methods to `Test`. This gives a more natural way of using middleware:

~~~js
// In this example, CustomFind middleware was added to Test by mixin
// and used if as it was a method on Test itself.

Test(<TestComponent />)
.mixin({
customFind: CustomFind
})
.customFind('cells', 'table td')
.element('cells', cells => {
expect(cells.length).to.be.equal(10)
})

~~~



You can see more examples in the tests directory.
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"description": "a chainable testing library for React",
"main": "legit-tests.js",
"scripts": {
"compile": "babel src --stage 0 --out-dir .;",
"prepublish": "babel src --stage 0 --out-dir .;",
"test": "mocha --opts ./mocha.opts; eslint ./src/ --ext .jsx,.js --global require,exports:true"
"compile": "babel src --out-dir .",
"prepublish": "babel src --out-dir .",
"lint": "eslint ./src/ ./tests/ --ext .jsx,.js --global require,exports:true",
"mocha": "mocha --opts ./mocha.opts",
"test": "npm run mocha; npm run lint"
},
"repository": {
"type": "git",
Expand All @@ -30,14 +32,16 @@
"babel-core": "^5.8.25",
"babel-eslint": "^4.1.3",
"babel-loader": "^5.3.2",
"babel-runtime": "^5.8.25",
"chai": "^3.3.0",
"eslint": "^1.5.1",
"eslint-plugin-react": "^3.4.2",
"expect": "^1.10.0",
"mocha": "^2.3.3",
"mocha-babel": "^3.0.0",
"react-hot-loader": "^1.3.0",
"sinon": "^1.17.0"
"sinon": "^1.17.0",
"webpack": "^1.12.2"
},
"dependencies": {
"jsdom": "^6.5.1",
Expand Down
12 changes: 6 additions & 6 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ function propagateToGlobal (window) {
}
}

var jsdom = require('jsdom');
var jsdom = require('jsdom')

var doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
var win = doc.defaultView;
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
var win = doc.defaultView

global.document = doc;
global.window = win;
global.document = doc
global.window = win

propagateToGlobal(win);
propagateToGlobal(win)
7 changes: 1 addition & 6 deletions src/legit-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
/* globals global */
import './dom'
import Test from './tests'

export default function TestWrapper(component, config){
return new Test(component, config)
}
export default from './tests'
8 changes: 5 additions & 3 deletions src/middleware/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default function find(selector){
}
else elements = TestUtils.scryRenderedDOMComponentsWithTag(this.instance, selector)

if(elements.length === 1) elements = elements[0]
if(!this.helpers.elements) this.helpers.elements = []
this.helpers.elements[name || selector] = elements
if (Array.isArray(elements) && elements.length === 1) {
this.elements[name || selector] = elements[0]
} else {
this.elements[name || selector] = elements
}
}
13 changes: 9 additions & 4 deletions src/middleware/simulate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from 'react'
let { TestUtils } = React.addons

export default function simulate(data){
let element = data.element ? this.helpers.elements[data.element] : this;
element = Array.isArray(element) ? element[0] : element;
export default function simulate(data) {
let element
if (data.element !== undefined ) {
element = Array.isArray(this.elements[data.element]) ?
this.elements[data.element][0] : this.elements[data.element]
} else {
throw new Error(`No element "${data.element}" is in elements`)
}
TestUtils.Simulate[data.method].call(this,
element,
data.options || null
);
)
}
6 changes: 1 addition & 5 deletions src/no-dom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
import Test from './tests'

export default function TestWrapper(component, config){
return new Test(component, config)
}
export default from './tests'
139 changes: 62 additions & 77 deletions src/tests.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,74 @@
import React from 'react/addons';
import React from 'react/addons'
let { TestUtils } = React.addons
global.React = React //expose React to tests so they can use jsx syntax when passing in components to the class
require('react/lib/ExecutionEnvironment').canUseDOM = true

import {Find, SetState, Simulate} from './middleware'
import { Find, SetState, Simulate } from './middleware'

export default class Test {
function Test(component, config) {

constructor(component, config){
this.component = component

if(config && config.shallow === true){
let shallowRenderer = TestUtils.createRenderer();
shallowRenderer.render(component);
this.instance = shallowRenderer.getRenderOutput();
}
else{
this.instance = TestUtils.renderIntoDocument(component)
}

this.helpers = {}
return this
let instance
if (config && config.shallow === true) {
const shallowRenderer = TestUtils.createRenderer()
shallowRenderer.render(component)
instance = shallowRenderer.getRenderOutput()
} else {
instance = TestUtils.renderIntoDocument(component)
}

use(callback, data){
callback.call(this, data)
return this
}

element(select, callback) {
if(!this.helpers) return

let element
if(typeof select === 'string') {
element = this.helpers.elements[select]
callback.call(this, element)
const testComponent = {
instance,
elements: {},
element(select, callback) {
let element
if (typeof select === 'string') {
element = this.elements[select]
callback.call(this, element)
return this
}

if (Array.isArray(select)) {
const args = select.map(elem => this.elements[elem])
callback.call(this, ...args)
return this
}

if (Object.keys(this.elements).length === 1) {
select.call(this, this.elements[Object.keys(this.elements)[0]])
} else {
throw new Error("There are multiple elements select one")
}
return this
},
use(callback, data) {
callback.call(this, data)
return this
},
mixin(spec) {
Object.keys(spec).forEach(key => {
this[key] = (...args) => {
spec[key].call(this, ...args)
return this
}
})
return this
},
test(callback) {
const param = {...this, ...this.elements}
callback.call(param, param)
return this
},
renderToString(callback) {
const componentString = React.renderToStaticMarkup(component)
callback.call(null, componentString)
return this
}

element = this.getFirst(this.helpers.elements)
select.call(this, element)
return this
}

test(callback) {
var params = this.params()
callback.call(params, params)
return this
}

params(){
var length = Object.keys(this.helpers).length
if(this.helpers.elements && length === 1) {
return Object.assign({}, this, this.helpers.elements)
}
return this
}

//private

getFirst(object){
for (let element in object) return object[element]
}

//Built in middleware

find(data){
Find.call(this, data)
return this
}

setState(data){
SetState.call(this, data)
return this
}

simulate(data){
Simulate.call(this, data)
return this
}

renderToString(callback){
var component = React.renderToStaticMarkup(this.component)
callback.call(null, component)
return this
}

return testComponent.mixin({
find: Find,
setState: SetState,
simulate: Simulate
})
}

export default Test
27 changes: 0 additions & 27 deletions tests/component.jsx

This file was deleted.

29 changes: 29 additions & 0 deletions tests/components/component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { Component } from 'react'
import TinyComponent from './tiny-component'

export default class TestComponent extends Component {
constructor(props, context){
super(props, context)
this.state = {}
}

render(){
return (
<body>
<div onClick={this.props.onClick}>{this.state.test}</div>
<p className="box">found me!</p>
<button
type="button"
onClick={this.props.onClick}>
Click Me
</button>
<button
type="button"
onClick={this.props.onClick}>
Click Me
</button>
<TinyComponent test="true"/>
</body>
)
}
}
2 changes: 2 additions & 0 deletions tests/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export TestComponent from './component'
export TinyComponent from './tiny-component'
7 changes: 7 additions & 0 deletions tests/components/tiny-component.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, { Component } from 'react'

export default class TinyComponent extends Component {
render() {
return <div className="foo"></div>
}
}
Loading

0 comments on commit 19c6843

Please sign in to comment.