-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathbatching-test.js
112 lines (92 loc) · 2.27 KB
/
batching-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { jsdom } from 'jsdom'
import Alt from '../'
import React from 'react'
import { assert } from 'chai'
import sinon from 'sinon'
import TestUtils from 'react-addons-test-utils'
import ReactDom from 'react-dom'
const Actions = {
buttonClick() {
setTimeout(() => {
this.switchComponent()
}, 10)
return null
},
switchComponent() {
return null
},
uhoh() {
return null
}
}
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.defaultView
},
afterEach() {
delete global.document
delete global.window
},
'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: ReactDom.unstable_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()
},
}
}