Angular 路由组件测试

Angular Routing Component Testing

我正在阅读来自 here 的 angular 路由测试的官方示例。我不明白 heroClick() 是如何触发点击的。

it('should tell ROUTER to navigate when hero clicked', () => {
  heroClick(); <------- how does this work !?  // trigger click on first inner <div class="hero">

  // args passed to router.navigateByUrl() spy
  const spy = router.navigateByUrl as jasmine.Spy;
  const navArgs = spy.calls.first().args[0];

  // expecting to navigate to id of the component's first hero
  const id = comp.heroes[0].id;
  expect(navArgs).toBe('/heroes/' + id, 'should nav to HeroDetail for first hero');
});

这是一个 link 到 stackblitz 示例

heroClick 是第 84 行中传递给 tests() 函数的参数。它是一个不带任何参数且 returns 不带任何参数的函数。在第 120 行,heroClick() 调用传递给 tests().

的任何内容

在第 27 和 48 行调用 tests(),传递不同的函数 clickForShallowclickForDeep,定义在它们的用法下方。这些函数通过与组件中的元素交互来模拟点击。