|
| 1 | +import { AuthService } from './auth.service'; |
| 2 | +import { Subject } from 'rxjs'; |
| 3 | +import { fakeAsync, tick } from '@angular/core/testing'; |
| 4 | + |
| 5 | +describe('AuthService', () => { |
| 6 | + let service: AuthService; |
| 7 | + |
| 8 | + const httpClient: any = { |
| 9 | + get: jasmine.createSpy('get') |
| 10 | + }; |
| 11 | + const authStore: any = { |
| 12 | + setLoading: jasmine.createSpy('setLoading').and.callThrough(), |
| 13 | + update: jasmine.createSpy('update').and.callThrough(), |
| 14 | + setError: jasmine.createSpy('setError').and.callThrough() |
| 15 | + }; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + service = new AuthService( |
| 19 | + httpClient, |
| 20 | + authStore |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should be able to login', () => { |
| 25 | + const data = new Subject(); |
| 26 | + httpClient.get.and.returnValue(data); |
| 27 | + service.login({ |
| 28 | + email: '', |
| 29 | + password: '', |
| 30 | + }); |
| 31 | + expect(authStore.setLoading).toHaveBeenCalledWith(true); |
| 32 | + data.next({ |
| 33 | + id: '', |
| 34 | + name: '', |
| 35 | + email: '', |
| 36 | + avatarUrl: '', |
| 37 | + createdAt: '', |
| 38 | + updatedAt: '', |
| 39 | + issueIds: [] |
| 40 | + }); |
| 41 | + expect(httpClient.get).toHaveBeenCalledWith('/assets/data/auth.json'); |
| 42 | + expect(authStore.update).toHaveBeenCalled(); |
| 43 | + }); |
| 44 | + it('should not be able to login', fakeAsync(() => { |
| 45 | + const data = new Subject(); |
| 46 | + httpClient.get.and.returnValue(data); |
| 47 | + service.login({ |
| 48 | + email: '', |
| 49 | + password: '', |
| 50 | + }); |
| 51 | + authStore.update.and.callFake(() => { |
| 52 | + throw new Error('Something bad happened'); |
| 53 | + }); |
| 54 | + expect(authStore.setLoading).toHaveBeenCalledWith(true); |
| 55 | + authStore.setLoading.calls.reset(); |
| 56 | + data.next({ |
| 57 | + id: '', |
| 58 | + name: '', |
| 59 | + email: '', |
| 60 | + avatarUrl: '', |
| 61 | + createdAt: '', |
| 62 | + updatedAt: '', |
| 63 | + issueIds: [] |
| 64 | + }); |
| 65 | + |
| 66 | + expect(authStore.setLoading).toHaveBeenCalledWith(false); |
| 67 | + expect(authStore.setError).toHaveBeenCalled(); |
| 68 | + })); |
| 69 | +}); |
0 commit comments