You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adev/src/content/guide/testing/components-scenarios.md
+4-24Lines changed: 4 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -515,21 +515,13 @@ Here's the previous test, rewritten using the click helper.
515
515
516
516
<docs-codeheader="app/dashboard/dashboard-hero.component.spec.ts (test with click helper)"path="adev/src/content/examples/testing/src/app/dashboard/dashboard-hero.component.spec.ts"visibleRegion="click-test-3"/>
517
517
518
-
<!-- TODO(atscott): Guide above this line updated on 06/11/2024. Continue updating sections below. -->
519
518
## Component inside a test host
520
519
521
520
The previous tests played the role of the host `DashboardComponent` themselves.
522
521
But does the `DashboardHeroComponent` work correctly when properly data-bound to a host component?
523
522
524
-
You could test with the actual `DashboardComponent`.
525
-
But doing so could require a lot of setup, especially when its template features an `*ngFor` repeater, other components, layout HTML, additional bindings, a constructor that injects multiple services, and it starts interacting with those services right away.
526
-
527
-
Imagine the effort to disable these distractions, just to prove a point that can be made satisfactorily with a *test host* like this one:
This test host binds to `DashboardHeroComponent` as the `DashboardComponent` would but without the noise of the `Router`, the `HeroService`, or the `*ngFor` repeater.
532
-
533
525
The test host sets the component's `hero` input property with its test hero.
534
526
It binds the component's `selected` event with its `onSelected` handler, which records the emitted hero in its `selectedHero` property.
535
527
@@ -541,7 +533,7 @@ The setup for the `test-host` tests is similar to the setup for the stand-alone
541
533
542
534
This testing module configuration shows three important differences:
543
535
544
-
* It *declares* both the `DashboardHeroComponent` and the `TestHostComponent`
536
+
* It *imports* both the `DashboardHeroComponent` and the `TestHostComponent`
545
537
* It *creates* the `TestHostComponent` instead of the `DashboardHeroComponent`
546
538
* The `TestHostComponent` sets the `DashboardHeroComponent.hero` with a binding
547
539
@@ -562,14 +554,7 @@ It confirms that the selected `DashboardHeroComponent` hero really does find its
562
554
A *routing component* is a component that tells the `Router` to navigate to another component.
563
555
The `DashboardComponent` is a *routing component* because the user can navigate to the `HeroDetailComponent` by clicking on one of the *hero buttons* on the dashboard.
564
556
565
-
Routing is pretty complicated.
566
-
Testing the `DashboardComponent` seemed daunting in part because it involves the `Router`, which it injects together with the `HeroService`.
Angular provides test helpers to reduce boilerplate and more effectively test code which depends on the Router and HttpClient.
557
+
Angular provides test helpers to reduce boilerplate and more effectively test code which depends HttpClient. The `provideRouter` function can be used directly in the test module as well.
To validate the links, you don't need the `Router` to navigate and you don't need the `<router-outlet>` to mark where the `Router` inserts *routed components*.
618
+
To validate the links but not the navigation, you don't need the `Router` to navigate and you don't need the `<router-outlet>` to mark where the `Router` inserts *routed components*.
634
619
635
620
The `BannerComponent` and `WelcomeComponent`\(indicated by `<app-banner>` and `<app-welcome>`\) are also irrelevant.
636
621
@@ -640,8 +625,6 @@ If you neglect to declare them, the Angular compiler won't recognize the `<app-b
640
625
641
626
If you declare the real components, you'll also have to declare *their* nested components and provide for *all* services injected in *any* component in the tree.
642
627
643
-
That's too much effort just to answer a few simple questions about links.
644
-
645
628
This section describes two techniques for minimizing the setup.
646
629
Use them, alone or in combination, to stay focused on testing the primary component.
647
630
@@ -719,8 +702,6 @@ Here are some tests that confirm those links are wired to the `routerLink` direc
719
702
720
703
The `HeroDetailComponent` is a simple view with a title, two hero fields, and two buttons.
721
704
722
-
<imgalt="HeroDetailComponent in action"src="assets/images/guide/testing/hero-detail.component.png">
723
-
724
705
But there's plenty of template complexity even in this simple form.
725
706
726
707
<docs-code
@@ -732,7 +713,6 @@ Tests that exercise the component need …
732
713
* A reference to the title text
733
714
* A reference to the name input box to inspect and set it
734
715
* References to the two buttons so they can click them
735
-
* Spies for some of the component and router methods
736
716
737
717
Even a small form such as this one can produce a mess of tortured conditional setup and CSS element selection.
0 commit comments