1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/test/unit/app/nodeify-test.js

71 lines
1.8 KiB
JavaScript
Raw Normal View History

import assert from 'assert'
import nodeify from '../../../app/scripts/lib/nodeify'
2016-11-29 02:27:20 +01:00
2017-05-04 23:35:10 +02:00
describe('nodeify', function () {
const obj = {
2016-11-29 02:27:20 +01:00
foo: 'bar',
promiseFunc (a) {
const solution = this.foo + a
2016-11-29 02:27:20 +01:00
return Promise.resolve(solution)
2017-05-04 23:35:10 +02:00
},
2016-11-29 02:27:20 +01:00
}
2017-05-04 23:35:10 +02:00
it('should retain original context', function (done) {
const nodified = nodeify(obj.promiseFunc, obj)
nodified('baz', (err, res) => {
2018-07-03 02:21:27 +02:00
if (!err) {
assert.equal(res, 'barbaz')
done()
return
2018-07-03 02:21:27 +02:00
}
done(new Error(err.toString()))
2016-11-29 02:27:20 +01:00
})
})
2017-08-04 04:10:33 +02:00
it('no callback - should allow the last argument to not be a function', function (done) {
2017-08-04 05:52:09 +02:00
const nodified = nodeify(obj.promiseFunc, obj)
2017-08-04 04:10:33 +02:00
try {
nodified('baz')
2017-08-04 05:52:09 +02:00
done()
2017-10-06 22:16:44 +02:00
} catch (err) {
done(new Error('should not have thrown if the last argument is not a function'))
2017-08-04 04:10:33 +02:00
}
})
it('sync functions - returns value', function (done) {
const nodified = nodeify(() => 42)
try {
nodified((err, result) => {
if (err) {
done(new Error(`should not have thrown any error: ${err.message}`))
return
}
assert.equal(42, result, 'got expected result')
})
done()
} catch (err) {
done(new Error(`should not have thrown any error: ${err.message}`))
}
})
it('sync functions - handles errors', function (done) {
const nodified = nodeify(() => {
throw new Error('boom!')
})
try {
nodified((err, result) => {
if (result) {
done(new Error('should not have returned any result'))
return
}
assert.ok(err, 'got expected error')
assert.ok(err.message.includes('boom!'), 'got expected error message')
})
done()
} catch (err) {
done(new Error(`should not have thrown any error: ${err.message}`))
}
})
2016-11-29 02:27:20 +01:00
})