Skip to content

Commit

Permalink
.attachChild accept route from any constructor [fix]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed May 9, 2020
1 parent f7839e1 commit 8b172a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/attach.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

// Imports
const Route = require('./index.js'),
{IS_INITIALIZED, ATTACH_TO, DEBUG_ZONE, DEBUG_ERROR} = require('./symbols.js');
{IS_INITIALIZED, ATTACH_TO, DEBUG_ZONE, DEBUG_ERROR} = require('./symbols.js'),
{isRoute} = Route;

// Exports

module.exports = {
attachChild(route) {
// Check is instance of Route class
if (!(route instanceof Route)) {
if (!isRoute(route)) {
throw this[DEBUG_ERROR](
new Error('attachChild must be called with an instance of Route class')
new Error(`attachChild must be called with an instance of Route class - received ${route}`)
);
}

Expand Down
22 changes: 22 additions & 0 deletions test/attachChild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ describe('.attachChild()', () => {
expect(child[ATTACH_TO]).toHaveBeenCalledWith(route);
});

describe('throws if passed', () => {
it('object', () => {
expect(() => route.attachChild({})).toThrowWithMessage(
Error,
'attachChild must be called with an instance of Route class - received [object Object] (router path /)'
);
});

it('Route class', () => { // eslint-disable-line jest/lowercase-name
expect(() => route.attachChild(Route)).toThrow(
/^attachChild must be called with an instance of Route class - received [\s\S]* \(router path \/\)$/
);
});

it('null', () => {
expect(() => route.attachChild(null)).toThrowWithMessage(
Error,
'attachChild must be called with an instance of Route class - received null (router path /)'
);
});
});

it('tags error thrown in `child[ATTACH_TO]()` with child router path', () => {
class RouteSubclass extends Route {
[ATTACH_TO](parent) {
Expand Down

0 comments on commit 8b172a4

Please sign in to comment.