|
1 | 1 | import { inject, TestBed } from '@angular/core/testing';
|
2 | 2 | import { Account, UserAgentApplication } from 'msal';
|
3 | 3 | import { AzureAdB2CService } from './azure.ad.b2c.service';
|
4 |
| - |
| 4 | +import { CookieService } from 'ngx-cookie-service'; |
5 | 5 |
|
6 | 6 | describe('AzureAdB2CService', () => {
|
7 | 7 | let service: AzureAdB2CService;
|
8 |
| - |
| 8 | + let cookieService: CookieService; |
9 | 9 | let account: Account;
|
10 | 10 |
|
11 | 11 | beforeEach(() => {
|
12 | 12 | TestBed.configureTestingModule({
|
13 | 13 | imports: [],
|
14 | 14 | });
|
15 | 15 | service = TestBed.inject(AzureAdB2CService);
|
| 16 | + cookieService = TestBed.inject(CookieService); |
16 | 17 | account = {
|
17 | 18 | accountIdentifier: 'abc',
|
18 | 19 | homeAccountIdentifier: 'abc',
|
@@ -75,30 +76,47 @@ describe('AzureAdB2CService', () => {
|
75 | 76 | expect(isAdmin).toBeTruthy();
|
76 | 77 | });
|
77 | 78 |
|
78 |
| - it('isLogin returns true if UserAgentApplication has a defined Account', () => { |
| 79 | + it('isLogin returns true if UserAgentApplication has a defined Account and token cookie exist', () => { |
79 | 80 | spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
|
| 81 | + spyOn(cookieService, 'check').and.returnValue(true); |
80 | 82 |
|
81 | 83 | const isLogin = service.isLogin();
|
82 | 84 |
|
83 | 85 | expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
|
| 86 | + expect(cookieService.check).toHaveBeenCalled(); |
84 | 87 | expect(isLogin).toEqual(true);
|
85 | 88 | });
|
86 | 89 |
|
| 90 | + it('isLogin returns false if UserAgentApplication has a defined Account and token cookie does not exist', () => { |
| 91 | + spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account); |
| 92 | + spyOn(cookieService, 'check').and.returnValue(false); |
| 93 | + |
| 94 | + const isLogin = service.isLogin(); |
| 95 | + |
| 96 | + expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled(); |
| 97 | + expect(cookieService.check).toHaveBeenCalled(); |
| 98 | + expect(isLogin).toEqual(false); |
| 99 | + }); |
| 100 | + |
87 | 101 | it('isLogin returns false if UserAgentApplication has a null value for Account', () => {
|
88 | 102 | spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(null);
|
| 103 | + |
89 | 104 | const isLogin = service.isLogin();
|
| 105 | + |
90 | 106 | expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
|
91 | 107 | expect(isLogin).toEqual(false);
|
92 | 108 | });
|
93 | 109 |
|
94 | 110 | it('setTenantId should save a tenantId in local storage', () => {
|
95 | 111 | spyOn(UserAgentApplication.prototype, 'getAccount').and.returnValue(account);
|
| 112 | + spyOn(cookieService, 'check').and.returnValue(true); |
96 | 113 | spyOn(localStorage, 'setItem').withArgs('tenant_id', '12345');
|
97 | 114 |
|
98 | 115 | const isLogin = service.isLogin();
|
99 | 116 | service.setTenantId();
|
100 | 117 |
|
101 | 118 | expect(UserAgentApplication.prototype.getAccount).toHaveBeenCalled();
|
| 119 | + expect(cookieService.check).toHaveBeenCalled(); |
102 | 120 | expect(isLogin).toEqual(true);
|
103 | 121 | expect(localStorage.setItem).toHaveBeenCalledWith('tenant_id', '12345');
|
104 | 122 | });
|
|
0 commit comments