mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 22:10:41 -07:00
refactor: strictly typed
This commit is contained in:
@@ -1,68 +1,26 @@
|
||||
import * as chai from 'chai'
|
||||
import * as sinon from 'sinon'
|
||||
import * as sinonChai from 'sinon-chai'
|
||||
import * as chaiAsPromised from 'chai-as-promised'
|
||||
|
||||
const expect = chai.expect
|
||||
chai.use(sinonChai)
|
||||
chai.use(chaiAsPromised)
|
||||
|
||||
const P = require('src/util/promise')
|
||||
|
||||
describe('util/promise', function () {
|
||||
describe('.anySeries()', function () {
|
||||
it('should resolve in series', function () {
|
||||
const spy1 = sinon.spy()
|
||||
const spy2 = sinon.spy()
|
||||
return P
|
||||
.anySeries(
|
||||
['first', 'second'],
|
||||
(item, idx) => new Promise(function (resolve, reject) {
|
||||
if (idx === 0) {
|
||||
setTimeout(function () {
|
||||
spy1()
|
||||
reject(new Error('first cb'))
|
||||
}, 10)
|
||||
} else {
|
||||
spy2()
|
||||
resolve('foo')
|
||||
}
|
||||
}))
|
||||
.then(() => expect(spy2).to.have.been.calledAfter(spy1))
|
||||
})
|
||||
it('should reject when all rejected', function () {
|
||||
const p = P.anySeries(['first', 'second', 'third'],
|
||||
item => Promise.reject(new Error(item)))
|
||||
return expect(p).to.be.rejectedWith('third')
|
||||
})
|
||||
it('should resolve the value that first callback resolved', async () => {
|
||||
const result = await P.anySeries(
|
||||
['first', 'second'],
|
||||
item => Promise.resolve(item)
|
||||
)
|
||||
return expect(result).to.equal('first')
|
||||
})
|
||||
it('should not call rest of callbacks once resolved', () => {
|
||||
const spy = sinon.spy()
|
||||
return P
|
||||
.anySeries(['first', 'second'], (item, idx) => {
|
||||
if (idx > 0) {
|
||||
spy()
|
||||
}
|
||||
return Promise.resolve(item)
|
||||
})
|
||||
.then(() => expect(spy).to.not.have.been.called)
|
||||
})
|
||||
})
|
||||
describe('.mapSeries()', async function () {
|
||||
it('should resolve when all resolved', async function () {
|
||||
const result = await P.mapSeries(
|
||||
['first', 'second', 'third'],
|
||||
item => Promise.resolve(item)
|
||||
(item: string) => Promise.resolve(item)
|
||||
)
|
||||
return expect(result).to.deep.equal(['first', 'second', 'third'])
|
||||
})
|
||||
it('should reject with the error that first callback rejected', () => {
|
||||
const p = P.mapSeries(['first', 'second'],
|
||||
item => Promise.reject(item))
|
||||
(item: string) => Promise.reject(item))
|
||||
return expect(p).to.rejectedWith('first')
|
||||
})
|
||||
it('should resolve in series', function () {
|
||||
@@ -71,7 +29,7 @@ describe('util/promise', function () {
|
||||
return P
|
||||
.mapSeries(
|
||||
['first', 'second'],
|
||||
(item, idx) => new Promise(function (resolve) {
|
||||
(item: string, idx: number) => new Promise(function (resolve) {
|
||||
if (idx === 0) {
|
||||
setTimeout(function () {
|
||||
spy1()
|
||||
@@ -87,7 +45,7 @@ describe('util/promise', function () {
|
||||
it('should not call rest of callbacks once rejected', () => {
|
||||
const spy = sinon.spy()
|
||||
return P
|
||||
.mapSeries(['first', 'second'], (item, idx) => {
|
||||
.mapSeries(['first', 'second'], (item: string, idx: number) => {
|
||||
if (idx > 0) {
|
||||
spy()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user