如何在组件中测试this.router.navigateToRoute()

问题描述 投票:1回答:1

我如何测试this.router.navigateToRoute(“ home”)?

在运行测试中,我不断得到。错误:找不到名称为'home'的路由。检查是否在路由的配置中指定了name: 'home'

我的测试文件

import { StageComponent } from "aurelia-testing";
import { bootstrap } from "aurelia-bootstrapper";
import { PLATFORM } from "aurelia-pal";
import { Container } from "aurelia-dependency-injection";
import { UserService } from "../../src/shared/services/user-service";
import { RouterConfiguration, Router } from "aurelia-router";

describe("MyComponent with sign up", () => {
  let component;

  beforeEach(() => {
    component = new Container();
    component.get(UserService);
    component.get(Router);
    component.get(RouterConfiguration);
    component = StageComponent.withResources(
      PLATFORM.moduleName("components/auth/auth-component")
    )
      .inView(
        '<auth-component type.bind="type" email.bind="email" password.bind="password"></auth-component>'
      )
      .boundTo({ type: "login", email: "t@1.com", password: "qqqq" });
  });

  it("should render Login", done => {
    component
      .create(bootstrap)
      .then(() => {
        const routeConfig = { name: "login" };
        component.viewModel.activate("", routeConfig);
        component.viewModel.determineActivationStrategy();
        const nameElement = document.querySelector(".LogTest");
        component.viewModel.submit();
        expect(nameElement.innerHTML).toBe("Sign in");
        done();
      })
      .catch(e => {
        console.log(e.toString());
      });
  });

  afterEach(() => {
    component.dispose();
  });
});
aurelia
1个回答
0
投票

您的错误表示您尚未在app.ts中注册名为home的路由,因此您需要先执行该操作,然后才能正常工作。但是,如果您只是想导航到这样的路由:https-:// [mydomain] / home,则只需要使用router.navigate而不是navigationToRoute。 NavigationToRoute函数用于通过初始配置向路由器注册的路由。

用于配置路由器:Router Config

导航至路线详细信息:navigateToRoute

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.