Last active
September 8, 2016 08:17
-
-
Save BartKrol/ad019183186c27edc0bb22617a71925d to your computer and use it in GitHub Desktop.
DataLoader fails promise when Error is returned
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
import { expect } from 'chai' | |
import DataLoader from 'dataloader' | |
import sinon from 'sinon' | |
function createByIdLoader(loadFunc) { | |
return new DataLoader( | |
ids => Promise.all( | |
ids.map(loadFunc) | |
.map(promise => promise.catch(err => new Error(err))) | |
), | |
{ | |
cache: false | |
} | |
) | |
} | |
describe('DataLoader', () => { | |
it('should return values when promises are fulfilled', () => { // Working | |
const value = 'success' | |
const stub = sinon.stub().returns(Promise.resolve(value)) | |
const loader = createByIdLoader(stub) | |
return loader.loadMany([1, 2]).then(values => { | |
expect(values).to.deep.equal([value, value]) | |
}) | |
}) | |
it('should return errors when promises are rejected', () => { // Failing | |
const value = 'fail' | |
const stub = sinon.stub().returns(Promise.reject(value)) | |
const loader = createByIdLoader(stub) | |
return loader.loadMany([1, 2]).then(errors => { | |
expect(errors).to.deep.equal([new Error(value), new Error(value)]) | |
}) | |
.catch((err) => { | |
console.log('Should never be here') | |
throw err | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment