Skip to content

Commit 79ab8c4

Browse files
atscottthePunderWoman
authored andcommitted
docs: More updates to components scenarios guide (angular#57052)
PR Close angular#57052
1 parent 165973b commit 79ab8c4

File tree

3 files changed

+11
-33
lines changed

3 files changed

+11
-33
lines changed

adev/src/content/examples/testing/src/app/dashboard/dashboard.component.spec.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('DashboardComponent (shallow)', () => {
6565

6666
/** Add TestBed providers, compile, and create DashboardComponent */
6767
function compileAndCreate() {
68-
beforeEach(waitForAsync(() => {
68+
beforeEach(async () => {
6969
// #docregion router-harness
7070
TestBed.configureTestingModule(
7171
Object.assign({}, appConfig, {
@@ -77,15 +77,12 @@ function compileAndCreate() {
7777
HeroService,
7878
],
7979
}),
80-
)
81-
.compileComponents()
82-
.then(async () => {
83-
harness = await RouterTestingHarness.create();
84-
comp = await harness.navigateByUrl('/', DashboardComponent);
85-
TestBed.inject(HttpTestingController).expectOne('api/heroes').flush(getTestHeroes());
86-
});
80+
);
81+
harness = await RouterTestingHarness.create();
82+
comp = await harness.navigateByUrl('/', DashboardComponent);
83+
TestBed.inject(HttpTestingController).expectOne('api/heroes').flush(getTestHeroes());
8784
// #enddocregion router-harness
88-
}));
85+
});
8986
}
9087

9188
/**

adev/src/content/examples/testing/src/app/hero/hero-detail.component.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function overrideSetup() {
115115
expect(TestBed.inject(Router).url).toEqual('/heroes');
116116
}));
117117
}
118+
// #enddocregion override-tests
118119

119120
////////////////////
120121
import {getTestHeroes} from '../model/testing/test-hero.service';

adev/src/content/guide/testing/components-scenarios.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -515,21 +515,13 @@ Here's the previous test, rewritten using the click helper.
515515

516516
<docs-code header="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"/>
517517

518-
<!-- TODO(atscott): Guide above this line updated on 06/11/2024. Continue updating sections below. -->
519518
## Component inside a test host
520519

521520
The previous tests played the role of the host `DashboardComponent` themselves.
522521
But does the `DashboardHeroComponent` work correctly when properly data-bound to a host component?
523522

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:
528-
529523
<docs-code header="app/dashboard/dashboard-hero.component.spec.ts (test host)" path="adev/src/content/examples/testing/src/app/dashboard/dashboard-hero.component.spec.ts" visibleRegion="test-host"/>
530524

531-
This test host binds to `DashboardHeroComponent` as the `DashboardComponent` would but without the noise of the `Router`, the `HeroService`, or the `*ngFor` repeater.
532-
533525
The test host sets the component's `hero` input property with its test hero.
534526
It binds the component's `selected` event with its `onSelected` handler, which records the emitted hero in its `selectedHero` property.
535527

@@ -541,7 +533,7 @@ The setup for the `test-host` tests is similar to the setup for the stand-alone
541533

542534
This testing module configuration shows three important differences:
543535

544-
* It *declares* both the `DashboardHeroComponent` and the `TestHostComponent`
536+
* It *imports* both the `DashboardHeroComponent` and the `TestHostComponent`
545537
* It *creates* the `TestHostComponent` instead of the `DashboardHeroComponent`
546538
* The `TestHostComponent` sets the `DashboardHeroComponent.hero` with a binding
547539

@@ -562,14 +554,7 @@ It confirms that the selected `DashboardHeroComponent` hero really does find its
562554
A *routing component* is a component that tells the `Router` to navigate to another component.
563555
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.
564556

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`.
567-
568-
<docs-code header="app/dashboard/dashboard.component.ts (constructor)" path="adev/src/content/examples/testing/src/app/dashboard/dashboard.component.ts" visibleRegion="ctor"/>
569-
570-
<docs-code header="app/dashboard/dashboard.component.ts (goToDetail)" path="adev/src/content/examples/testing/src/app/dashboard/dashboard.component.ts" visibleRegion="goto-detail" />
571-
572-
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.
573558

574559
<docs-code header="app/dashboard/dashboard.component.spec.ts" path="adev/src/content/examples/testing/src/app/dashboard/dashboard.component.spec.ts" visibleRegion="router-harness"/>
575560

@@ -624,13 +609,13 @@ This test expects the component to try to navigate to the `HeroListComponent`.
624609

625610
Component templates often have nested components, whose templates might contain more components.
626611

627-
The component tree can be very deep and, most of the time, the nested components play no role in testing the component at the top of the tree.
612+
The component tree can be very deep and sometimes the nested components play no role in testing the component at the top of the tree.
628613

629614
The `AppComponent`, for example, displays a navigation bar with anchors and their `RouterLink` directives.
630615

631616
<docs-code header="app/app.component.html" path="adev/src/content/examples/testing/src/app/app.component.html"/>
632617

633-
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*.
634619

635620
The `BannerComponent` and `WelcomeComponent` \(indicated by `<app-banner>` and `<app-welcome>`\) are also irrelevant.
636621

@@ -640,8 +625,6 @@ If you neglect to declare them, the Angular compiler won't recognize the `<app-b
640625

641626
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.
642627

643-
That's too much effort just to answer a few simple questions about links.
644-
645628
This section describes two techniques for minimizing the setup.
646629
Use them, alone or in combination, to stay focused on testing the primary component.
647630

@@ -719,8 +702,6 @@ Here are some tests that confirm those links are wired to the `routerLink` direc
719702

720703
The `HeroDetailComponent` is a simple view with a title, two hero fields, and two buttons.
721704

722-
<img alt="HeroDetailComponent in action" src="assets/images/guide/testing/hero-detail.component.png">
723-
724705
But there's plenty of template complexity even in this simple form.
725706

726707
<docs-code
@@ -732,7 +713,6 @@ Tests that exercise the component need …
732713
* A reference to the title text
733714
* A reference to the name input box to inspect and set it
734715
* References to the two buttons so they can click them
735-
* Spies for some of the component and router methods
736716

737717
Even a small form such as this one can produce a mess of tortured conditional setup and CSS element selection.
738718

0 commit comments

Comments
 (0)