1 var Q = require('../kew')
3 // test that contexts are propogated based on position
4 exports.testContextWithDelay = function (test) {
8 .then(function (val, context) {
9 test.equal(context.id, 1, 'Should return the first context')
13 .then(function (val, context) {
14 test.equal(context.id, 2, 'Should return the second context')
18 .then(function (val, context) {
19 test.equal(typeof context, 'undefined', 'Should return an undefined context')
26 // test adding and removing contexts
27 exports.testGeneralContextFlow = function (test) {
29 // test no context exists
30 .then(function (val, context) {
31 test.equal(typeof context, 'undefined', 'Context should be undefined')
34 .fail(function (e, context) {
35 test.equal(typeof context, 'undefined', 'Context should be undefined')
38 // set the context and mutate it
39 .setContext({counter: 1})
40 .then(function (val, context) {
41 test.equal(context.counter, 1, 'Counter should be 1')
44 .then(function (val, context) {
45 test.equal(context.counter, 2, 'Counter should be 2')
49 .fail(function (e, context) {
50 test.equal(context.counter, 3, 'Counter should be 3')
54 .then(function (val, context) {
55 return Q.resolve(false)
56 .setContext({counter: 0})
58 .then(function (val, context) {
59 test.equal(context.counter, 0, 'Counter should be 0')
62 .fail(function (e, context) {
63 test.equal(context.counter, 0, 'Counter should be 0')
66 // returning a promise with a cleared context won't clear the parent context
67 .then(function (val, context) {
68 return Q.resolve(false).clearContext()
70 .then(function (val, context) {
71 test.equal(context.counter, 0, 'Counter should be 0')
74 .fail(function (e, context) {
75 test.equal(context.counter, 0, 'Counter should be 0')
78 // test that clearing the context works
80 .then(function (val, context) {
81 test.equal(typeof context, 'undefined', 'Context should be undefined')
84 .fail(function (e, context) {
85 test.equal(typeof context, 'undefined', 'Context should be undefined')