mirror of
https://github.com/axios/axios.git
synced 2026-04-11 02:11:50 +08:00
* fix(progress): clamp loaded value to total in progress reducer * test(progress): cover out-of-order events in reducer clamp case * fix(progress): keep bytesNotified monotonic for out-of-order events --------- Co-authored-by: Jay <jasonsaayman@gmail.com>
28 lines
1.0 KiB
JavaScript
28 lines
1.0 KiB
JavaScript
import assert from 'assert';
|
|
import { progressEventReducer } from '../../../lib/helpers/progressEventReducer.js';
|
|
|
|
describe('helpers::progressEventReducer', () => {
|
|
it('should clamp loaded/progress and avoid negative bytes for out-of-order events', () => {
|
|
const events = [];
|
|
const [onProgress, flush] = progressEventReducer((data) => {
|
|
events.push(data);
|
|
}, false, Number.POSITIVE_INFINITY);
|
|
|
|
onProgress({ lengthComputable: true, loaded: 80, total: 100 });
|
|
onProgress({ lengthComputable: true, loaded: 60, total: 100 });
|
|
onProgress({ lengthComputable: true, loaded: 180, total: 100 });
|
|
flush();
|
|
|
|
assert.strictEqual(events.length, 3);
|
|
assert.strictEqual(events[0].bytes, 80);
|
|
assert.strictEqual(events[1].bytes, 0);
|
|
|
|
const last = events[events.length - 1];
|
|
assert.strictEqual(last.loaded, 100);
|
|
assert.strictEqual(last.total, 100);
|
|
assert.strictEqual(last.progress, 1);
|
|
assert.strictEqual(last.upload, true);
|
|
assert.strictEqual(last.bytes, 20);
|
|
});
|
|
});
|