Skip to content

Commit 4c43cbc

Browse files
authored
Merge pull request trungvose#84 from darxx/more-unit-test
Unit test Example of AuthService login and LoginPayload separation.
2 parents 24d3144 + 46a93fc commit 4c43cbc

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
});

frontend/src/app/project/auth/auth.service.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { of } from 'rxjs';
55
import { catchError, finalize, map } from 'rxjs/operators';
66
import { AuthStore } from './auth.store';
77
import { environment } from 'src/environments/environment';
8+
import { LoginPayload } from '@trungk18/project/auth/loginPayload';
89

910
@Injectable({ providedIn: 'root' })
1011
export class AuthService {
@@ -36,11 +37,4 @@ export class AuthService {
3637
}
3738
}
3839

39-
export class LoginPayload {
40-
email: string;
41-
password: string;
42-
constructor() {
43-
this.email = '[email protected]';
44-
this.password = `${new Date().getTime()}`;
45-
}
46-
}
40+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class LoginPayload {
2+
email: string;
3+
password: string;
4+
constructor() {
5+
this.email = '[email protected]';
6+
this.password = `${new Date().getTime()}`;
7+
}
8+
}

frontend/src/app/project/project.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import { ProjectService } from './state/project/project.service';
3-
import { AuthService, LoginPayload } from './auth/auth.service';
3+
import { AuthService } from './auth/auth.service';
4+
import { LoginPayload } from '@trungk18/project/auth/loginPayload';
45

56
@Component({
67
selector: 'app-project',

0 commit comments

Comments
 (0)