Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5b11535
commit f57217b
Showing
6 changed files
with
193 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
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') | ||
}, | ||
|
||
'element mounts and unmounts'() { | ||
const div = document.createElement('div') | ||
|
||
const LegacyComponent = connectToStores(React.createClass({ | ||
statics: { | ||
getStores() { | ||
return [testStore] | ||
}, | ||
getPropsFromStores(props) { | ||
return testStore.getState() | ||
} | ||
}, | ||
render() { | ||
return React.createElement('div', null, `Foo${this.props.delim}${this.props.foo}`) | ||
} | ||
})) | ||
|
||
React.render( | ||
<LegacyComponent /> | ||
, div) | ||
|
||
React.unmountComponentAtNode(div) | ||
}, | ||
|
||
'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') | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.