Mirror of https://github.com/roostorg/coop github.com/roostorg/coop
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 97 lines 2.4 kB view raw
1import { 2 chunkAsyncIterableByKey, 3 chunkAsyncIterableBySize, 4} from './iterables.js'; 5 6describe('Iterable utils', () => { 7 describe('chunkAsyncIterableBySize', () => { 8 test('should yield chunks of proper size, except for the last chunk, which should hold the remaining items', async () => { 9 async function* foo() { 10 yield 1; 11 yield 2; 12 yield 3; 13 yield 4; 14 yield 5; 15 } 16 17 const chunkedIterable = chunkAsyncIterableBySize(2, foo()); 18 await chunkedIterable.next().then(({ value, done }) => { 19 expect(value).toEqual([1, 2]); 20 expect(done).toEqual(false); 21 }); 22 await chunkedIterable.next().then(({ value, done }) => { 23 expect(value).toEqual([3, 4]); 24 expect(done).toEqual(false); 25 }); 26 await chunkedIterable.next().then(({ value, done }) => { 27 expect(value).toEqual([5]); 28 expect(done).toEqual(false); 29 }); 30 await chunkedIterable.next().then(({ value, done }) => { 31 expect(value).toEqual(undefined); 32 expect(done).toEqual(true); 33 }); 34 }); 35 }); 36}); 37 38describe('chunkAsyncIterableByKey', () => { 39 it('should chunk numbers based on even and odd', async () => { 40 async function* sampleStream() { 41 for (let i = 1; i <= 5; i++) { 42 yield i; 43 yield i; 44 } 45 } 46 47 let result: number[][] = []; 48 for await (const chunk of chunkAsyncIterableByKey( 49 sampleStream(), 50 (item) => item % 2 === 0, 51 )) { 52 result = [...result, chunk]; 53 } 54 55 expect(result).toEqual([ 56 [1, 1], 57 [2, 2], 58 [3, 3], 59 [4, 4], 60 [5, 5], 61 ]); 62 }); 63 64 it('should handle an empty stream', async () => { 65 async function* emptyStream() {} 66 67 let result: number[][] = []; 68 for await (const chunk of chunkAsyncIterableByKey( 69 emptyStream(), 70 (it) => it, 71 )) { 72 result = [...result, chunk]; 73 } 74 75 expect(result).toEqual([]); 76 }); 77 78 it('should handle undefined as a valid key', async () => { 79 async function* sampleStream() { 80 yield 1; 81 yield undefined; 82 yield 1; 83 } 84 85 const chunkKey = (item: number | undefined) => item; 86 87 let result: (number | undefined)[][] = []; 88 for await (const chunk of chunkAsyncIterableByKey( 89 sampleStream(), 90 chunkKey, 91 )) { 92 result = [...result, chunk]; 93 } 94 95 expect(result).toEqual([[1], [undefined], [1]]); 96 }); 97});