Skip to content

Commit

Permalink
changed some things
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Sep 11, 2015
1 parent ae9a776 commit 5442488
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ This is a super friendly testing framework for React, inspired by express middle

~~~js
import Test from 'legit-tests'
import Find from 'legit-tests/middleware/find'
import SetState from 'legit-tests/middleware/setState'
import {SetState, Find} from '../src/middleware'

Test(<TestComponent/>)
.use(SetState, {test: 'test'})
.use(Find, 'div')
.end((component, helpers) => {
expect(helpers.div[0].props.children).to.be.equal('test')
.test(function() {
expect(this.helpers.div.props.children).to.be.equal('test')
})
~~~
`Find` is a piece of included middleware that takes the component instance and finds a tag or class name for you. I'll explain that below.
Expand All @@ -39,8 +38,8 @@ export default function setState(state){
}
~~~

##End
##test

The `.end` function will be given the component instance and the helpers array so that you can use arrow functions as courtesy. If you use a regular function, the context of this will be the same as the middleware. You can access the component and helpers on `this`.
The `.test` function will be given the component instance and the helpers array. You can't use an arrow function.

You can see more examples in the tests directory.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "legit-tests",
"version": "0.0.1",
"version": "0.1.0",
"description": "a chainable testing library for React",
"main": "lib/legit-tests.js",
"scripts": {
Expand Down
13 changes: 9 additions & 4 deletions src/legit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ class Test {
}

callMiddleware(){
for (let middleware of this.middleware){
middleware.call()
for (let i in this.middleware){
this.middleware[i].call()
}
this.middleware = []
}

end(callback) {
test(callback) {
this.callMiddleware()
callback.call(this, this.component, this.helpers)
callback.call({
middleware: this.middleware,
helpers: this.helpers
})
return this
}

}
Expand Down
7 changes: 7 additions & 0 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Find from './middleware/find'
import SetState from './middleware/setState'

export default {
Find,
SetState
}
16 changes: 7 additions & 9 deletions src/middleware/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ let { TestUtils } = React.addons

export default function find(selector){
let elements
if(typeof selector === 'string'){
elements = TestUtils.scryRenderedDOMComponentsWithTag(this.component, selector)
this.helpers[selector] = elements
}
else{
if(selector.class){
elements = TestUtils.scryRenderedDOMComponentsWithClass(this.component, selector.class)
this.helpers[selector.class] = elements
}
if(selector.match(/\./)){
selector = selector.replace(/\./, '')
elements = TestUtils.scryRenderedDOMComponentsWithClass(this.component, selector)
}
else elements = TestUtils.scryRenderedDOMComponentsWithTag(this.component, selector)

if(elements.length === 1) elements = elements[0]
this.helpers[selector] = elements
}
12 changes: 6 additions & 6 deletions tests/find.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { expect } from 'chai';
import TestComponent from './component'

import Test from '../src/legit-tests'
import Find from '../src/middleware/find'
import {Find} from '../src/middleware'

describe('Find middleware', () => {

it('should find div', () => {
Test(<TestComponent/>)
.use(Find, 'div') // {class: 'class'}
.end((component, helpers) => {
expect(helpers.div[0].props.children).to.be.equal(undefined)
.test(function() {
expect(this.helpers.div.props.children).to.be.equal(undefined)
})
});

it('should find p tag with class', () => {
Test(<TestComponent/>)
.use(Find, {class: 'box'}) // {class: 'class'}
.end((component, helpers) => {
expect(helpers.box[0].props.children).to.be.equal('found me!')
.use(Find, '.box') // {class: 'class'}
.test(function() {
expect(this.helpers.box.props.children).to.be.equal('found me!')
})
});

Expand Down
15 changes: 9 additions & 6 deletions tests/setState.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { expect } from 'chai';
import TestComponent from './component'

import Test from '../src/legit-tests'
import Find from '../src/middleware/find'
import SetState from '../src/middleware/setState'
import {SetState, Find} from '../src/middleware'


describe('setState middleware', () => {

it('should change state', () => {
Test(<TestComponent/>)
.use(SetState, {test: 'test'})
.use(Find, 'div') // {class: 'class'}
.end((component, helpers) => {
expect(helpers.div[0].props.children).to.be.equal('test')
.use(Find, 'div')
.test(function(component, helpers) {
expect(this.helpers.div.props.children).to.be.equal('test')
})
.use(SetState, {test: 'changed!'})
.test(function() {
expect(this.helpers.div.props.children).to.be.equal('changed!')
})

});

});

0 comments on commit 5442488

Please sign in to comment.