Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
jest.mock('app/login/login.service');
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import { NgxWebstorageModule } from 'ngx-webstorage';
import { TranslateModule } from '@ngx-translate/core';
import { ProfileInfo } from 'app/layouts/profiles/profile-info.model';
import { Account } from 'app/core/auth/account.model';
import { AccountService } from 'app/core/auth/account.service';
import { ProfileService } from 'app/layouts/profiles/profile.service';
import { LoginService } from 'app/login/login.service';
import { NavbarComponent } from './navbar.component';
describe('Navbar Component', () => {
let comp: NavbarComponent;
let fixture: ComponentFixture<NavbarComponent>;
let accountService: AccountService;
let profileService: ProfileService;
const account: Account = {
activated: true,
authorities: [],
email: '',
firstName: 'John',
langKey: '',
lastName: 'Doe',
login: 'john.doe',
imageUrl: '',
};
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes([]), TranslateModule.forRoot(), NgxWebstorageModule.forRoot()],
declarations: [NavbarComponent],
providers: [LoginService],
})
.overrideTemplate(NavbarComponent, '')
.compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(NavbarComponent);
comp = fixture.componentInstance;
accountService = TestBed.inject(AccountService);
profileService = TestBed.inject(ProfileService);
});
it('Should call profileService.getProfileInfo on init', () => {
// GIVEN
jest.spyOn(profileService, 'getProfileInfo').mockReturnValue(of(new ProfileInfo()));
// WHEN
comp.ngOnInit();
// THEN
expect(profileService.getProfileInfo).toHaveBeenCalled();
});
it('Should hold current authenticated user in variable account', () => {
// WHEN
comp.ngOnInit();
// THEN
expect(comp.account).toBeNull();
// WHEN
accountService.authenticate(account);
// THEN
expect(comp.account).toEqual(account);
// WHEN
accountService.authenticate(null);
// THEN
expect(comp.account).toBeNull();
});
it('Should hold current authenticated user in variable account if user is authenticated before page load', () => {
// GIVEN
accountService.authenticate(account);
// WHEN
comp.ngOnInit();
// THEN
expect(comp.account).toEqual(account);
// WHEN
accountService.authenticate(null);
// THEN
expect(comp.account).toBeNull();
});
});