-
Notifications
You must be signed in to change notification settings - Fork 321
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
f350a99
commit cd48286
Showing
1 changed file
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { jsdom } from 'jsdom' | ||
import Alt from '../' | ||
import React from 'react/addons' | ||
import { assert } from 'chai' | ||
import sinon from 'sinon' | ||
|
||
const { TestUtils } = React.addons | ||
|
||
const Actions = { | ||
buttonClick() { | ||
setTimeout(() => { | ||
this.actions.switchComponent() | ||
}, 10) | ||
}, | ||
|
||
switchComponent() { | ||
this.dispatch() | ||
}, | ||
|
||
uhoh() { | ||
this.dispatch() | ||
} | ||
} | ||
|
||
function Store(actions) { | ||
this.active = false | ||
|
||
this.bindAction(actions.switchComponent, () => { | ||
this.active = true | ||
}) | ||
} | ||
|
||
class ComponentA extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
|
||
this.state = props.alt.stores.store.getState() | ||
} | ||
|
||
componentWillMount() { | ||
this.props.alt.stores.store.listen(state => this.setState(state)) | ||
} | ||
|
||
render() { | ||
if (this.state.active) { | ||
return <ComponentB alt={this.props.alt} callback={this.props.callback} /> | ||
} else { | ||
return <div /> | ||
} | ||
} | ||
} | ||
|
||
class ComponentB extends React.Component { | ||
componentWillMount() { | ||
let error = null | ||
try { | ||
this.props.alt.actions.actions.uhoh() | ||
} catch (err) { | ||
error = err | ||
} finally { | ||
this.props.callback(error) | ||
} | ||
} | ||
|
||
render() { | ||
return <div /> | ||
} | ||
} | ||
|
||
export default { | ||
'Batching dispatcher': { | ||
beforeEach() { | ||
global.document = jsdom('<!doctype html><html><body></body></html>') | ||
global.window = global.document.parentWindow | ||
global.navigator = global.window.navigator | ||
}, | ||
|
||
afterEach() { | ||
delete global.document | ||
delete global.window | ||
delete global.navigator | ||
}, | ||
|
||
'does not batch'(done) { | ||
const alt = new Alt() | ||
alt.addActions('actions', Actions) | ||
alt.addStore('store', Store, alt.actions.actions) | ||
|
||
function test(err) { | ||
assert.match(err, /dispatch in the middle of a dispatch/) | ||
done() | ||
} | ||
|
||
TestUtils.renderIntoDocument(<ComponentA alt={alt} callback={test} />) | ||
alt.actions.actions.buttonClick() | ||
}, | ||
|
||
'allows batching'(done) { | ||
const alt = new Alt({ batchingFunction: React.addons.batchedUpdates }) | ||
alt.addActions('actions', Actions) | ||
alt.addStore('store', Store, alt.actions.actions) | ||
|
||
function test(err) { | ||
assert.isNull(err) | ||
done() | ||
} | ||
|
||
TestUtils.renderIntoDocument(<ComponentA alt={alt} callback={test} />) | ||
alt.actions.actions.buttonClick() | ||
}, | ||
} | ||
} |