Test the addition of a route via ConfigureRouter.

2019-07-30 00:46发布

问题:

In the following test, the router.navigation property has a length of zero. How do we access our routes NavModel?

import { Container } from "aurelia-framework";
import { RouterConfiguration, Router } from "aurelia-router";

describe("the RouterConfiguration", function () {

    let container: Container = new Container();
    let routerConfiguration: RouterConfiguration = container.get(RouterConfiguration);
    let router: Router = container.get(Router);

    it("adds a route with a NavModel", function (done) {

        routerConfiguration.mapRoute({
            moduleId: "test",
            name: "test",
            navigationStrategy: (instruction) => {
                instruction.config.moduleId = "something";
            },
            route: "test",
        });

        let configureRouter: Promise<void> = router.configure(routerConfiguration);

        configureRouter.then(function () {

            router.baseUrl = "foobar";
            router.refreshNavigation();

            expect(router.isConfigured).toBe(true);
            expect(router.routes.length).toBe(1);
            expect(router.navigation.length).toBe(1); // fails
            expect(router.hasRoute("test")).toBe(true);
            done();
        });
    });
});

Writing the router to the console displays this:

Router
{
    parent: null, 
    options: Object{}, 
    container: undefined, 
    history: undefined, 
    viewPorts: Object{}, 
    routes: [
        Object
        {
            moduleId: ..., 
            name: ..., 
            navigationStrategy: ..., 
            route: ..., 
            settings: ..., 
            navModel: ...
        }
    ], 
    baseUrl: '', 
    isConfigured: true, 
    isNavigating: false, 
    navigation: [], 
    currentInstruction: null, 
    _fallbackOrder: 100,
    _recognizer: RouteRecognizer
    {
        rootState: State {charSpec: ..., nextStates: ...}, 
        names: Object{test: ...}
    }, 
    _childRecognizer: RouteRecognizer
    {
        rootState: State{charSpec: ..., nextStates: ...}, 
        names: Object{test: ...}
    }, 
    _resolveConfiguredPromise: function (value) { ... },
    _configuredPromise: Promise
    {
        _bitField: 33554432, 
        _fulfillmentHandler0: undefined, 
        _rejectionHandler0: undefined, 
        _promise0: undefined, 
        _receiver0: undefined, 
        _trace: CapturedTrace
        {
            _parent: ..., 
            _promisesCreated: ..., 
            _length: ..., 
            _promiseCreated: ...
        }
    }
}

回答1:

I think you just need to add the nav: true property to the route you are mapping in mapRoute.

routerConfiguration.mapRoute({
    moduleId: "test",
    name: "test",
    navigationStrategy: (instruction) => {
      instruction.config.moduleId = "something";
    },
    route: "test",
    nav: true // I want the route to be included in the nav model...
  });