Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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'; |
| 17 | import { ActivatedRoute, Params } from '@angular/router'; |
| 18 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 19 | import { FormsModule } from '@angular/forms'; |
| 20 | import { DebugElement } from '@angular/core'; |
| 21 | import { By } from '@angular/platform-browser'; |
| 22 | import { MeterComponent } from './meter.component'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 23 | import { |
| 24 | FnService, |
| 25 | IconService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 26 | IconComponent, |
| 27 | LionService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 28 | LogService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 29 | TableFilterPipe, |
| 30 | ThemeService, |
| 31 | UrlFnService, |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 32 | WebSocketService, LoadingComponent |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 33 | } from 'gui2-fw-lib'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 34 | import { of, Subject } from 'rxjs'; |
| 35 | import { } from 'jasmine'; |
| 36 | import { RouterTestingModule } from '@angular/router/testing'; |
| 37 | |
| 38 | |
| 39 | class MockActivatedRoute extends ActivatedRoute { |
| 40 | constructor(params: Params) { |
| 41 | super(); |
| 42 | this.queryParams = of(params); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 47 | class MockFnService { } |
| 48 | |
| 49 | class MockIconService { |
| 50 | loadIconDef() { } |
| 51 | } |
| 52 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 53 | class MockThemeService { } |
| 54 | |
| 55 | class MockUrlFnService { } |
| 56 | |
| 57 | class MockWebSocketService { |
| 58 | createWebSocket() { } |
| 59 | isConnected() { return false; } |
| 60 | unbindHandlers() { } |
| 61 | bindHandlers() { } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * ONOS GUI -- Meter Panel View -- Unit Tests |
| 67 | */ |
| 68 | |
| 69 | describe('MeterComponent', () => { |
| 70 | |
| 71 | let fs: FnService; |
| 72 | let ar: MockActivatedRoute; |
| 73 | let windowMock: Window; |
| 74 | let logServiceSpy: jasmine.SpyObj<LogService>; |
| 75 | let component: MeterComponent; |
| 76 | let fixture: ComponentFixture<MeterComponent>; |
| 77 | |
| 78 | const bundleObj = { |
| 79 | 'core.view.Meter': { |
| 80 | test: 'test1' |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | const mockLion = (key) => { |
| 85 | return bundleObj[key] || '%' + key + '%'; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | |
| 90 | beforeEach(async(() => { |
| 91 | |
| 92 | const logSpy = jasmine.createSpyObj('LogService', ['info', 'debug', 'warn', 'error']); |
| 93 | ar = new MockActivatedRoute({ 'debug': 'txrx' }); |
| 94 | |
| 95 | windowMock = <any>{ |
| 96 | location: <any>{ |
| 97 | hostname: 'foo', |
| 98 | host: 'foo', |
| 99 | port: '80', |
| 100 | protocol: 'http', |
| 101 | search: { debug: 'true' }, |
| 102 | href: 'ws://foo:123/onos/ui/websock/path', |
| 103 | absUrl: 'ws://foo:123/onos/ui/websock/path' |
| 104 | } |
| 105 | }; |
| 106 | fs = new FnService(ar, logSpy, windowMock); |
| 107 | |
| 108 | TestBed.configureTestingModule({ |
| 109 | imports: [BrowserAnimationsModule, FormsModule, RouterTestingModule], |
Sean Condon | 95fb574 | 2019-04-02 12:16:55 +0100 | [diff] [blame] | 110 | declarations: [ |
| 111 | MeterComponent, |
| 112 | IconComponent, |
| 113 | TableFilterPipe, |
| 114 | LoadingComponent |
| 115 | ], |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 116 | providers: [ |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 117 | { provide: FnService, useValue: fs }, |
| 118 | { provide: IconService, useClass: MockIconService }, |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 119 | { |
| 120 | provide: LionService, useFactory: (() => { |
| 121 | return { |
| 122 | bundle: ((bundleId) => mockLion), |
| 123 | ubercache: new Array(), |
| 124 | loadCbs: new Map<string, () => void>([]) |
| 125 | }; |
| 126 | }) |
| 127 | }, |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 128 | { provide: LogService, useValue: logSpy }, |
| 129 | { provide: ThemeService, useClass: MockThemeService }, |
| 130 | { provide: UrlFnService, useClass: MockUrlFnService }, |
| 131 | { provide: WebSocketService, useClass: MockWebSocketService }, |
| 132 | { provide: 'Window', useValue: windowMock }, |
| 133 | ] |
| 134 | }) |
| 135 | .compileComponents(); |
| 136 | logServiceSpy = TestBed.get(LogService); |
| 137 | })); |
| 138 | |
| 139 | beforeEach(() => { |
| 140 | fixture = TestBed.createComponent(MeterComponent); |
| 141 | component = fixture.componentInstance; |
| 142 | fixture.detectChanges(); |
| 143 | }); |
| 144 | |
| 145 | it('should create', () => { |
| 146 | expect(component).toBeTruthy(); |
| 147 | }); |
| 148 | |
| 149 | |
| 150 | it('should have a div.tabular-header inside a div#ov-meter', () => { |
| 151 | const metDe: DebugElement = fixture.debugElement; |
| 152 | const divDe = metDe.query(By.css('div#ov-meter div.tabular-header')); |
| 153 | expect(divDe).toBeTruthy(); |
| 154 | }); |
| 155 | |
| 156 | it('should have a h2 inside the div.tabular-header', () => { |
| 157 | const metDe: DebugElement = fixture.debugElement; |
| 158 | const divDe = metDe.query(By.css('div#ov-meter div.tabular-header h2')); |
| 159 | const div: HTMLElement = divDe.nativeElement; |
| 160 | expect(div.textContent).toEqual(' Meter for Device (0 Total )'); |
| 161 | }); |
| 162 | |
| 163 | it('should have a refresh button inside the div.tabular-header', () => { |
| 164 | const metDe: DebugElement = fixture.debugElement; |
| 165 | const divDe = metDe.query(By.css('div#ov-meter div.tabular-header div.ctrl-btns div.refresh')); |
| 166 | expect(divDe).toBeTruthy(); |
| 167 | }); |
| 168 | |
| 169 | |
| 170 | it('should have a div.summary-list inside a div#ov-meter', () => { |
| 171 | const hostDe: DebugElement = fixture.debugElement; |
| 172 | const divDe = hostDe.query(By.css('div#ov-meter div.summary-list')); |
| 173 | expect(divDe).toBeTruthy(); |
| 174 | }); |
| 175 | |
| 176 | it('should have a div.table-header inside a div.summary-list inside a div#ov-meter', () => { |
| 177 | const hostDe: DebugElement = fixture.debugElement; |
| 178 | const divDe = hostDe.query(By.css('div#ov-meter div.summary-list div.table-header')); |
| 179 | expect(divDe).toBeTruthy(); |
| 180 | }); |
| 181 | |
| 182 | it('should have a div.table-body inside a div.summary-list inside a div#ov-meter', () => { |
| 183 | const hostDe: DebugElement = fixture.debugElement; |
| 184 | const divDe = hostDe.query(By.css('div#ov-meter div.summary-list div.table-body')); |
| 185 | expect(divDe).toBeTruthy(); |
| 186 | }); |
| 187 | }); |