|
1 | | -import { NSRouterLink } from '@nativescript/angular'; |
2 | | -import { ActivatedRoute, Router } from '@angular/router'; |
| 1 | +import { NSRouterLink, NativeScriptRouterModule } from '@nativescript/angular'; |
3 | 2 | import { RouterExtensions } from '@nativescript/angular'; |
4 | | -import { fake, spy, stub } from './test-config.spec'; |
5 | | -import { SinonStub } from 'sinon'; |
6 | | -import { Label } from '@nativescript/core'; |
| 3 | +import { fake } from './test-config.spec'; |
| 4 | +import { Component, ViewChild } from '@angular/core'; |
| 5 | +import { TestBed, ComponentFixture } from '@angular/core/testing'; |
| 6 | +import { NativeScriptModule } from '@nativescript/angular'; |
7 | 7 |
|
8 | | -describe('NSRouterLink', () => { |
9 | | - const mockRouter = {} as Router; |
10 | | - const mockRouterExtensions = { |
11 | | - navigateByUrl: fake(), |
12 | | - navigate: fake(), |
13 | | - }; |
14 | | - const mockActivatedRoute = {} as ActivatedRoute; |
15 | | - let nsRouterLink: NSRouterLink; |
16 | | - let urlTreeStub: SinonStub; |
| 8 | +@Component({ |
| 9 | + imports: [NativeScriptRouterModule, NSRouterLink], |
| 10 | + template: `<Label nsRouterLink="/test" text="Test"></Label>`, |
| 11 | +}) |
| 12 | +class RouterLinkTestComponent { |
| 13 | + @ViewChild(NSRouterLink, { static: false }) nsRouterLink: NSRouterLink; |
| 14 | +} |
17 | 15 |
|
18 | | - beforeEach(() => { |
19 | | - const el = { |
20 | | - nativeElement: new Label(), |
21 | | - }; |
22 | | - nsRouterLink = new NSRouterLink(null, mockRouter, mockRouterExtensions as unknown as RouterExtensions, mockActivatedRoute, el); |
23 | | - urlTreeStub = stub(nsRouterLink, 'urlTree').get(() => null); |
24 | | - }); |
| 16 | +describe('NSRouterLink', () => { |
| 17 | + let mockNavigate: ReturnType<typeof fake>; |
| 18 | + let fixture: ComponentFixture<RouterLinkTestComponent>; |
25 | 19 |
|
26 | | - afterEach(() => { |
27 | | - urlTreeStub.restore(); |
| 20 | + beforeEach(async () => { |
| 21 | + mockNavigate = fake(); |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + imports: [ |
| 24 | + NativeScriptModule, |
| 25 | + NativeScriptRouterModule.forRoot([{ path: 'test', component: RouterLinkTestComponent }]), |
| 26 | + RouterLinkTestComponent, |
| 27 | + ], |
| 28 | + providers: [ |
| 29 | + { |
| 30 | + provide: RouterExtensions, |
| 31 | + useValue: { |
| 32 | + navigateByUrl: fake(), |
| 33 | + navigate: mockNavigate, |
| 34 | + }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }); |
| 38 | + await TestBed.compileComponents(); |
| 39 | + fixture = TestBed.createComponent(RouterLinkTestComponent); |
| 40 | + fixture.detectChanges(); |
| 41 | + await fixture.whenStable(); |
28 | 42 | }); |
29 | 43 |
|
30 | 44 | it('#tap should call navigate with undefined transition in extras when boolean is given for pageTransition input', () => { |
31 | | - nsRouterLink.pageTransition = false; |
32 | | - nsRouterLink.onTap(); |
33 | | - expect(mockRouterExtensions.navigate.lastCall.args[1].transition).toBeUndefined(); |
34 | | - // assert.isUndefined(mockRouterExtensions.navigateByUrl.lastCall.args[1].transition); |
| 45 | + const directive = fixture.componentInstance.nsRouterLink; |
| 46 | + directive.pageTransition = false; |
| 47 | + directive['onTap'](); |
| 48 | + expect(mockNavigate.lastCall.args[1].transition).toBeUndefined(); |
35 | 49 | }); |
36 | 50 |
|
37 | 51 | it('#tap should call navigate with correct transition in extras when NavigationTransition object is given for pageTransition input', () => { |
38 | 52 | const pageTransition = { |
39 | 53 | name: 'slide', |
40 | 54 | duration: 500, |
41 | 55 | }; |
42 | | - nsRouterLink.pageTransition = pageTransition; |
43 | | - stub(nsRouterLink, 'urlTree').get(() => null); |
44 | | - nsRouterLink.onTap(); |
45 | | - expect(mockRouterExtensions.navigate.lastCall.args[1].transition).toBe(pageTransition); |
| 56 | + const directive = fixture.componentInstance.nsRouterLink; |
| 57 | + directive.pageTransition = pageTransition; |
| 58 | + directive['onTap'](); |
| 59 | + expect(mockNavigate.lastCall.args[1].transition).toBe(pageTransition); |
46 | 60 | }); |
47 | 61 | }); |
0 commit comments