测试除了通过ConfigureRouter的路线。 我们如何看NavModel?(Test th

2019-09-26 17:28发布

在下面的试验中, router.navigation属性具有零长度。 我们如何进入我们的路线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();
        });
    });
});

编写router的控制台显示此:

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: ...
        }
    }
}

Answer 1:

我想,你只需要在添加nav: true属性,你中映射路线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...
  });


文章来源: Test the addition of a route via ConfigureRouter. How do we see the NavModel?