Adding aliases for imports
[portal/nonrtric-controlpanel.git] / webapp-frontend / src / app / app.component.spec.ts
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
21 import { TestBed, async, ComponentFixture } from '@angular/core/testing';
22 import { RouterTestingModule } from '@angular/router/testing';
23 import { CookieService } from 'ngx-cookie-service';
24 import { AppComponent } from './app.component';
25 import { UiService } from '@services/ui/ui.service';
26
27 describe('AppComponent', () => {
28   let fixture: ComponentFixture<AppComponent>;
29   let app: AppComponent;
30   let cookieServiceSpy: any;
31   let uiService: UiService;
32
33   beforeEach(async(() => {
34     cookieServiceSpy = jasmine.createSpyObj('CookieService', [ 'get', 'set' ]);
35     TestBed.configureTestingModule({
36       imports: [
37         RouterTestingModule
38       ],
39       schemas: [
40         CUSTOM_ELEMENTS_SCHEMA
41       ],
42       declarations: [
43         AppComponent
44       ],
45       providers: [
46         { provide: CookieService, useValue: cookieServiceSpy },
47         UiService
48       ]
49     }).compileComponents();
50   }));
51
52   beforeEach(() => {
53     fixture = TestBed.createComponent(AppComponent);
54     app = fixture.componentInstance;
55     uiService = TestBed.inject(UiService);
56     fixture.detectChanges();
57   });
58
59   it('should create the app', () => {
60     expect(app).toBeTruthy();
61   });
62
63   describe('#content', () => {
64     it('should contain oran logo', async(() => {
65       const ele = fixture.debugElement.nativeElement.querySelector('img');
66       expect(ele.src).toContain('assets/oran-logo.png');
67     }));
68
69     it('should contain navigation menu', async(() => {
70       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-sidenav-list');
71       expect(ele).toBeTruthy();
72     }));
73
74     it('should contain dark mode selector', async(() => {
75       const ele = fixture.debugElement.nativeElement.querySelector('#darkModeSwitch');
76       expect(ele).toBeTruthy();
77     }));
78
79     it('should contain heading', async(() => {
80       const ele = fixture.debugElement.nativeElement.querySelector('tspan');
81       expect(ele.textContent.trim()).toBe('Non-RT RIC Control Panel');
82     }));
83
84     it('should contain router-outlet', async(() => {
85       const ele = fixture.debugElement.nativeElement.querySelector('router-outlet');
86       expect(ele).toBeTruthy();
87     }));
88
89     it('should contain nrcp-footer', async(() => {
90       const ele = fixture.debugElement.nativeElement.querySelector('nrcp-footer');
91       expect(ele).toBeTruthy();
92     }));
93   });
94
95   describe('#darkMode', () => {
96     it('when dark mode value yes from cookie at start should set mode to dark', () => {
97       uiService.darkModeState.next(false); // Set internal state to light mode.
98       cookieServiceSpy.get.and.returnValue('yes'); // Cookie shall return dark mode used.
99
100       app.ngOnInit();
101
102       expect(uiService.getDarkMode()).toBeTruthy();
103       expect(app.darkMode).toBeTruthy();
104       expect(cookieServiceSpy.get).toHaveBeenCalled();
105     });
106
107     it('should toggle dark mode when selector is clicked', () => {
108       const darkModeSelector = fixture.debugElement.nativeElement.querySelector('#darkModeSwitch');
109
110       // Toggle to light mode
111       darkModeSelector.click();
112       expect(uiService.getDarkMode()).toBeFalsy();
113       expect(cookieServiceSpy.set).toHaveBeenCalledWith('darkMode', 'no');
114
115
116       // Toggle to dark mode
117       darkModeSelector.click();
118       expect(uiService.getDarkMode()).toBeTruthy();
119       expect(cookieServiceSpy.set).toHaveBeenCalledWith('darkMode', 'yes');
120     });
121   });
122 });