Skip to content

Commit

Permalink
Fix connectToStores tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed Apr 18, 2015
1 parent b9a2f51 commit df33e4b
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 119 deletions.
114 changes: 114 additions & 0 deletions test/connect-to-stores-test.js
@@ -0,0 +1,114 @@
import { jsdom } from 'jsdom'
import Alt from '../dist/alt-with-runtime'
import React from 'react/addons'
import connectToStores from '../utils/connectToStores'
import { assert } from 'chai'

const { TestUtils } = React.addons

const alt = new Alt()

const testActions = alt.generateActions('updateFoo')

const testStore = alt.createStore(
class TestStore {
constructor() {
this.bindAction(testActions.updateFoo, this.onChangeFoo)
this.foo = 'Bar'
}
onChangeFoo(newValue) {
this.foo = newValue
}
}
)

export default {
'connectToStores wrapper': {
beforeEach() {
global.document = jsdom('<!doctype html><html><body></body></html>')
global.window = global.document.parentWindow
global.navigator = global.window.navigator
require('react/lib/ExecutionEnvironment').canUseDOM = true

alt.recycle()
},

afterEach() {
delete global.document
delete global.window
delete global.navigator
},

'missing the static getStores() method should throw'() {
const BadComponentOne = React.createClass({
render() {
return React.createElement('div', null, 'Bad')
}
})

assert.throws(() => connectToStores(BadComponentOne), 'expects the wrapped component to have a static getStores() method')
},

'missing the static getPropsFromStores() method should throw'() {
const BadComponentTwo = React.createClass({
statics: {
getStores() {
return [testStore]
}
},
render() {
return React.createElement('div', null, 'Bad')
}
})

assert.throws(() => connectToStores(BadComponentTwo), 'expects the wrapped component to have a static getPropsFromStores() method')
},

'createClass() component can get props from stores'() {
const LegacyComponent = React.createClass({
statics: {
getStores() {
return [testStore]
},
getPropsFromStores(props) {
return testStore.getState()
}
},
render() {
return React.createElement('div', null, `Foo${this.props.delim}${this.props.foo}`)
}
})

const WrappedComponent = connectToStores(LegacyComponent)
const element = React.createElement(WrappedComponent, {delim: ': '})
const output = React.renderToStaticMarkup(element)
assert.include(output, 'Foo: Bar')
},

'ES6 class component responds to store events'() {
class ClassComponent extends React.Component {
static getStores() {
return [testStore]
}
static getPropsFromStores(props) {
return testStore.getState()
}
render() {
return <span foo={this.props.foo} />
}
}

const WrappedComponent = connectToStores(ClassComponent)

const node = TestUtils.renderIntoDocument(
<WrappedComponent />
)

testActions.updateFoo('Baz')

const span = TestUtils.findRenderedDOMComponentWithTag(node, 'span')

assert(span.props.foo === 'Baz')
}
}
}
109 changes: 0 additions & 109 deletions test/connect-to-stores.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/store-listener-component-test.js
@@ -1,9 +1,9 @@
import { jsdom } from 'jsdom'
import Alt from '../dist/alt-with-runtime'
import React from 'react/addons'
import AltContainer from '../components/AltContainer'
import withAltContext from '../utils/withAltContext'
import { assert } from 'chai'
import { jsdom } from 'jsdom'
import sinon from 'sinon'

const { TestUtils } = React.addons
Expand Down
11 changes: 2 additions & 9 deletions utils/connectToStores.js
Expand Up @@ -47,8 +47,6 @@
var React = require('react')
var assign = require('object-assign')

var createClass = React.createClass

function connectToStores(Component) {

// Check for required static methods.
Expand All @@ -60,10 +58,10 @@ function connectToStores(Component) {
}

// Cache stores.
const stores = Component.getStores()
var stores = Component.getStores()

// Wrapper Component.
const StoreConnection = createClass({
var StoreConnection = React.createClass({
getInitialState: function () {
return Component.getPropsFromStores(this.props)
},
Expand Down Expand Up @@ -95,9 +93,4 @@ function connectToStores(Component) {
return StoreConnection
}

/**
* Expose createClass to facilitate testing.
*/
connectToStores.createClass = createClass

module.exports = connectToStores

0 comments on commit df33e4b

Please sign in to comment.