Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 17 | import { RouterModule, ActivatedRoute, Params } from '@angular/router'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 18 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 19 | import { DebugElement } from '@angular/core'; |
| 20 | import { By } from '@angular/platform-browser'; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 21 | import { of, from } from 'rxjs'; |
| 22 | import { HttpClient, HttpErrorResponse } from '@angular/common/http'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 23 | |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 24 | import { |
| 25 | ConsoleLoggerService, |
| 26 | FnService, |
| 27 | IconComponent, |
| 28 | IconService, |
| 29 | LionService, |
| 30 | LogService, |
| 31 | NavService } from 'gui2-fw-lib'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 32 | import { NavComponent } from './nav.component'; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 33 | |
| 34 | class MockActivatedRoute extends ActivatedRoute { |
| 35 | constructor(params: Params) { |
| 36 | super(); |
| 37 | this.queryParams = of(params); |
| 38 | } |
| 39 | } |
| 40 | |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 41 | class MockHttpClient { |
| 42 | get() { |
| 43 | return from(['{"id":"app","icon":"nav_apps","cat":"PLATFORM","label":"Applications"},' + |
| 44 | '{"id":"settings","icon":"nav_settings","cat":"PLATFORM","label":"Settings"}']); |
| 45 | } |
| 46 | |
| 47 | subscribe() {} |
| 48 | } |
| 49 | |
| 50 | class MockNavService { |
| 51 | uiPlatformViews = []; |
| 52 | uiNetworkViews = []; |
| 53 | uiOtherViews = []; |
| 54 | uiHiddenViews = []; |
| 55 | |
| 56 | getUiViews() {} |
| 57 | } |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 58 | |
| 59 | class MockIconService { |
| 60 | loadIconDef() {} |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * ONOS GUI -- Util -- Navigation Component - Unit Tests |
| 65 | */ |
| 66 | describe('NavComponent', () => { |
| 67 | let log: LogService; |
| 68 | let fs: FnService; |
| 69 | let ar: MockActivatedRoute; |
| 70 | let windowMock: Window; |
| 71 | let component: NavComponent; |
| 72 | let fixture: ComponentFixture<NavComponent>; |
| 73 | const bundleObj = { |
| 74 | 'core.view.App': { |
| 75 | test: 'test1' |
| 76 | } |
| 77 | }; |
| 78 | const mockLion = (key) => { |
| 79 | return bundleObj[key] || '%' + key + '%'; |
| 80 | }; |
| 81 | |
| 82 | beforeEach(async(() => { |
| 83 | log = new ConsoleLoggerService(); |
| 84 | ar = new MockActivatedRoute({'debug': 'txrx'}); |
| 85 | |
| 86 | windowMock = <any>{ |
| 87 | location: <any> { |
| 88 | hostname: 'foo', |
| 89 | host: 'foo', |
| 90 | port: '80', |
| 91 | protocol: 'http', |
| 92 | search: { debug: 'true'}, |
| 93 | href: 'ws://foo:123/onos/ui/websock/path', |
| 94 | absUrl: 'ws://foo:123/onos/ui/websock/path' |
| 95 | } |
| 96 | }; |
| 97 | fs = new FnService(ar, log, windowMock); |
| 98 | |
| 99 | TestBed.configureTestingModule({ |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 100 | imports: [ BrowserAnimationsModule, RouterModule ], |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 101 | declarations: [ NavComponent, IconComponent ], |
| 102 | providers: [ |
| 103 | { provide: FnService, useValue: fs }, |
| 104 | { provide: IconService, useClass: MockIconService }, |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 105 | { provide: HttpClient, useClass: MockHttpClient }, |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 106 | { provide: LionService, useFactory: (() => { |
| 107 | return { |
| 108 | bundle: ((bundleId) => mockLion), |
| 109 | ubercache: new Array(), |
| 110 | loadCbs: new Map<string, () => void>([]) |
| 111 | }; |
| 112 | }) |
| 113 | }, |
| 114 | { provide: LogService, useValue: log }, |
| 115 | { provide: NavService, useClass: MockNavService }, |
| 116 | { provide: 'Window', useValue: windowMock }, |
| 117 | ] |
| 118 | }) |
| 119 | .compileComponents(); |
| 120 | })); |
| 121 | |
| 122 | beforeEach(() => { |
| 123 | fixture = TestBed.createComponent(NavComponent); |
| 124 | component = fixture.debugElement.componentInstance; |
| 125 | fixture.detectChanges(); |
| 126 | }); |
| 127 | |
| 128 | it('should create', () => { |
| 129 | expect(component).toBeTruthy(); |
| 130 | }); |
| 131 | |
| 132 | it('should have a nav#nav', () => { |
| 133 | const appDe: DebugElement = fixture.debugElement; |
| 134 | const divDe = appDe.query(By.css('nav#nav')); |
| 135 | expect(divDe).toBeTruthy(); |
| 136 | }); |
| 137 | |
| 138 | it('should have a platform div.nav-hdr inside a nav#nav', () => { |
| 139 | const appDe: DebugElement = fixture.debugElement; |
Bhavesh Kumar | d0b8bae | 2018-07-31 16:56:43 +0530 | [diff] [blame] | 140 | const divDe = appDe.query(By.css('nav#nav div#platform.nav-hdr')); |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 141 | const div: HTMLElement = divDe.nativeElement; |
| 142 | expect(div.textContent).toEqual('%cat_platform%'); |
| 143 | }); |
| 144 | |
Bhavesh Kumar | d0b8bae | 2018-07-31 16:56:43 +0530 | [diff] [blame] | 145 | it('should have a network div.nav-hdr inside a nav#nav', () => { |
| 146 | const appDe: DebugElement = fixture.debugElement; |
| 147 | const divDe = appDe.query(By.css('nav#nav div#network.nav-hdr')); |
| 148 | const div: HTMLElement = divDe.nativeElement; |
| 149 | expect(div.textContent).toEqual('%cat_network%'); |
| 150 | }); |
| 151 | |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 152 | }); |