|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import chaiQuantifiers from 'chai-quantifiers'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import sinonChai from 'sinon-chai'; |
| 6 | + |
| 7 | +import { |
| 8 | + map as bufferAsyncIterable, |
| 9 | +} from '../index.js'; |
| 10 | + |
| 11 | +import { |
| 12 | + yieldValuesOverTime, |
| 13 | +} from './utils.js'; |
| 14 | + |
| 15 | +chai.use(chaiAsPromised); |
| 16 | +chai.use(chaiQuantifiers); |
| 17 | +chai.use(sinonChai); |
| 18 | + |
| 19 | +chai.should(); |
| 20 | + |
| 21 | +describe('bufferAsyncIterable() AsyncInterface return()', () => { |
| 22 | + const count = 6; |
| 23 | + |
| 24 | + /** @type {import('sinon').SinonFakeTimers} */ |
| 25 | + let clock; |
| 26 | + /** @type {AsyncIterable<number>} */ |
| 27 | + let baseAsyncIterable; |
| 28 | + /** @type {number[]} */ |
| 29 | + let expectedResult; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + clock = sinon.useFakeTimers(); |
| 33 | + baseAsyncIterable = yieldValuesOverTime(count, (i) => i % 2 === 1 ? 2000 : 100); |
| 34 | + |
| 35 | + expectedResult = []; |
| 36 | + for (let i = 0; i < count; i++) { |
| 37 | + expectedResult.push(i); |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + sinon.restore(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should end the iterator when called', async () => { |
| 46 | + const iterator = bufferAsyncIterable(baseAsyncIterable, async (item) => item); |
| 47 | + |
| 48 | + await iterator.next().should.eventually.deep.equal({ value: 0 }); |
| 49 | + |
| 50 | + const returnValue = iterator.return(); |
| 51 | + const nextAfterReturn = iterator.next(); |
| 52 | + |
| 53 | + await clock.runAllAsync(); |
| 54 | + |
| 55 | + await returnValue.should.eventually.deep.equal({ done: true, value: undefined }); |
| 56 | + await nextAfterReturn.should.eventually.deep.equal({ done: true, value: undefined }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should be called when a loop breaks', async () => { |
| 60 | + const iterator = bufferAsyncIterable(baseAsyncIterable, async (item) => item, { queueSize: 3 }); |
| 61 | + const returnSpy = sinon.spy(iterator, 'return'); |
| 62 | + const throwSpy = sinon.spy(iterator, 'throw'); |
| 63 | + |
| 64 | + const promisedResult = (async () => { |
| 65 | + // eslint-disable-next-line no-unreachable-loop |
| 66 | + for await (const value of iterator) { |
| 67 | + value.should.equal(0); |
| 68 | + break; |
| 69 | + } |
| 70 | + |
| 71 | + return Date.now(); |
| 72 | + })(); |
| 73 | + |
| 74 | + await clock.runAllAsync(); |
| 75 | + |
| 76 | + const duration = await promisedResult; |
| 77 | + |
| 78 | + // TODO: Do we need an await here? |
| 79 | + await iterator.next().should.eventually.deep.equal({ done: true, value: undefined }); |
| 80 | + duration.should.equal(2200); |
| 81 | + |
| 82 | + returnSpy.should.have.been.calledOnceWithExactly(); |
| 83 | + throwSpy.should.not.have.been.called; |
| 84 | + }); |
| 85 | + |
| 86 | + it('should be called when a loop throws', async () => { |
| 87 | + const iterator = bufferAsyncIterable(baseAsyncIterable, async (item) => item); |
| 88 | + const returnSpy = sinon.spy(iterator, 'return'); |
| 89 | + const throwSpy = sinon.spy(iterator, 'throw'); |
| 90 | + const errorToThrow = new Error('Yet another error'); |
| 91 | + |
| 92 | + const promisedResult = (async () => { |
| 93 | + // eslint-disable-next-line no-unreachable-loop |
| 94 | + for await (const value of iterator) { |
| 95 | + value.should.equal(0); |
| 96 | + throw errorToThrow; |
| 97 | + } |
| 98 | + |
| 99 | + return Date.now(); |
| 100 | + })(); |
| 101 | + |
| 102 | + await clock.runAllAsync(); |
| 103 | + |
| 104 | + returnSpy.should.have.been.calledOnceWithExactly(); |
| 105 | + throwSpy.should.not.have.been.called; |
| 106 | + |
| 107 | + await promisedResult.should.eventually.be.rejectedWith(errorToThrow); |
| 108 | + await iterator.next().should.eventually.deep.equal({ done: true, value: undefined }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments