|
| 1 | +import { assert } from 'chai' |
| 2 | +import sinon from 'sinon' |
| 3 | + |
| 4 | +import { addDisposeCallback, options } from '@tko/utils' |
| 5 | + |
| 6 | +import { queueCleanNode } from '../src/jsxClean' |
| 7 | + |
| 8 | +describe('jsxClean queue', function () { |
| 9 | + let cleaned: Node[] |
| 10 | + let clock: sinon.SinonFakeTimers |
| 11 | + let originalBatchSize: number |
| 12 | + |
| 13 | + function makeNode(): HTMLElement { |
| 14 | + const node = document.createElement('div') |
| 15 | + addDisposeCallback(node, () => cleaned.push(node)) |
| 16 | + return node |
| 17 | + } |
| 18 | + |
| 19 | + beforeEach(function () { |
| 20 | + cleaned = [] |
| 21 | + originalBatchSize = options.jsxCleanBatchSize |
| 22 | + clock = sinon.useFakeTimers() |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(function () { |
| 26 | + clock.restore() |
| 27 | + options.jsxCleanBatchSize = originalBatchSize |
| 28 | + }) |
| 29 | + |
| 30 | + describe('jsxCleanBatchSize = 0 (synchronous)', function () { |
| 31 | + beforeEach(function () { |
| 32 | + options.jsxCleanBatchSize = 0 |
| 33 | + }) |
| 34 | + |
| 35 | + it('runs cleanup synchronously on queueCleanNode', function () { |
| 36 | + const node = makeNode() |
| 37 | + queueCleanNode(node) |
| 38 | + assert.deepEqual(cleaned, [node]) |
| 39 | + }) |
| 40 | + |
| 41 | + it('drains all queued nodes synchronously in a single call', function () { |
| 42 | + const nodes = [makeNode(), makeNode(), makeNode()] |
| 43 | + for (const n of nodes) queueCleanNode(n) |
| 44 | + assert.deepEqual(cleaned, nodes) |
| 45 | + }) |
| 46 | + |
| 47 | + it('does not schedule a timer', function () { |
| 48 | + queueCleanNode(makeNode()) |
| 49 | + assert.equal(clock.countTimers(), 0) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('jsxCleanBatchSize > 0 (batched, default 1000)', function () { |
| 54 | + beforeEach(function () { |
| 55 | + options.jsxCleanBatchSize = 1000 |
| 56 | + }) |
| 57 | + |
| 58 | + it('defers cleanup until the 25ms timer fires', function () { |
| 59 | + const node = makeNode() |
| 60 | + queueCleanNode(node) |
| 61 | + assert.lengthOf(cleaned, 0, 'not cleaned before timer fires') |
| 62 | + clock.tick(25) |
| 63 | + assert.deepEqual(cleaned, [node]) |
| 64 | + }) |
| 65 | + |
| 66 | + it('does not re-trigger the timer while one is already pending', function () { |
| 67 | + const a = makeNode() |
| 68 | + const b = makeNode() |
| 69 | + queueCleanNode(a) |
| 70 | + queueCleanNode(b) |
| 71 | + clock.tick(24) |
| 72 | + assert.lengthOf(cleaned, 0) |
| 73 | + clock.tick(1) |
| 74 | + assert.deepEqual(cleaned, [a, b]) |
| 75 | + }) |
| 76 | + |
| 77 | + it('processes at most batchSize nodes per 25ms tick', function () { |
| 78 | + options.jsxCleanBatchSize = 3 |
| 79 | + const nodes = [makeNode(), makeNode(), makeNode(), makeNode(), makeNode()] |
| 80 | + for (const n of nodes) queueCleanNode(n) |
| 81 | + |
| 82 | + clock.tick(25) |
| 83 | + assert.lengthOf(cleaned, 3, 'first tick processes one batch') |
| 84 | + |
| 85 | + clock.tick(25) |
| 86 | + assert.lengthOf(cleaned, 5, 'second tick drains the remainder') |
| 87 | + }) |
| 88 | + |
| 89 | + it('re-schedules itself when the queue is non-empty after a batch', function () { |
| 90 | + options.jsxCleanBatchSize = 2 |
| 91 | + const nodes = Array.from({ length: 5 }, makeNode) |
| 92 | + for (const n of nodes) queueCleanNode(n) |
| 93 | + |
| 94 | + clock.tick(25) |
| 95 | + assert.lengthOf(cleaned, 2) |
| 96 | + clock.tick(25) |
| 97 | + assert.lengthOf(cleaned, 4) |
| 98 | + clock.tick(25) |
| 99 | + assert.lengthOf(cleaned, 5) |
| 100 | + }) |
| 101 | + |
| 102 | + it('drains synchronously if batchSize is flipped to 0 while a timer is pending', function () { |
| 103 | + const nodes = [makeNode(), makeNode(), makeNode()] |
| 104 | + for (const n of nodes) queueCleanNode(n) |
| 105 | + assert.equal(clock.countTimers(), 1) |
| 106 | + |
| 107 | + options.jsxCleanBatchSize = 0 |
| 108 | + clock.tick(25) |
| 109 | + |
| 110 | + assert.deepEqual(cleaned, nodes) |
| 111 | + assert.equal(clock.countTimers(), 0, 'no re-armed timer') |
| 112 | + }) |
| 113 | + |
| 114 | + it('drains synchronously if batchSize becomes non-finite while a timer is pending', function () { |
| 115 | + const nodes = [makeNode(), makeNode()] |
| 116 | + for (const n of nodes) queueCleanNode(n) |
| 117 | + |
| 118 | + options.jsxCleanBatchSize = Number.NaN as unknown as number |
| 119 | + clock.tick(25) |
| 120 | + |
| 121 | + assert.deepEqual(cleaned, nodes) |
| 122 | + assert.equal(clock.countTimers(), 0) |
| 123 | + }) |
| 124 | + }) |
| 125 | +}) |
0 commit comments