1
- import { TestBed , ComponentFixture , waitForAsync } from '@angular/core/testing' ;
2
- import { ActivatedRoute } from '@angular/router' ;
3
- import { RouterTestingModule } from '@angular/router/testing' ;
4
- import { RouterLinkStubDirective } from '../tests/router-link-directive-stub' ;
1
+ import { TestBed , waitForAsync , fakeAsync , tick } from '@angular/core/testing' ;
2
+ import { Router , RouterLink , provideRouter } from '@angular/router' ;
3
+ import { RouterTestingHarness } from '@angular/router/testing' ;
5
4
import { AppComponent } from './app.component' ;
6
- import { NO_ERRORS_SCHEMA } from '@angular/core' ;
7
5
import { By } from '@angular/platform-browser' ;
8
-
6
+ import { appConfig } from './app.config' ;
9
7
10
8
describe ( 'AppComponent' , ( ) => {
11
- let route : ActivatedRoute ;
12
9
let appComponent : AppComponent ;
13
- let app : ComponentFixture < AppComponent > ;
14
- let routerLinks : RouterLinkStubDirective [ ] ;
10
+ let harness : RouterTestingHarness ;
15
11
16
12
beforeEach ( waitForAsync ( ( ) => {
17
- TestBed . configureTestingModule ( {
18
- imports : [
19
- RouterTestingModule
20
- ] ,
21
- declarations : [
22
- AppComponent ,
23
- RouterLinkStubDirective
24
- ] ,
25
- schemas : [ NO_ERRORS_SCHEMA ] ,
26
- providers :
27
- [
28
- {
29
- provide : ActivatedRoute ,
30
- useValue : {
31
- snapshot : { data : { title : 'Angular - Reactive form input value cross-validation' } }
32
- }
33
- }
34
- ]
35
- } ) . compileComponents ( ) ;
36
-
37
- route = TestBed . inject ( ActivatedRoute ) ;
38
-
13
+ TestBed . configureTestingModule ( Object . assign ( { } , appConfig , {
14
+ imports : [ AppComponent ] ,
15
+ providers : [
16
+ provideRouter ( [ { path : '**' , component : AppComponent } ] )
17
+ ]
18
+ } ) )
19
+ . compileComponents ( )
20
+ . then ( async ( ) => {
21
+ harness = await RouterTestingHarness . create ( ) ;
22
+ appComponent = await harness . navigateByUrl ( '/' , AppComponent ) ;
23
+ harness . detectChanges ( ) ;
24
+ } ) ;
39
25
} ) ) ;
40
26
41
- beforeEach ( ( ) => {
42
- app = TestBed . createComponent ( AppComponent ) ;
43
- appComponent = app . componentInstance ;
44
- appComponent . title = route . snapshot . data [ 'title' ] ;
45
- app . detectChanges ( ) ;
46
-
47
- // Find DebugElements with an attached RouterLinkStubDirective
48
- const linkElms = app . debugElement . queryAll ( By . directive ( RouterLinkStubDirective ) ) ;
49
-
50
- // Using each DebugElement's injector
51
- routerLinks = linkElms . map ( el => el . injector . get ( RouterLinkStubDirective ) ) ;
27
+ it ( 'should create an instance of the app' , ( ) => {
28
+ expect ( appComponent ) . toBeInstanceOf ( AppComponent ) ;
52
29
} ) ;
53
30
54
- it ( 'Should create the app' , ( ) => {
55
- expect ( appComponent ) . toBeTruthy ( ) ;
31
+ it ( 'should get RouterLinks from template' , ( ) => {
32
+ const linkItems = harness . routeNativeElement ?. querySelectorAll ( 'a' ) as unknown as HTMLAnchorElement [ ] ;
33
+ expect ( linkItems . length ) . toBe ( 2 ) ;
34
+ expect ( linkItems [ 0 ] . getAttribute ( 'routerLink' ) ) . toBe ( '/reactive-form' ) ;
35
+ expect ( linkItems [ 1 ] . getAttribute ( 'routerLink' ) ) . toBe ( '/template-driven-form' ) ;
56
36
} ) ;
57
37
58
- it ( 'can get RouterLinks from template ' , ( ) => {
59
- expect ( routerLinks . length ) . toBe ( 2 ) ;
60
- expect ( routerLinks [ 0 ] . linkParams ) . toBe ( '/reactive-form' ) ;
61
- expect ( routerLinks [ 1 ] . linkParams ) . toBe ( '/template-driven-form' ) ;
62
- } ) ;
38
+ it ( 'should activate RouterLinks' , fakeAsync ( ( ) => {
39
+ const linkElms = harness . routeDebugElement ?. queryAll ( By . directive ( RouterLink ) ) ;
40
+ linkElms ! [ 0 ] . triggerEventHandler ( 'click' , { button : 0 , } ) ;
41
+ tick ( ) ;
42
+ expect ( TestBed . inject ( Router ) . url ) . toEqual ( '/reactive-form' ) ;
63
43
64
- it ( "Should have 'route.snapshot.data.title' as title" , ( ) => {
65
- expect ( appComponent . title ) . toEqual ( 'Angular - Reactive form input value cross-validation' ) ;
66
- } ) ;
44
+ linkElms ! [ 1 ] . triggerEventHandler ( 'click' , { button : 0 , } ) ;
45
+ tick ( ) ;
46
+ expect ( TestBed . inject ( Router ) . url ) . toEqual ( '/template-driven-form' ) ;
47
+ } ) ) ;
67
48
68
- it ( 'Should render title' , ( ) => {
69
- const compiled = app . nativeElement ;
70
- expect ( compiled . querySelector ( 'h1' ) . textContent ) . toContain ( 'Angular - Reactive form input value cross-validation' ) ;
49
+ it ( 'should render correct page heading' , ( ) => {
50
+ appComponent . pageHeading = 'Angular - Reactive form input value cross-validation' ;
51
+ harness . detectChanges ( ) ;
52
+ const heading = harness . routeNativeElement ?. querySelector ( 'h1' ) ;
53
+ expect ( heading ?. textContent ) . toContain ( 'Angular - Reactive form input value cross-validation' ) ;
71
54
} ) ;
72
- } ) ;
55
+ } ) ;
0 commit comments