Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleans up warnings when testing #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"jest": "~19.0.1",
"publish": "^0.6.0",
"react": "^15.5.4",
"react-addons-test-utils": "~15.5.1",
"react-dom": "^15.5.4",
"webpack": "^2.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/actions_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import {fromJS} from 'immutable'
import TestUtils from 'react-addons-test-utils'
import TestUtils from 'react-dom/test-utils'
import Tide from 'base'
import TideComponent from 'component'
import Actions, {initActions} from 'actions'
Expand Down
57 changes: 24 additions & 33 deletions test/component_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/no-multi-comp, react/display-name */
import React from 'react'
import TestUtils from 'react-addons-test-utils'
import TestUtils from 'react-dom/test-utils'
import PropTypes from 'prop-types'
import Immutable from 'immutable'

Expand All @@ -14,27 +14,26 @@ describe('Component', function() {
tideInstance = new Tide()
})

const createComponent = (render) => (props) => {
return React.createElement(React.createClass({
const createComponent = (render) => (propsToInject) => {
class C extends React.Component {
render() {
this.props = {...this.props, ...props}
this.props = {...this.props, ...propsToInject}
render.apply(this)
return null
}
}))
}
return React.createElement(C)
}
describe('Context', function() {
it('passes down the given tide instance in the context', function() {
const Child = React.createClass({
contextTypes: {
tide: PropTypes.object
},
class Child extends React.Component {
static contextTypes = {tide: PropTypes.object}

render() {
expect(this.context.tide).toBe(tideInstance)
return null
}
})
}

const tree = React.createElement(Component, {tide: tideInstance},
({tide, ...props}) => React.createElement('div', props,
Expand All @@ -49,16 +48,14 @@ describe('Component', function() {
it(
'uses tide from context instead of props when directly nesting multiple components',
function() {
const Child = React.createClass({
contextTypes: {
tide: PropTypes.object
},
class Child extends React.Component {
static contextTypes = {tide: PropTypes.object}

render() {
expect(this.context.tide).toBe(tideInstance)
return null
}
})
}

const tree = React.createElement(Component, {tide: tideInstance},
(props) => React.createElement(Component, {},
Expand Down Expand Up @@ -236,28 +233,24 @@ describe('Component', function() {
it('does not re-render if something outside of the listened state updates', function(done) {
const childRenderSpy = jest.fn()

const Child = React.createClass({
render() {
childRenderSpy()
return null
}
})
function Child () {
childRenderSpy()
return null
}

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {foo: 'foo'}
},
class Parent extends React.Component {
state = {foo: 'foo'}

componentDidMount() {
parentSetState = this.setState.bind(this)
},
}

render() {
const child = React.createElement(Child, this.state)
return React.createElement(Component, {tide: tideInstance}, () => child)
}
})
}

TestUtils.renderIntoDocument(React.createElement(Parent))

Expand All @@ -274,19 +267,17 @@ describe('Component', function() {
tideInstance.setState(Immutable.Map({foo: 'foo', bar: 'bar'}))

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {foo: 'foo'}
},
class Parent extends React.Component {
state = {foo: 'foo'}

componentDidMount() {
parentSetState = this.setState.bind(this)
},
}

render() {
return React.createElement(Component, {tide: tideInstance, impure: true}, Child)
}
})
}

expect(spy).toHaveBeenCalledTimes(0)
TestUtils.renderIntoDocument(React.createElement(Parent))
Expand Down
86 changes: 23 additions & 63 deletions test/wrap_test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/* eslint-disable react/no-multi-comp */
import React from 'react'
import Immutable from 'immutable'
import TestUtils from 'react-addons-test-utils'
import TestUtils from 'react-dom/test-utils'

import Tide from 'base'
import wrap from 'wrap'

let tideInstance

describe('wrap', function() {
let parentSetState
beforeEach(function() {
tideInstance = new Tide()
parentSetState = null
})

function createWrappedComponent(tideInstance, renderSpy, tideProps = {}, mappers) {
Expand All @@ -24,6 +26,18 @@ describe('wrap', function() {
return wrap(Child, {tide: tideInstance, ...tideProps}, mappers)
}

function getParent(Wrapped) {
return class Parent extends React.Component {
state = {childProp: 'bar'}
componentDidMount() {
parentSetState = this.setState.bind(this)
}
render() {
return React.createElement(Wrapped, this.state)
}
}
}

it('wraps the given component class with a tide component', function() {
const spy = jest.fn()

Expand Down Expand Up @@ -65,27 +79,13 @@ describe('wrap', function() {
const Wrapped = createWrappedComponent(tideInstance, function() {
return spy(this.props.childProp)
})

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {childProp: 'foo'}
},

componentDidMount() {
parentSetState = this.setState.bind(this)
},

render() {
return React.createElement(Wrapped, this.state)
}
})
const Parent = getParent(Wrapped)

TestUtils.renderIntoDocument(React.createElement(Parent))

expect(spy).toHaveBeenCalledWith('foo')
parentSetState({childProp: 'bar'})
expect(spy).toHaveBeenCalledWith('bar')
parentSetState({childProp: 'foo'})
expect(spy).toHaveBeenCalledWith('foo')
})

it('does not re-render the child when given the same props', function(done) {
Expand All @@ -94,26 +94,12 @@ describe('wrap', function() {
const Wrapped = createWrappedComponent(tideInstance, function() {
return spy(this.props.childProp)
})

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {childProp: 'foo'}
},

componentDidMount() {
parentSetState = this.setState.bind(this)
},

render() {
return React.createElement(Wrapped, this.state)
}
})
const Parent = getParent(Wrapped)

TestUtils.renderIntoDocument(React.createElement(Parent))

expect(spy).toHaveBeenCalledTimes(1)
parentSetState({childProp: 'foo'}, () => {
parentSetState({childProp: 'bar'}, () => {
expect(spy).toHaveBeenCalledTimes(1)
done()
})
Expand All @@ -126,21 +112,8 @@ describe('wrap', function() {
return spy(this.props.childProp)
}
, {impure: true})
const Parent = getParent(Wrapped)

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {childProp: 'foo'}
},

componentDidMount() {
parentSetState = this.setState.bind(this)
},

render() {
return React.createElement(Wrapped, this.state)
}
})
TestUtils.renderIntoDocument(React.createElement(Parent))
expect(spy).toHaveBeenCalledTimes(1)
parentSetState({childProp: 'foo'}, () => {
Expand All @@ -159,21 +132,7 @@ describe('wrap', function() {
}, {foo: 'foo', bar: 'bar'}, {
foo: (val) => 'mapped_' + val,
})

let parentSetState = null
const Parent = React.createClass({
getInitialState() {
return {childProp: 'bar'}
},

componentDidMount() {
parentSetState = this.setState.bind(this)
},

render() {
return React.createElement(Wrapped, this.state)
}
})
const Parent = getParent(Wrapped)

TestUtils.renderIntoDocument(React.createElement(Parent))

Expand All @@ -195,3 +154,4 @@ describe('wrap', function() {
})
})
})