Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
fix(moduleRoute): return promises (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
JAdshead authored Jan 20, 2021
1 parent 7b7d2d8 commit cbaa54d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
57 changes: 57 additions & 0 deletions packages/holocron-module-route/__tests__/ModuleRouteUtils.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ describe('ModuleRouteUtils', () => {
});
return moduleRoute.getIndexRoute(undefined, callback);
});

it('errors can be caught', async () => {
expect.assertions(1);
const moduleName = 'test-module';
const props = { moduleName, store };
const moduleRoute = createModuleRoute(defaultProps, props);
// eslint-disable-next-line no-throw-literal
const callback = jest.fn(() => { throw 'some error'; });
try {
await moduleRoute.getIndexRoute(undefined, callback);
} catch (e) {
expect(e).toEqual('some error');
}
});
});

describe('getChildRoutes', () => {
Expand All @@ -163,6 +177,20 @@ describe('ModuleRouteUtils', () => {
});
return moduleRoute.getChildRoutes(undefined, callback);
});

it('errors can be caught', async () => {
expect.assertions(1);
const moduleName = 'test-module';
const props = { moduleName, store };
const moduleRoute = createModuleRoute(defaultProps, props);
// eslint-disable-next-line no-throw-literal
const callback = jest.fn(() => { throw 'some error'; });
try {
await moduleRoute.getChildRoutes(undefined, callback);
} catch (e) {
expect(e).toEqual('some error');
}
});
});

describe('getComponent', () => {
Expand All @@ -177,6 +205,20 @@ describe('ModuleRouteUtils', () => {
});
return moduleRoute.getComponent(undefined, callback);
});

it('errors can be caught', async () => {
expect.assertions(1);
const moduleName = 'test-module';
const props = { moduleName, store };
const moduleRoute = createModuleRoute(defaultProps, props);
// eslint-disable-next-line no-throw-literal
const callback = jest.fn(() => { throw 'some error'; });
try {
await moduleRoute.getComponent(undefined, callback);
} catch (e) {
expect(e).toEqual('some error');
}
});
});

describe('onEnter', () => {
Expand All @@ -195,6 +237,21 @@ describe('ModuleRouteUtils', () => {
moduleRoute.onEnter('nextState', replace, cb);
});

it('errors can be caught', async () => {
expect.assertions(1);
const moduleName = 'store-hook-module';
const props = { moduleName, store };
const moduleRoute = createModuleRoute(defaultProps, props);
// eslint-disable-next-line no-throw-literal
const cb = jest.fn(() => { throw 'onEnter error'; });
const replace = jest.fn();
try {
await moduleRoute.onEnter('nextState', replace, cb);
} catch (e) {
expect(e).toEqual('onEnter error');
}
});

it('should allow the Module to specify a synchronous onEnter hook', (done) => {
const moduleName = 'sync-hook-module';
const props = { moduleName, store };
Expand Down
8 changes: 4 additions & 4 deletions packages/holocron-module-route/src/ModuleRouteUtils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ export const createModuleRoute = (defaultProps, props) => {
if (moduleName) {
moduleRoute = Object.assign(moduleRoute, {
getIndexRoute(partialNextState, cb) {
store.dispatch(loadModule(moduleName))
return store.dispatch(loadModule(moduleName))
.then(
({ childRoutes }) => cb(null, getRouteIndex(childRoutes, { store })),
cb
);
},
getChildRoutes(location, cb) {
store.dispatch(loadModule(moduleName))
return store.dispatch(loadModule(moduleName))
.then(
({ childRoutes }) => cb(null, passChildrenProps(childRoutes, { store })),
cb
);
},
getComponent(nextState, cb) {
store.dispatch(loadModule(moduleName))
return store.dispatch(loadModule(moduleName))
.then(
(module) => cb(null, module),
cb
Expand All @@ -73,7 +73,7 @@ export const createModuleRoute = (defaultProps, props) => {
if (moduleName && !moduleRoute.onEnter) {
moduleRoute = Object.assign(moduleRoute, {
onEnter(nextState, replace, cb) {
store.dispatch(loadModule(moduleName))
return store.dispatch(loadModule(moduleName))
.then(
({ onEnterRouteHook }) => {
if (!onEnterRouteHook) {
Expand Down

0 comments on commit cbaa54d

Please # to comment.