-
Notifications
You must be signed in to change notification settings - Fork 322
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
20c8382
commit a568bfb
Showing
6 changed files
with
358 additions
and
23 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
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,109 @@ | ||
import Alt from '../dist/alt-with-runtime' | ||
import React from 'react' | ||
import ReactComponent from './helpers/ReactComponent' | ||
import connectToStores from '../utils/connectToStores' | ||
import { assert } from 'chai' | ||
|
||
const alt = new Alt(); | ||
|
||
const testActions = alt.createActions( | ||
class TestActions { | ||
updateFoo(newValue) { | ||
this.dispatch(newValue) | ||
} | ||
} | ||
) | ||
|
||
const testStore = alt.createStore( | ||
class TestStore { | ||
constructor() { | ||
this.bindAction(testActions.updateFoo, this.onChangeFoo) | ||
this.foo = 'Bar' | ||
} | ||
onChangeFoo(newValue) { | ||
this.foo = newValue | ||
} | ||
} | ||
) | ||
|
||
const BadComponentOne = React.createClass({ | ||
render() { | ||
return React.createElement('div', null, 'Bad'); | ||
} | ||
}); | ||
const BadComponentTwo = React.createClass({ | ||
statics: { | ||
getStores() { | ||
return [testStore] | ||
} | ||
}, | ||
render() { | ||
return React.createElement('div', null, 'Bad'); | ||
} | ||
}) | ||
|
||
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}`) | ||
} | ||
}) | ||
|
||
class ClassComponent extends ReactComponent { | ||
static getStores() { | ||
return [testStore] | ||
} | ||
static getPropsFromStores(props) { | ||
return testStore.getState() | ||
} | ||
render() { | ||
// Will never get called due to mocked wrapper. | ||
} | ||
} | ||
|
||
export default { | ||
'connectToStores wrapper': { | ||
|
||
'missing the static getStores() method should throw'() { | ||
assert.throws(() => connectToStores(BadComponentOne), 'expects the wrapped component to have a static getStores() method') | ||
}, | ||
|
||
'missing the static getPropsFromStores() method should throw'() { | ||
assert.throws(() => connectToStores(BadComponentTwo), 'expects the wrapped component to have a static getPropsFromStores() method') | ||
}, | ||
|
||
'createClass() component can get props from stores'() { | ||
connectToStores.createClass = React.createClass | ||
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'() { | ||
let renderCalled = false | ||
connectToStores.createClass = (spec) => { | ||
class FakeComponent extends ReactComponent {} | ||
Object.assign(FakeComponent.prototype, spec) | ||
FakeComponent.prototype.render = function() { | ||
assert.strictEqual(this.state.foo, 'Baz', 'wrapped component did not receive store changes') | ||
renderCalled = true | ||
} | ||
return ReactComponent.prepare(FakeComponent) | ||
} | ||
const WrappedComponent = connectToStores(ClassComponent) | ||
ReactComponent.test(WrappedComponent, () => { | ||
testActions.updateFoo('Baz') | ||
assert(renderCalled === true, 'render was never called') | ||
}, {delim: ' - '}) | ||
} | ||
|
||
} | ||
} |
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.