Skip to content

Commit 6784ad9

Browse files
author
dphaener
committed
Merge branch 'master' into feature/store-tests
2 parents be7b191 + 2e59f50 commit 6784ad9

24 files changed

Lines changed: 316 additions & 205 deletions

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"stage": 0
3+
}

.eslintrc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"parser": "babel-eslint",
33
"env": {
4-
"es6": true
4+
"es6": true,
5+
"mocha": true,
6+
"node": true
57
},
68
"ecmaFeatures": {
79
"blockBindings": true,
@@ -14,6 +16,9 @@
1416
"max-len": 0,
1517
"semi": 0,
1618
"quotes": 0,
19+
"semi": [2, "never"],
20+
"no-unused-vars": 2,
21+
"no-undef": 2,
1722
"no-console": 0,
1823
"no-trailing-spaces": 0,
1924
"curly": 0,
@@ -27,7 +32,7 @@
2732
"react/no-did-update-set-state": 1,
2833
"react/no-multi-comp": 1,
2934
"react/no-unknown-property": 1,
30-
"react/prop-types": 1,
35+
"react/prop-types": 0,
3136
"react/react-in-jsx-scope": 1,
3237
"react/self-closing-comp": 1,
3338
"react/sort-comp": 1,

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
node_modules
22
lib
3+
/legit-tests.js
4+
/dom.js
5+
/middleware.js
6+
/middleware
7+
/no-dom.js
8+
/tests.js
9+
npm-debug.log

README.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
This is a super friendly testing library for React, inspired by express middleware, it's easily extendable. Why did I make this when you can use [React's Test Utils](https://facebook.github.io/react/docs/test-utils.html)? Because who likes typing out `scryRenderedDOMComponentsWithTag` and the other method names on there. Not only that, but setting up the render process is also a hassle.
44

5-
###0.4.0
6-
7-
In 0.4.0 there is a breaking change where `this.component` is now the component itself and the rendered instance is `this.instance`. This makes much more sense but sadly means tests need to be changed in order to update to the latest version.
8-
95
###Install
106

117
`npm install legit-tests --save`
@@ -14,6 +10,9 @@ In 0.4.0 there is a breaking change where `this.component` is now the component
1410

1511
~~~js
1612
import Test from 'legit-tests'
13+
//or
14+
import Test from 'legit-tests/no-dom' //don't include jsdom
15+
1716
import { expect } from 'chai'
1817
import sinon from 'sinon'
1918
import TestComponent from './TestComponent'
@@ -51,6 +50,9 @@ You can write middleware to do anything you repeatedly use. You can pass `.use`
5150
- `helpers` - an array you can add on to with data for the end function
5251

5352
**Example**:
53+
54+
- See `mixin` below, this syntax may soon be deprecated
55+
5456
This is the setState function used above.
5557
~~~js
5658

@@ -69,12 +71,53 @@ export default function setState(state){
6971
The `.test` function will be given the component instance and the helpers array. You can use a regular function to reference `this` or an arrow function:
7072

7173
~~~js
72-
.test(({helpers, component}) => { ... })
74+
.test(({helpers, instance}) => { ... })
7375
.test(function() {
7476
//this.instance, this.helpers
7577
})
7678
~~~
7779

80+
##element
81+
82+
Use `.element` if you're just testing an element you found with the `.find` method. The syntax is a little smaller:
83+
84+
~~~js
85+
Test(<TestComponent/>)
86+
.find('.box')
87+
.element(box => {
88+
expect(box.props.children).to.be.equal('found me!')
89+
})
90+
//or specify the element
91+
92+
.find('.box')
93+
.find('div')
94+
.element('box', box => {
95+
expect(box.props.children).to.be.equal('found me!')
96+
})
97+
98+
~~~
99+
100+
##mixin
101+
102+
Use `.mixin` if you want to add new middleware as methods to `Test`. This gives a more natural way of using middleware:
103+
104+
~~~js
105+
// In this example, CustomFind middleware was added to Test by mixin
106+
// and used if as it was a method on Test itself.
107+
108+
Test(<TestComponent />)
109+
.mixin({
110+
customFind: CustomFind
111+
})
112+
.customFind('cells', 'table td')
113+
.element('cells', cells => {
114+
expect(cells.length).to.be.equal(10)
115+
})
116+
117+
~~~
118+
119+
120+
78121
You can see more examples in the tests directory.
79122

80123
##Testing Alt Stores

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
{
22
"name": "legit-tests",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "a chainable testing library for React",
5-
"main": "lib/legit-tests.js",
5+
"main": "legit-tests.js",
66
"scripts": {
7-
"compile": "babel src --stage 0 --out-dir lib;",
8-
"prepublish": "babel src --stage 0 --out-dir lib;",
9-
"test": "mocha --opts ./mocha.opts; eslint ./src/ --ext .jsx,.js --global require,exports:true"
7+
"compile": "babel src --out-dir .",
8+
"prepublish": "babel src --out-dir .",
9+
"lint": "eslint ./src/ ./tests/ --ext .jsx,.js --global require,exports:true",
10+
"mocha": "mocha --opts ./mocha.opts",
11+
"test": "npm run mocha; npm run lint"
1012
},
1113
"repository": {
1214
"type": "git",
@@ -31,6 +33,7 @@
3133
"babel-core": "^5.8.25",
3234
"babel-eslint": "^4.1.3",
3335
"babel-loader": "^5.3.2",
36+
"babel-runtime": "^5.8.25",
3437
"chai": "^3.3.0",
3538
"eslint": "^1.5.1",
3639
"eslint-plugin-react": "^3.4.2",
@@ -39,7 +42,8 @@
3942
"mocha": "^2.3.3",
4043
"mocha-babel": "^3.0.0",
4144
"react-hot-loader": "^1.3.0",
42-
"sinon": "^1.17.0"
45+
"sinon": "^1.17.0",
46+
"webpack": "^1.12.2"
4347
},
4448
"dependencies": {
4549
"jsdom": "^6.5.1",

src/dom.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ function propagateToGlobal (window) {
88
}
99
}
1010

11-
var jsdom = require('jsdom');
11+
var jsdom = require('jsdom')
1212

13-
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>');
14-
var win = doc.defaultView;
13+
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>')
14+
var win = doc.defaultView
1515

16-
global.document = doc;
17-
global.window = win;
16+
global.document = doc
17+
global.window = win
1818

19-
propagateToGlobal(win);
19+
propagateToGlobal(win)

src/legit-tests.js

Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,2 @@
1-
/* globals global */
21
import './dom'
3-
import React from 'react/addons';
4-
let { TestUtils } = React.addons
5-
global.React = React //expose React to tests so they can use jsx syntax when passing in components to the class
6-
require('react/lib/ExecutionEnvironment').canUseDOM = true
7-
8-
import {Find, SetState, Simulate} from './middleware'
9-
10-
class Test {
11-
12-
constructor(component, config){
13-
this.component = component
14-
15-
if(config && config.shallow === true){
16-
let shallowRenderer = TestUtils.createRenderer();
17-
shallowRenderer.render(component);
18-
this.instance = shallowRenderer.getRenderOutput();
19-
}
20-
else{
21-
this.instance = TestUtils.renderIntoDocument(component)
22-
}
23-
24-
this.helpers = {}
25-
return this
26-
}
27-
28-
use(callback, data){
29-
callback.call(this, data)
30-
return this
31-
}
32-
33-
element(select, callback) {
34-
if(!this.helpers) return
35-
36-
let element
37-
if(typeof select === 'string') {
38-
element = this.helpers.elements[select]
39-
callback.call(this, element)
40-
return this
41-
}
42-
43-
element = this.getFirst(this.helpers.elements)
44-
select.call(this, element)
45-
return this
46-
}
47-
48-
test(callback) {
49-
var params = this.params()
50-
callback.call(params, params)
51-
return this
52-
}
53-
54-
params(){
55-
var length = Object.keys(this.helpers).length
56-
if(this.helpers.elements && length === 1) {
57-
return Object.assign({}, this, this.helpers.elements)
58-
}
59-
return this
60-
}
61-
62-
//private
63-
64-
getFirst(object){
65-
for (let element in object) return object[element]
66-
}
67-
68-
//Built in middleware
69-
70-
find(data){
71-
Find.call(this, data)
72-
return this
73-
}
74-
75-
setState(data){
76-
SetState.call(this, data)
77-
return this
78-
}
79-
80-
simulate(data){
81-
Simulate.call(this, data)
82-
return this
83-
}
84-
85-
renderToString(callback){
86-
var component = React.renderToStaticMarkup(this.component)
87-
callback.call(null, component)
88-
return this
89-
}
90-
91-
}
92-
93-
export default function TestWrapper(component, config){
94-
return new Test(component, config)
95-
}
2+
export default from './tests'

src/middleware/find.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export default function find(selector){
1212
}
1313
else elements = TestUtils.scryRenderedDOMComponentsWithTag(this.instance, selector)
1414

15-
if(elements.length === 1) elements = elements[0]
16-
if(!this.helpers.elements) this.helpers.elements = []
17-
this.helpers.elements[name || selector] = elements
15+
if (Array.isArray(elements) && elements.length === 1) {
16+
this.elements[name || selector] = elements[0]
17+
} else {
18+
this.elements[name || selector] = elements
19+
}
1820
}

src/middleware/simulate.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import React from 'react'
22
let { TestUtils } = React.addons
33

4-
export default function simulate(data){
5-
let element = data.element ? this.helpers.elements[data.element] : this;
6-
element = Array.isArray(element) ? element[0] : element;
4+
export default function simulate(data) {
5+
let element
6+
if (data.element !== undefined ) {
7+
element = Array.isArray(this.elements[data.element]) ?
8+
this.elements[data.element][0] : this.elements[data.element]
9+
} else {
10+
throw new Error(`No element "${data.element}" is in elements`)
11+
}
712
TestUtils.Simulate[data.method].call(this,
813
element,
914
data.options || null
10-
);
15+
)
1116
}

src/no-dom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default from './tests'

0 commit comments

Comments
 (0)