Skip to content

Instantly share code, notes, and snippets.

@mainyaa
Last active December 31, 2015 22:39
Show Gist options
  • Save mainyaa/8054803 to your computer and use it in GitHub Desktop.
Save mainyaa/8054803 to your computer and use it in GitHub Desktop.
unit testing nested $q.all().
'use strict'
# dependencies angularjs, karma, karma-mocha, mocha, chai, lodash
describe 'Unit: $q.all', () ->
describe 'should simulate nested promise', ->
scope = null
q = null
promise1resolved = false
promise1rejected = false
promise2resolved = false
promise2rejected = false
promise3resolved = false
promise3rejected = false
deferredList1 = null
promiseList1 = null
allPromise1 = null
deferredList2 = null
promiseList2 = null
allPromise2 = null
deferredList3 = null
promiseList3 = null
deferred3 = null
promise3 = null
allPromise3 = null
beforeEach inject ($q, $rootScope) =>
scope = $rootScope.$new()
q = $q
promise1resolved = false
promise1rejected = false
promise2resolved = false
promise2rejected = false
promise3resolved = false
promise3rejected = false
deferredList1 = _.map _.range(3), () => q.defer()
promiseList1 = _.map deferredList1, (def) => def.promise
deferredList2 = _.map _.range(3), () => q.defer()
promiseList2 = _.map deferredList2, (def) => def.promise
allPromise1 = q.all(promiseList1)
allPromise1.then(
(success) => promise1resolved = true
(failure) => promise1rejected = true
)
allPromise2 = q.all(promiseList2)
allPromise2.then(
(success) => promise2resolved = true
(failure) => promise2rejected = true
)
deferred3 = q.defer()
promise3 = deferred3.promise
# top level promise
allPromise3 = q.all([allPromise1, allPromise2, promise3])
allPromise3.then(
(success) => promise3resolved = true
(failure) => promise3rejected = true
)
afterEach =>
scope = null
q = null
promise1resolved = false
promise1rejected = false
promise2resolved = false
promise2rejected = false
promise3resolved = false
promise3rejected = false
deferredList1 = null
promiseList1 = null
allPromise1 = null
deferredList2 = null
promiseList2 = null
allPromise2 = null
deferredList3 = null
promiseList3 = null
deferred3 = null
promise3 = null
allPromise3 = null
it 'should promise.all resolve when all nested promise resolve', =>
# test target
target1 = () =>
_.each deferredList1, (def) -> def.resolve(true)
scope.$apply()
target2 = () =>
_.each deferredList2, (def) -> def.resolve(true)
scope.$apply()
target3 = () =>
deferred3.resolve(true)
scope.$apply()
# nothing happens
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
# done promise1
target1()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
# done promise2
target2()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
# You've all done very well!
target3()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.true
expect(promise3rejected).to.be.false
it 'should promise.all reject when a nested promise1 rejected', =>
# test target
target1 = () =>
# reject
_.each deferredList1, (def) -> def.reject(true)
scope.$apply()
target2 = () =>
_.each deferredList2, (def) -> def.resolve(true)
scope.$apply()
target3 = () =>
deferred3.resolve(true)
scope.$apply()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target1()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.true
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true # top level promise rejected
target2()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.true
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true
target3()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.true
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true
it 'should promise.all reject when a nested promise2 rejected', =>
# test target
target1 = () =>
_.each deferredList1, (def) -> def.resolve(true)
scope.$apply()
target2 = () =>
# reject
_.each deferredList2, (def) -> def.reject(true)
scope.$apply()
target3 = () =>
deferred3.resolve(true)
scope.$apply()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target1()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target2()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.true
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true # top level promise rejected
target3()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.true
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true
it 'should promise.all reject when a nested promise3 rejected', =>
# test target
target1 = () =>
_.each deferredList1, (def) -> def.resolve(true)
scope.$apply()
target2 = () =>
_.each deferredList2, (def) -> def.resolve(true)
scope.$apply()
target3 = () =>
# reject
deferred3.reject(true)
scope.$apply()
expect(promise1resolved).to.be.false
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target1()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.false
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target2()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.false
target3()
expect(promise1resolved).to.be.true
expect(promise1rejected).to.be.false
expect(promise2resolved).to.be.true
expect(promise2rejected).to.be.false
expect(promise3resolved).to.be.false
expect(promise3rejected).to.be.true # top level promise rejected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment