|
| 1 | +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { SocialAuthService } from 'angularx-social-login'; |
| 4 | +import { CookieService } from 'ngx-cookie-service'; |
| 5 | +import { of } from 'rxjs'; |
| 6 | + |
| 7 | +import { LoginService } from './login.service'; |
| 8 | + |
| 9 | +describe('LoginService', () => { |
| 10 | + let service: LoginService; |
| 11 | + let httpMock: HttpTestingController; |
| 12 | + let cookieService: CookieService; |
| 13 | + let socialAuthService: SocialAuthService; |
| 14 | + let account; |
| 15 | + const socialAuthServiceStub = jasmine.createSpyObj('SocialAuthService', ['signOut', 'signIn']); |
| 16 | + const cookieStoreStub = {}; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + TestBed.configureTestingModule({ |
| 20 | + imports: [HttpClientTestingModule], |
| 21 | + providers: [ |
| 22 | + { providers: CookieService, useValue: cookieStoreStub }, |
| 23 | + { provide: SocialAuthService, useValue: socialAuthServiceStub }, |
| 24 | + ], |
| 25 | + }); |
| 26 | + service = TestBed.inject(LoginService); |
| 27 | + cookieService = TestBed.inject(CookieService); |
| 28 | + httpMock = TestBed.inject(HttpTestingController); |
| 29 | + socialAuthService = TestBed.inject(SocialAuthService); |
| 30 | + account = { |
| 31 | + id: 'abc', |
| 32 | + name: 'abc', |
| 33 | + email: 'abc', |
| 34 | + groups: ['abc'], |
| 35 | + }; |
| 36 | + let store = {}; |
| 37 | + const mockLocalStorage = { |
| 38 | + getItem: (key: string): string => { |
| 39 | + return key in store ? store[key] : null; |
| 40 | + }, |
| 41 | + setItem: (key: string, value: string) => { |
| 42 | + store[key] = `${value}`; |
| 43 | + }, |
| 44 | + clear: () => { |
| 45 | + store = {}; |
| 46 | + }, |
| 47 | + }; |
| 48 | + spyOn(localStorage, 'getItem').and.callFake(mockLocalStorage.getItem); |
| 49 | + spyOn(localStorage, 'setItem').and.callFake(mockLocalStorage.setItem); |
| 50 | + spyOn(localStorage, 'clear').and.callFake(mockLocalStorage.clear); |
| 51 | + localStorage.setItem('user2', JSON.stringify(account)); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should be created', () => { |
| 55 | + expect(service).toBeTruthy(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should get name from localStorage', () => { |
| 59 | + const name = service.getName(); |
| 60 | + |
| 61 | + expect(name).toEqual(account.name); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should get userId from localStorage', () => { |
| 65 | + const userId = service.getUserId(); |
| 66 | + |
| 67 | + expect(userId).toEqual(account.id); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should get UserGroup from localStorage', () => { |
| 71 | + const userGroup = service.getUserGroup(); |
| 72 | + |
| 73 | + expect(userGroup).toEqual(account.groups); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should get BearerToken from localStorage', () => { |
| 77 | + localStorage.setItem('idToken', 'token'); |
| 78 | + |
| 79 | + const bearerToken = service.getBearerToken(); |
| 80 | + |
| 81 | + expect(bearerToken).toEqual('token'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should set key and value in localStorage', () => { |
| 85 | + service.setLocalStorage('key', 'value'); |
| 86 | + |
| 87 | + const value = localStorage.getItem('key'); |
| 88 | + |
| 89 | + expect(value).toEqual('value'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('load a user by sending a token using POST', () => { |
| 93 | + service.baseUrl = '/users'; |
| 94 | + service.getUser('token').subscribe(); |
| 95 | + |
| 96 | + const loadUserRequest = httpMock.expectOne(`${service.baseUrl}/login`); |
| 97 | + expect(loadUserRequest.request.method).toBe('POST'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return true when user is Login', () => { |
| 101 | + spyOn(cookieService, 'check').and.returnValue(true); |
| 102 | + |
| 103 | + const isLogin = service.isLogin(); |
| 104 | + |
| 105 | + expect(isLogin).toBeTruthy(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return false when user is not Login', () => { |
| 109 | + spyOn(cookieService, 'check').and.returnValue(false); |
| 110 | + |
| 111 | + const isLogin = service.isLogin(); |
| 112 | + |
| 113 | + expect(isLogin).toBeFalsy(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should login with social angularx-social-login', () => { |
| 117 | + service.signIn(); |
| 118 | + expect(socialAuthService.signIn).toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should logout with social angularx-social-login', () => { |
| 122 | + service.logout(); |
| 123 | + const cookies = cookieService.getAll(); |
| 124 | + expect(socialAuthService.signOut).toHaveBeenCalled(); |
| 125 | + expect(localStorage.length).toEqual(0); |
| 126 | + expect(cookies).toEqual({}); |
| 127 | + }); |
| 128 | +}); |
0 commit comments