forked from MithrilJS/mithril.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmithril.redraw.js
220 lines (185 loc) · 4.84 KB
/
mithril.redraw.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
describe("m.redraw()", function () {
"use strict"
beforeEach(function () {
mock.requestAnimationFrame.$resolve()
})
it("exists", function () {
expect(m.redraw).to.be.a("function")
})
it("correctly renders a property if the controller value changes", function () { // eslint-disable-line
var ctx
var root = mock.document.createElement("div")
m.mount(root, {
controller: function () { ctx = this }, // eslint-disable-line
view: function (ctrl) { return ctrl.value }
})
mock.requestAnimationFrame.$resolve()
var valueBefore = root.childNodes[0].nodeValue
ctx.value = "foo"
m.redraw()
mock.requestAnimationFrame.$resolve()
expect(valueBefore).to.equal("")
expect(root.childNodes[0].nodeValue).to.equal("foo")
})
it("runs unnecessary redraws asynchronously", function () {
var root = mock.document.createElement("div")
var view = sinon.spy()
m.mount(root, {
controller: function () {},
view: view
})
mock.requestAnimationFrame.$resolve() // teardown
m.redraw()
// These should run asynchronously
m.redraw()
m.redraw()
m.redraw()
mock.requestAnimationFrame.$resolve() // teardown
expect(view).to.be.calledThrice
})
it("runs unnecessary forced redraws asynchronously", function () {
var root = mock.document.createElement("div")
var view = sinon.spy()
m.mount(root, {
controller: function () {},
view: view
})
mock.requestAnimationFrame.$resolve() // teardown
m.redraw(true)
// These should run asynchronously
m.redraw(true)
m.redraw(true)
m.redraw(true)
mock.requestAnimationFrame.$resolve() // teardown
expect(view).to.have.callCount(5)
})
context("m.redraw.strategy()", function () {
// Use this instead of m.route() unless you have to call m.route and do
// something else in the same frame.
function route() {
var res = m.route.apply(null, arguments)
mock.requestAnimationFrame.$resolve()
return res
}
// Little helper utility
function noop() {}
// Use this if all you need to do is render a view (i.e. a pure
// component).
function pure(view) {
return {
controller: noop,
view: view
}
}
// Use these instead of `it` and `xit` in this set of tests if you need
// a root element.
var dit = makeIt(it)
// Wraps the `it` function for dependency injection that doesn't require
// `this`
/* eslint-disable no-invalid-this */
function makeIt(it) {
return function (name, callback) {
return it(name, function () {
var args = [this.root]
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i])
}
callback.apply(null, args)
})
}
}
beforeEach(function () {
mock.requestAnimationFrame.$resolve()
mock.location.search = "?"
m.route.mode = "search"
this.root = mock.document.createElement("div")
})
afterEach(function () {
m.mount(this.root, null)
})
/* eslint-enable no-invalid-this */
it("exists", function () {
expect(m.redraw.strategy).to.be.a("function")
})
dit("works with \"all\"", function (root) {
var strategy
route(root, "/foo1", {
"/foo1": {
controller: function () {
strategy = m.redraw.strategy()
m.redraw.strategy("none")
},
view: function () {
return m("div")
}
}
})
expect(strategy).to.equal("all")
expect(root.childNodes).to.be.empty
})
dit("works with \"redraw\"", function (root) {
var count = 0
var strategy
function config(el, init) {
if (!init) count++
}
route(root, "/foo1", {
"/foo1": pure(function () {
return m("div", {config: config})
}),
"/bar1": {
controller: function () {
strategy = m.redraw.strategy()
m.redraw.strategy("redraw")
},
view: function () {
return m("div", {config: config})
}
}
})
route("/bar1")
expect(strategy).to.equal("all")
expect(count).to.equal(1)
})
dit("works with \"diff\"", function (root) {
var strategy
m.route(root, "/foo1", {
"/foo1": {
controller: function () { this.number = 1 },
view: function (ctrl) {
return m("div", {
onclick: function () {
strategy = m.redraw.strategy()
ctrl.number++
m.redraw.strategy("none")
}
}, ctrl.number)
}
}
})
root.childNodes[0].onclick({})
mock.requestAnimationFrame.$resolve()
expect(strategy).to.equal("diff")
expect(root.childNodes[0].childNodes[0].nodeValue).to.equal("1")
})
dit("recreates the component when \"all\"", function (root) {
var count = 0
function config(el, init) {
if (!init) count++
}
m.route(root, "/foo1", {
"/foo1": pure(function () {
return m("div", {
config: config,
onclick: function () {
m.redraw.strategy("all")
}
})
})
})
root.childNodes[0].onclick({})
mock.requestAnimationFrame.$resolve()
expect(count).to.equal(2)
})
})
})