BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / kew / test / context.js

context.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
context.js 2.5 KiB Download Raw
1var Q = require('../kew')
2
3// test that contexts are propogated based on position
4exports.testContextWithDelay = function (test) {
5
6 Q.resolve(true)
7 .setContext({id: 1})
8 .then(function (val, context) {
9 test.equal(context.id, 1, 'Should return the first context')
10 return Q.delay(500)
11 })
12 .setContext({id: 2})
13 .then(function (val, context) {
14 test.equal(context.id, 2, 'Should return the second context')
15 return Q.delay(500)
16 })
17 .clearContext()
18 .then(function (val, context) {
19 test.equal(typeof context, 'undefined', 'Should return an undefined context')
20 return Q.delay(500)
21 })
22 .setContext({id: 3})
23 .fin(test.done)
24}
25
26// test adding and removing contexts
27exports.testGeneralContextFlow = function (test) {
28 Q.resolve(true)
29 // test no context exists
30 .then(function (val, context) {
31 test.equal(typeof context, 'undefined', 'Context should be undefined')
32 throw new Error()
33 })
34 .fail(function (e, context) {
35 test.equal(typeof context, 'undefined', 'Context should be undefined')
36 })
37
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')
42 context.counter++
43 })
44 .then(function (val, context) {
45 test.equal(context.counter, 2, 'Counter should be 2')
46 context.counter++
47 throw new Error()
48 })
49 .fail(function (e, context) {
50 test.equal(context.counter, 3, 'Counter should be 3')
51 })
52
53 // return a context
54 .then(function (val, context) {
55 return Q.resolve(false)
56 .setContext({counter: 0})
57 })
58 .then(function (val, context) {
59 test.equal(context.counter, 0, 'Counter should be 0')
60 throw new Error()
61 })
62 .fail(function (e, context) {
63 test.equal(context.counter, 0, 'Counter should be 0')
64 })
65
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()
69 })
70 .then(function (val, context) {
71 test.equal(context.counter, 0, 'Counter should be 0')
72 throw new Error()
73 })
74 .fail(function (e, context) {
75 test.equal(context.counter, 0, 'Counter should be 0')
76 })
77
78 // test that clearing the context works
79 .clearContext()
80 .then(function (val, context) {
81 test.equal(typeof context, 'undefined', 'Context should be undefined')
82 throw new Error()
83 })
84 .fail(function (e, context) {
85 test.equal(typeof context, 'undefined', 'Context should be undefined')
86 })
87
88 .fin(test.done)
89}