|
209 | 209 |
|
210 | 210 | }); |
211 | 211 | }); |
| 212 | + |
| 213 | + suite('enqueueDebouncer & flush', function() { |
| 214 | + function testEnqueue(shouldFlush, done) { |
| 215 | + // Longer-running debouncer |
| 216 | + const timeoutCallback = sinon.spy(() => actualCallbacks.push(timeoutCallback)); |
| 217 | + Polymer.enqueueDebouncer(Polymer.Debouncer.debounce(null, Polymer.Async.timeOut, timeoutCallback)); |
| 218 | + // Set of short-running debouncers enqueued in the middle of first set |
| 219 | + const nestedCallbacks = new Array(150).fill().map((_, i) => sinon.spy(() => |
| 220 | + actualCallbacks.push(nestedCallbacks[i]))); |
| 221 | + // First set of short-running debouncers |
| 222 | + const microtaskCallbacks = new Array(150).fill().map((_, i) => sinon.spy(() => { |
| 223 | + actualCallbacks.push(microtaskCallbacks[i]); |
| 224 | + if (i === 125) { |
| 225 | + nestedCallbacks.forEach(cb => |
| 226 | + Polymer.enqueueDebouncer(Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb))); |
| 227 | + } |
| 228 | + })); |
| 229 | + microtaskCallbacks.forEach(cb => |
| 230 | + Polymer.enqueueDebouncer(Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb))); |
| 231 | + // Expect short before long |
| 232 | + let expectedCallbacks; |
| 233 | + const actualCallbacks = []; |
| 234 | + const verify = () => { |
| 235 | + actualCallbacks.forEach(cb => assert.isTrue(cb.calledOnce)); |
| 236 | + assert.deepEqual(expectedCallbacks, actualCallbacks); |
| 237 | + done(); |
| 238 | + }; |
| 239 | + if (shouldFlush) { |
| 240 | + expectedCallbacks = [timeoutCallback, ...microtaskCallbacks, ...nestedCallbacks]; |
| 241 | + Polymer.flush(); |
| 242 | + // When flushing, order is order of enqueing |
| 243 | + verify(); |
| 244 | + } else { |
| 245 | + expectedCallbacks = [...microtaskCallbacks, ...nestedCallbacks, timeoutCallback]; |
| 246 | + Polymer.Async.timeOut.run(verify); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + test('non-flushed', function(done) { |
| 251 | + testEnqueue(false, done); |
| 252 | + }); |
| 253 | + |
| 254 | + test('flushed', function(done) { |
| 255 | + testEnqueue(true, done); |
| 256 | + }); |
| 257 | + |
| 258 | + }); |
212 | 259 | </script> |
213 | 260 | </body> |
214 | 261 | </html> |
0 commit comments