rename limiter to throttle and refactor
- don't inject raf/setTimeout since we can't really mock them w/ a good degree of timing accuracy anyways fix some unrelated tests
This commit is contained in:
parent
2af3aa27c7
commit
977239d207
15 changed files with 813 additions and 353 deletions
84
api/tests/test-throttle.js
Normal file
84
api/tests/test-throttle.js
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
"use strict"
|
||||
|
||||
var o = require("../../ospec/ospec")
|
||||
var callAsync = require("../../test-utils/callAsync")
|
||||
var throttle = require("../../api/throttle")
|
||||
|
||||
o.spec("throttle", function() {
|
||||
var FRAME_BUDGET = 1000 / 60
|
||||
var spy, throttled
|
||||
o.beforeEach(function() {
|
||||
spy = o.spy()
|
||||
throttled = throttle(spy)
|
||||
})
|
||||
|
||||
o("runs first call synchronously", function() {
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
})
|
||||
|
||||
o("throttles subsequent synchronous calls", function(done) {
|
||||
throttled()
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
|
||||
setTimeout(function() {
|
||||
o(spy.callCount).equals(2)
|
||||
|
||||
done()
|
||||
}, FRAME_BUDGET) //this delay is much higher than 16.6ms due to setTimeout clamp and other runtime costs
|
||||
})
|
||||
|
||||
o("calls after threshold", function(done) {
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
|
||||
setTimeout(function(t) {
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(2)
|
||||
|
||||
done()
|
||||
}, FRAME_BUDGET)
|
||||
|
||||
})
|
||||
|
||||
o("throttles before threshold", function(done) {
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
|
||||
callAsync(function(t) {
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
o("it only runs once per tick", function(done) {
|
||||
throttled()
|
||||
throttled()
|
||||
throttled()
|
||||
|
||||
o(spy.callCount).equals(1)
|
||||
|
||||
setTimeout(function() {
|
||||
o(spy.callCount).equals(2)
|
||||
|
||||
done()
|
||||
}, FRAME_BUDGET)
|
||||
})
|
||||
|
||||
o("it supports forcing a synchronous redraw", function() {
|
||||
throttled()
|
||||
throttled()
|
||||
throttled(true)
|
||||
|
||||
o(spy.callCount).equals(2)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue