Skip to content

Commit

Permalink
add tests with async loaded substates. WIP fixing emberjs#19266
Browse files Browse the repository at this point in the history
  • Loading branch information
sly7-7 committed Mar 10, 2021
1 parent dfd38c8 commit cf582e6
Show file tree
Hide file tree
Showing 5 changed files with 1,105 additions and 1,084 deletions.
12 changes: 9 additions & 3 deletions packages/@ember/-internals/routing/lib/system/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,15 @@ function findRouteStateName(route: Route, state: string) {
return routeHasBeenDefined(owner, router, stateName, stateNameFull) ? stateNameFull : '';
}

function isPromise(p: unknown): boolean {
return p !== null && typeof p === 'object' && typeof p.then === 'function';
}

/**
Determines whether or not a route has been defined by checking that the route
is in the Router's map and the owner has a registration for that route.
Determines whether or not a route has been defined by checking
- that the route is in the Router's map and
- the owner has a registration for that route and
- it has been fully resolved (think of aync assets loading)
@private
@param {Owner} owner
Expand All @@ -1412,7 +1418,7 @@ function routeHasBeenDefined(owner: Owner, router: any, localName: string, fullN
let routerHasRoute = router.hasRoute(fullName);
let ownerHasRoute =
owner.hasRegistration(`template:${localName}`) || owner.hasRegistration(`route:${localName}`);
return routerHasRoute && ownerHasRoute;
return routerHasRoute && ownerHasRoute && !isPromise(router._routerMicrolib.getRoute(fullName));
}

export function triggerEvent(
Expand Down
54 changes: 8 additions & 46 deletions packages/ember/tests/routing/model_loading_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
import { Route } from '@ember/-internals/routing';
import Controller from '@ember/controller';
import { Object as EmberObject, A as emberA } from '@ember/-internals/runtime';
import { moduleFor, ApplicationTestCase, getTextOf, runLoopSettled } from 'internal-test-helpers';
import {
moduleFor,
ApplicationTestCase,
getTextOf,
runLoopSettled,
lazyLoadingRouterOptions,
} from 'internal-test-helpers';
import { run } from '@ember/runloop';
import { computed, set } from '@ember/-internals/metal';
import { isDestroying } from '@glimmer/destroyable';
import RSVP from 'rsvp';

let originalConsoleError;

Expand Down Expand Up @@ -958,49 +962,7 @@ moduleFor(
'Route - model loading (simulated within lazy engine)',
class extends LoadingTests {
get routerOptions() {
return {
location: 'none',
setupRouter() {
this._super(...arguments);
let getRoute = this._routerMicrolib.getRoute;
this._enginePromises = Object.create(null);
this._resolvedEngines = Object.create(null);

let routes = new Map();
let routePromises = new Map();
this._routerMicrolib.getRoute = (name) => {
if (routes.has(name)) {
return routes.get(name);
}

if (routePromises.has(name)) {
return routePromises.get(name);
}

// if (name.indexOf('loading')) {
// let route = getRoute(name);
// routes.set(name, route);
// return route;
// }

let promise = new RSVP.Promise((resolve) => {
setTimeout(() => {
if (isDestroying(this)) {
return;
}

let route = getRoute(name);

routes.set(name, route);
resolve(route);
}, 10);
});
routePromises.set(name, promise);

return promise;
};
},
};
return lazyLoadingRouterOptions;
}
}
);
Loading

0 comments on commit cf582e6

Please sign in to comment.