| 1 | var Q = require('../kew') |
| 2 | |
| 3 | exports.testThen = function (test) { |
| 4 | var detectedScope = null |
| 5 | Q.resolve(true).then(function () { |
| 6 | detectedScope = this |
| 7 | }).then(function () { |
| 8 | test.ok(Q.isPromise(detectedScope), 'then() should be called in context of promise') |
| 9 | test.done() |
| 10 | }) |
| 11 | } |
| 12 | |
| 13 | exports.testFail = function (test) { |
| 14 | var detectedScope = null |
| 15 | Q.reject(new Error()).fail(function () { |
| 16 | detectedScope = this |
| 17 | }).then(function () { |
| 18 | test.ok(Q.isPromise(detectedScope), 'fail() should be called in context of promise') |
| 19 | test.done() |
| 20 | }) |
| 21 | } |
| 22 | |
| 23 | exports.testThenBound = function (test) { |
| 24 | var detectedScope = scope |
| 25 | var scope = {} |
| 26 | Q.resolve(true).thenBound(function () { |
| 27 | detectedScope = scope |
| 28 | }, scope).then(function () { |
| 29 | test.ok(detectedScope === scope, 'thenScoped() should be called in context of scope') |
| 30 | test.done() |
| 31 | }) |
| 32 | } |
| 33 | |
| 34 | exports.testFailBound = function (test) { |
| 35 | var detectedScope = scope |
| 36 | var scope = {} |
| 37 | Q.reject(new Error()).failBound(function () { |
| 38 | detectedScope = scope |
| 39 | }, scope).then(function () { |
| 40 | test.equal(detectedScope, scope, 'failBound() should be called in context of scope') |
| 41 | test.done() |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | exports.testThenBoundWithArgs = function (test) { |
| 46 | var detectedScope = scope |
| 47 | var scope = {} |
| 48 | Q.resolve(-1).thenBound(function (a, b, c, d) { |
| 49 | test.equal(a, 1) |
| 50 | test.equal(b, 2) |
| 51 | test.equal(c, 3) |
| 52 | test.equal(d, -1) |
| 53 | detectedScope = scope |
| 54 | }, scope, 1, 2, 3).then(function () { |
| 55 | test.ok(detectedScope === scope, 'failScoped() should be called in context of scope') |
| 56 | test.done() |
| 57 | }) |
| 58 | } |
| 59 | |