promise.spec.ts 776 B

12345678910111213141516171819202122232425262728
  1. import { batchProcessPromiseAll } from './promise';
  2. describe('batchProcessPromiseAll', () => {
  3. it('processes items in batch', async() => {
  4. const batch1 = [1, 2, 3, 4, 5];
  5. const batch2 = [6, 7, 8, 9, 10];
  6. const batch3 = [11, 12];
  7. const all = [...batch1, ...batch2, ...batch3];
  8. const actualProcessedBatches: number[][] = [];
  9. const result = await batchProcessPromiseAll(all, 5, async(num, i, arr) => {
  10. if (arr != null && i === 0) {
  11. actualProcessedBatches.push(arr);
  12. }
  13. return num * 10;
  14. });
  15. const expected = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
  16. expect(result).toStrictEqual(expected);
  17. expect(actualProcessedBatches).toStrictEqual([
  18. batch1,
  19. batch2,
  20. batch3,
  21. ]);
  22. });
  23. });