|
34 | 34 | Polymer({is: 'x-basic'});
|
35 | 35 | });
|
36 | 36 |
|
| 37 | + suite('enqueueDebouncer & flush', function() { |
| 38 | + |
| 39 | + // NOTE: This is a regression test; the bug it fixed only occurred if the |
| 40 | + // debouncer was flushed before any microtasks run, hence it should be |
| 41 | + // first in this file |
| 42 | + test('re-enqueue canceled debouncer', function() { |
| 43 | + const cb = sinon.spy(); |
| 44 | + let db; |
| 45 | + db = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb); |
| 46 | + Polymer.enqueueDebouncer(db); |
| 47 | + db.cancel(); |
| 48 | + assert.equal(db.isActive(), false); |
| 49 | + assert.equal(cb.callCount, 0); |
| 50 | + db = Polymer.Debouncer.debounce(db, Polymer.Async.microTask, cb); |
| 51 | + Polymer.enqueueDebouncer(db); |
| 52 | + Polymer.flush(); |
| 53 | + assert.isTrue(cb.calledOnce); |
| 54 | + }); |
| 55 | + |
| 56 | + test('flushDebouncers from enqueued debouncer', function(done) { |
| 57 | + const cb = sinon.spy(() => Polymer.flush()); |
| 58 | + let db = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb); |
| 59 | + Polymer.enqueueDebouncer(db); |
| 60 | + setTimeout(() => { |
| 61 | + assert.isTrue(cb.calledOnce); |
| 62 | + done(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + const testEnqueue = (shouldFlush, done) => { |
| 67 | + const actualOrder = []; |
| 68 | + const enqueue = (type, {db, cb} = {}) => { |
| 69 | + cb = cb || (() => actualOrder.push(cb)); |
| 70 | + db = Polymer.Debouncer.debounce(db, type, cb); |
| 71 | + Polymer.enqueueDebouncer(db); |
| 72 | + return {db, cb}; |
| 73 | + }; |
| 74 | + const db1 = enqueue(Polymer.Async.microTask); |
| 75 | + const db2 = enqueue(Polymer.Async.microTask); |
| 76 | + const db3 = enqueue(Polymer.Async.timeOut); |
| 77 | + const db4 = enqueue(Polymer.Async.microTask); |
| 78 | + enqueue(Polymer.Async.microTask, db2); |
| 79 | + enqueue(Polymer.Async.microTask, db1); |
| 80 | + if (shouldFlush) { |
| 81 | + Polymer.flush(); |
| 82 | + assert.deepEqual(actualOrder, [db1.cb, db2.cb, db3.cb, db4.cb]); |
| 83 | + done(); |
| 84 | + } else { |
| 85 | + Polymer.Async.timeOut.run(() => { |
| 86 | + assert.deepEqual(actualOrder, [db4.cb, db2.cb, db1.cb, db3.cb]); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + test('non-flushed', function(done) { |
| 93 | + testEnqueue(false, done); |
| 94 | + }); |
| 95 | + |
| 96 | + test('flushed', function(done) { |
| 97 | + testEnqueue(true, done); |
| 98 | + }); |
| 99 | + |
| 100 | + test('reentrant flush', function() { |
| 101 | + const cb2 = sinon.spy(); |
| 102 | + let db2; |
| 103 | + const cb1 = sinon.spy(() => { |
| 104 | + Polymer.flush(); |
| 105 | + db2 = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb2); |
| 106 | + Polymer.enqueueDebouncer(db2); |
| 107 | + }); |
| 108 | + const db1 = Polymer.Debouncer.debounce(null, Polymer.Async.microTask, cb1); |
| 109 | + Polymer.enqueueDebouncer(db1); |
| 110 | + Polymer.flush(); |
| 111 | + assert.isTrue(cb1.calledOnce); |
| 112 | + assert.isTrue(cb2.calledOnce); |
| 113 | + }); |
| 114 | + |
| 115 | + }); |
| 116 | + |
37 | 117 | suite('debounce', function() {
|
38 | 118 | var element;
|
39 | 119 |
|
|
208 | 288 | });
|
209 | 289 |
|
210 | 290 | });
|
211 |
| - }); |
212 |
| - |
213 |
| - suite('enqueueDebouncer & flush', function() { |
214 |
| - |
215 |
| - const testEnqueue = (shouldFlush, done) => { |
216 |
| - const actualOrder = []; |
217 |
| - const enqueue = (type, {db, cb} = {}) => { |
218 |
| - cb = cb || (() => actualOrder.push(cb)); |
219 |
| - db = Polymer.Debouncer.debounce(db, type, cb); |
220 |
| - Polymer.enqueueDebouncer(db); |
221 |
| - return {db, cb}; |
222 |
| - }; |
223 |
| - const db1 = enqueue(Polymer.Async.microTask); |
224 |
| - const db2 = enqueue(Polymer.Async.microTask); |
225 |
| - const db3 = enqueue(Polymer.Async.timeOut); |
226 |
| - const db4 = enqueue(Polymer.Async.microTask); |
227 |
| - enqueue(Polymer.Async.microTask, db2); |
228 |
| - enqueue(Polymer.Async.microTask, db1); |
229 |
| - if (shouldFlush) { |
230 |
| - Polymer.flush(); |
231 |
| - assert.deepEqual(actualOrder, [db1.cb, db2.cb, db3.cb, db4.cb]); |
232 |
| - done(); |
233 |
| - } else { |
234 |
| - Polymer.Async.timeOut.run(() => { |
235 |
| - assert.deepEqual(actualOrder, [db4.cb, db2.cb, db1.cb, db3.cb]); |
236 |
| - done(); |
237 |
| - }); |
238 |
| - } |
239 |
| - }; |
240 |
| - |
241 |
| - test('non-flushed', function(done) { |
242 |
| - testEnqueue(false, done); |
243 |
| - }); |
244 |
| - |
245 |
| - test('flushed', function(done) { |
246 |
| - testEnqueue(true, done); |
247 |
| - }); |
248 | 291 |
|
249 | 292 | });
|
250 | 293 | </script>
|
|
0 commit comments