This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from airbnb/all-errors-serializable
Allows non instanceOf Error to be thrown
- Loading branch information
Showing
3 changed files
with
149 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { assert } from 'chai'; | ||
import renderBatch from '../lib/utils/renderBatch'; | ||
|
||
class Response { | ||
status(status) { | ||
this._status = status; | ||
return this; | ||
} | ||
|
||
json(res) { | ||
this._json = res; | ||
return this; | ||
} | ||
|
||
end() {} | ||
|
||
getResponse() { | ||
return { | ||
status: this._status, | ||
json: this._json, | ||
}; | ||
} | ||
} | ||
|
||
class Request { | ||
constructor() { | ||
this.body = { | ||
a: { | ||
name: 'HypernovaExample', | ||
data: {}, | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
function makeExpress() { | ||
const req = new Request(); | ||
const res = new Response(); | ||
|
||
return { req, res }; | ||
} | ||
|
||
describe('renderBatch', () => { | ||
it('returns a batch properly', (done) => { | ||
const expressRoute = renderBatch({ | ||
getComponent() { | ||
return null; | ||
}, | ||
plugins: [], | ||
}, () => false); | ||
|
||
const { req, res } = makeExpress(); | ||
|
||
expressRoute(req, res).then(() => { | ||
assert.isObject(res.getResponse()); | ||
|
||
const { status, json } = res.getResponse(); | ||
|
||
assert.isDefined(status); | ||
|
||
assert.equal(status, 200); | ||
|
||
assert.isTrue(json.success); | ||
assert.isNull(json.error); | ||
|
||
const { a } = json.results; | ||
assert.isDefined(a); | ||
assert.property(a, 'html'); | ||
assert.property(a, 'meta'); | ||
assert.property(a, 'duration'); | ||
assert.property(a, 'success'); | ||
assert.property(a, 'error'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('rejects a Promise with a string and its ok', (done) => { | ||
const expressRoute = renderBatch({ | ||
getComponent() { | ||
return Promise.reject('Nope'); | ||
}, | ||
plugins: [], | ||
}, () => false); | ||
|
||
const { req, res } = makeExpress(); | ||
|
||
expressRoute(req, res).then(() => { | ||
const { json } = res.getResponse(); | ||
const { a } = json.results; | ||
|
||
assert.equal(a.error.name, 'Error'); | ||
assert.equal(a.error.message, 'Nope'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('rejects a Promise with a ReferenceError', (done) => { | ||
const expressRoute = renderBatch({ | ||
getComponent() { | ||
return Promise.reject(new ReferenceError()); | ||
}, | ||
plugins: [], | ||
}, () => false); | ||
|
||
const { req, res } = makeExpress(); | ||
|
||
expressRoute(req, res).then(() => { | ||
const { json } = res.getResponse(); | ||
const { a } = json.results; | ||
|
||
assert.equal(a.error.name, 'ReferenceError'); | ||
|
||
done(); | ||
}); | ||
}); | ||
|
||
it('rejects a Promise with an Array', (done) => { | ||
const expressRoute = renderBatch({ | ||
getComponent() { | ||
return Promise.reject([1, 2, 3]); | ||
}, | ||
plugins: [], | ||
}, () => false); | ||
|
||
const { req, res } = makeExpress(); | ||
|
||
expressRoute(req, res).then(() => { | ||
const { json } = res.getResponse(); | ||
const { a } = json.results; | ||
|
||
assert.equal(a.error.name, 'Error'); | ||
assert.equal(a.error.message, '1,2,3'); | ||
|
||
done(); | ||
}); | ||
}); | ||
}); |