Skip to content

Commit

Permalink
fix: router lifecycle use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
platosha committed Oct 1, 2024
1 parent 1c6a7e6 commit 63493c9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 78 deletions.
10 changes: 7 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ function amend<
...args: A
): (result: ActionResult) => MaybePromise<ActionResult | undefined> {
return async (amendmentResult: ActionResult) => {
if (amendmentResult && ('cancel' in amendmentResult || 'redirect' in amendmentResult)) {
if (
amendmentResult &&
isObject(amendmentResult) &&
('cancel' in amendmentResult || 'redirect' in amendmentResult)
) {
return amendmentResult;
}

Expand Down Expand Up @@ -737,7 +741,7 @@ export class Router<R extends AnyObject = EmptyObject> extends Resolver<R> {
}
}
return await callbacks.then(async (amendmentResult: ActionResult) => {
if (amendmentResult) {
if (amendmentResult && isObject(amendmentResult)) {
if ('cancel' in amendmentResult && this.__previousContext) {
this.__previousContext.__renderId = newContext.__renderId;
return this.__previousContext;
Expand Down Expand Up @@ -766,7 +770,7 @@ export class Router<R extends AnyObject = EmptyObject> extends Resolver<R> {
result = (await beforeLeaveFunction(await callbacks)) as ResolveResult<R> | undefined;
}

if (result && !('redirect' in (result as object))) {
if (result && !(isObject(result) && 'redirect' in (result as object))) {
return result as ActionResult;
}
}
Expand Down
159 changes: 84 additions & 75 deletions test/router/lifecycle-events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,18 @@ describe('Vaadin Router lifecycle events', () => {
before(() => {
outlet = document.createElement('div');
document.body.append(outlet);
history.pushState(null, '', '/');
});

after(() => {
outlet.remove();
history.back();
});

beforeEach(() => {
// create a new router instance
router = new Router(outlet);
history.replaceState(null, '', '/');
});

afterEach(() => {
Expand Down Expand Up @@ -286,22 +289,22 @@ describe('Vaadin Router lifecycle events', () => {
document.createElement('div'),
];

await Promise.all(
values.map(async (value) => {
const onBeforeEnter = sinon.stub().returns(value);
await router.setRoutes(
[
{ path: '/', action: onBeforeEnterAction('x-home-view', onBeforeEnter) },
{ path: '/users', component: 'x-users-list' },
],
true,
);
for (const value of values) {
const onBeforeEnter = sinon.stub().returns(value);
// eslint-disable-next-line no-await-in-loop
await router.setRoutes(
[
{ path: '/', action: onBeforeEnterAction('x-home-view', onBeforeEnter) },
{ path: '/users', component: 'x-users-list' },
],
true,
);

await router.render('/');
expect(outlet.children[0].tagName).to.match(/x-home-view/iu);
verifyActiveRoutes(router, ['/']);
}),
);
// eslint-disable-next-line no-await-in-loop
await router.render('/');
expect(outlet.children[0].tagName).to.match(/x-home-view/iu);
verifyActiveRoutes(router, ['/']);
}
});

it('should support returning a promise (and continue the resolve pass after the promise resolves)', async () => {
Expand Down Expand Up @@ -466,39 +469,43 @@ describe('Vaadin Router lifecycle events', () => {
document.createElement('div'),
];

await Promise.all(
values.map(async (value) => {
const onBeforeLeave = sinon.stub().returns(value);
await router.setRoutes([
{ path: '/', action: onBeforeLeaveAction('x-home-view', onBeforeLeave) },
{ path: '/users', component: 'x-users-list' },
]);

await router.render('/');
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}),
);
for (const value of values) {
const onBeforeLeave = sinon.stub().returns(value);
// eslint-disable-next-line no-await-in-loop
await router.setRoutes([
{ path: '/', action: onBeforeLeaveAction('x-home-view', onBeforeLeave) },
{ path: '/users', component: 'x-users-list' },
]);

// eslint-disable-next-line no-await-in-loop
await router.render('/');
// eslint-disable-next-line no-await-in-loop
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}
});

it('should support returning a promise (and continue the resolve pass after the promise resolves)', async () => {
await router.setRoutes([
{
path: '/a',
action: onBeforeLeaveAction(
'x-spy',
async () => {
callbacksLog.push('a.onBeforeLeave');
await sleep(100);
callbacksLog.push('a.onBeforeLeave.promise');
return undefined;
},
'a',
),
},
{ path: '/b', action: elementWithAllLifecycleCallbacks('b') },
]);
await router.setRoutes(
[
{
path: '/a',
action: onBeforeLeaveAction(
'x-spy',
async () => {
callbacksLog.push('a.onBeforeLeave');
await sleep(100);
callbacksLog.push('a.onBeforeLeave.promise');
return undefined;
},
'a',
),
},
{ path: '/b', action: elementWithAllLifecycleCallbacks('b') },
],
true,
);

await router.render('/').catch(() => {});
await router.render('/a');
Expand Down Expand Up @@ -619,20 +626,21 @@ describe('Vaadin Router lifecycle events', () => {
document.createElement('div'),
];

await Promise.all(
values.map(async (value) => {
const onAfterLeave = sinon.stub().returns(value);
await router.setRoutes([
{ path: '/', action: onAfterLeaveAction('x-home-view', onAfterLeave) },
{ path: '/users', component: 'x-users-list' },
]);

await router.render('/');
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}),
);
for (const value of values) {
const onAfterLeave = sinon.stub().returns(value);
// eslint-disable-next-line no-await-in-loop
await router.setRoutes([
{path: '/', action: onAfterLeaveAction('x-home-view', onAfterLeave)},
{path: '/users', component: 'x-users-list'},
]);

// eslint-disable-next-line no-await-in-loop
await router.render('/');
// eslint-disable-next-line no-await-in-loop
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}
});
});

Expand Down Expand Up @@ -692,23 +700,24 @@ describe('Vaadin Router lifecycle events', () => {
document.createElement('div'),
];

await Promise.all(
values.map(async (value) => {
const onAfterEnter = sinon.stub().returns(value);
await router.setRoutes(
[
{ path: '/', action: onAfterEnterAction('x-home-view', onAfterEnter) },
{ path: '/users', component: 'x-users-list' },
],
true,
);
for (const value of values) {
const onAfterEnter = sinon.stub().returns(value);
// eslint-disable-next-line no-await-in-loop
await router.setRoutes(
[
{ path: '/', action: onAfterEnterAction('x-home-view', onAfterEnter) },
{ path: '/users', component: 'x-users-list' },
],
true,
);

await router.render('/');
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}),
);
// eslint-disable-next-line no-await-in-loop
await router.render('/');
// eslint-disable-next-line no-await-in-loop
await router.render('/users');
expect(outlet.children[0].tagName).to.match(/x-users-list/iu);
verifyActiveRoutes(router, ['/users']);
}
});
});

Expand Down

0 comments on commit 63493c9

Please sign in to comment.