uppy/packages/@uppy/utils/src/limitPromises.test.js
Renée Kooi 2e257b7e55
Move utils files to @uppy/utils package.
And rewrite all the require calls.

```bash
git mv src/utils/* packages/@uppy/utils/src
sed -i 's/[./]*\/utils\//@uppy\/utils\/lib\//' src/**/*.js # transform (../)*utils → @uppy/utils
```
2018-06-14 16:31:19 +02:00

47 lines
1,003 B
JavaScript

const limitPromises = require('./limitPromises')
describe('limitPromises', () => {
let pending = 0
function fn () {
pending++
return new Promise((resolve) => setTimeout(resolve, 10))
.then(() => pending--)
}
it('should run at most N promises at the same time', () => {
const limit = limitPromises(4)
const fn2 = limit(fn)
const result = Promise.all([
fn2(), fn2(), fn2(), fn2(),
fn2(), fn2(), fn2(), fn2(),
fn2(), fn2()
])
expect(pending).toBe(4)
setTimeout(() => {
expect(pending).toBe(4)
}, 10)
return result.then(() => {
expect(pending).toBe(0)
})
})
it('should accept Infinity as limit', () => {
const limit = limitPromises(Infinity)
const fn2 = limit(fn)
const result = Promise.all([
fn2(), fn2(), fn2(), fn2(),
fn2(), fn2(), fn2(), fn2(),
fn2(), fn2()
])
expect(pending).toBe(10)
return result.then(() => {
expect(pending).toBe(0)
})
})
})