Skip to content

Commit ec58745

Browse files
Serhii KlymenkoSerhii Klymenko
authored andcommitted
fix 'or' resolver
if wrapped component thrown an error, this error should be returned cover local error case with unit test
1 parent 6fb0b6a commit ec58745

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/helper.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ export const and = (...conditions) => resolver => {
1616

1717
// Accepts multiple authentication resolvers and returns a function which will be called
1818
// if any of the authentication resolvers succeed, or throw an error if all of them fail
19-
export const or = (...conditions) => resolver => (...query) => {
19+
export const or = (...conditions) => resolver => (root, args, context, info) => {
2020
return new Promise((resolve, reject) => {
2121
let limit = conditions.length - 1;
2222
const attempt = (i) =>
23-
conditions[i].createResolver(resolver)(...query)
24-
.then(res => resolve(res))
23+
createResolver(conditions[i])(root, args, context, info)
24+
.then(() => {
25+
createResolver(resolver)(root, args, context, info)
26+
.then(res => resolve(res))
27+
.catch(err => reject(err));
28+
})
2529
.catch(err => {
2630
if(i === limit) reject(err);
2731
else attempt(i + 1);

test/unit/helper_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,18 @@ describe('(unit) src/helper.js', () => {
172172
expect(r1.handle.calledOnce).to.be.true;
173173
});
174174
});
175+
176+
it('(true, false) should return local error from wrapped resolver', () => {
177+
const resolver = or(successResolver, failureResolver);
178+
const localError = new Error('test');
179+
const finalResolver = resolver(() => {
180+
throw localError;
181+
});
182+
return finalResolver()
183+
.catch(err => {
184+
expect(err).to.equal(localError);
185+
});
186+
});
175187
});
176188

177189
});

0 commit comments

Comments
 (0)