-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added promise support for library (#140)
* Added promise support for library Based on this excellent SO answer: https://stackoverflow.com/a/36838115/3694288 The objective is to make the library more available and easy to use. * Added linting check in package.json
- Loading branch information
1 parent
39af582
commit d73f838
Showing
4 changed files
with
113 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var getSchema = require('../'); | ||
var assert = require('assert'); | ||
var _ = require('lodash'); | ||
|
||
describe('getSchema should return promise', function() { | ||
var docs = [ | ||
{ foo: 'bar'}, | ||
{ country: 'Croatia'}, | ||
{ country: 'Croatia'}, | ||
{ country: 'England'}, | ||
]; | ||
|
||
it('Check if return value is a promise', () => { | ||
var result = getSchema(docs); | ||
assert.strictEqual(result instanceof Promise, true); | ||
}); | ||
|
||
it('Check that promise returns expected schema', async() => { | ||
const result = await getSchema(docs); | ||
const fieldNames = result.fields.map(v => v.name); | ||
assert.deepStrictEqual(fieldNames, ['country', 'foo']); | ||
}); | ||
|
||
describe('Check callback and promise return the same thing; success', () => { | ||
let promiseResponse; | ||
let callbackResponse; | ||
|
||
before((done) => { | ||
getSchema(docs).then((result) => { | ||
promiseResponse = result; | ||
}); | ||
getSchema(docs, (err, callbackResult) => { | ||
if (err) { | ||
throw err; | ||
} | ||
callbackResponse = callbackResult; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Using callback and promise should return the same thing', () => { | ||
assert(_.isEqual(promiseResponse, callbackResponse)); | ||
}); | ||
}); | ||
|
||
describe('Check callback and promise return the same thing; failure', () => { | ||
let promiseErrMessage; | ||
let callbackErrMessage; | ||
|
||
before((done) => { | ||
getSchema({foo: 'bar'}, (err) => { | ||
getSchema({foo: 'bar'}).catch(error => { | ||
promiseErrMessage = error.message; | ||
}); | ||
callbackErrMessage = err.message; | ||
done(); | ||
}); | ||
}); | ||
|
||
it('Using callback and promise should give same err message', () => { | ||
assert.strictEqual(promiseErrMessage, callbackErrMessage); | ||
}); | ||
}); | ||
}); |