|
| 1 | +/*! |
| 2 | + * always-done <https://github.com/hybridables/always-done> |
| 3 | + * |
| 4 | + * Copyright (c) 2015-2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk) |
| 5 | + * Released under the MIT license. |
| 6 | + */ |
| 7 | + |
| 8 | +/* jshint asi:true */ |
| 9 | + |
| 10 | +'use strict' |
| 11 | + |
| 12 | +var fs = require('fs') |
| 13 | +var test = require('assertit') |
| 14 | +var path = require('path') |
| 15 | +var alwaysDone = require('../index') |
| 16 | + |
| 17 | +test('should allow passing custom context through options', function (done) { |
| 18 | + alwaysDone(function () { |
| 19 | + test.strictEqual(this.num, 123) |
| 20 | + test.strictEqual(this.bool, true) |
| 21 | + test.strictEqual(arguments.length, 0) |
| 22 | + }, { |
| 23 | + context: { num: 123, bool: true } |
| 24 | + }, done) |
| 25 | +}) |
| 26 | + |
| 27 | +test('should allow passing custom context through `.call`', function (done) { |
| 28 | + alwaysDone.call({ |
| 29 | + foo: 'bar' |
| 30 | + }, function () { |
| 31 | + test.strictEqual(this.foo, 'bar') |
| 32 | + test.strictEqual(arguments.length, 0) |
| 33 | + }, done) |
| 34 | +}) |
| 35 | + |
| 36 | +test('should allow passing custom arguments through options', function (done) { |
| 37 | + var filepath = path.join(__dirname, '../package.json') |
| 38 | + alwaysDone(fs.readFile, { |
| 39 | + args: [filepath, 'utf8'] |
| 40 | + }, function (err, res) { |
| 41 | + test.strictEqual(err, null) |
| 42 | + test.strictEqual(typeof res, 'string') |
| 43 | + |
| 44 | + var json = JSON.parse(res) |
| 45 | + test.strictEqual(json.license, 'MIT') |
| 46 | + done() |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +test('should pass callback arg, when `fn` is async or when sync but passCallback: true', function (done) { |
| 51 | + alwaysDone(function (foo, bar) { |
| 52 | + test.strictEqual(arguments.length, 2) |
| 53 | + test.strictEqual(foo, 'bar') |
| 54 | + test.strictEqual(bar, 'qux') |
| 55 | + }, { |
| 56 | + args: ['bar', 'qux'] |
| 57 | + }, function (err) { |
| 58 | + test.strictEqual(err, null) |
| 59 | + alwaysDone(function (aaa, bbb) { |
| 60 | + test.strictEqual(arguments.length, 3) |
| 61 | + test.strictEqual(aaa, 111) |
| 62 | + test.strictEqual(bbb, 222) |
| 63 | + |
| 64 | + // get the callback (3rd argument) |
| 65 | + // intentionally getting it in this way |
| 66 | + var cb = arguments[2] |
| 67 | + cb() |
| 68 | + }, { |
| 69 | + passCallback: true, |
| 70 | + args: [111, 222] |
| 71 | + }, done) |
| 72 | + }) |
| 73 | +}) |
0 commit comments