-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.service.test.ts
80 lines (58 loc) · 2.3 KB
/
auth.service.test.ts
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
import { AxiosError, AxiosResponse } from 'axios';
import { HttpClient } from '../utils/httpClient.util';
import { AuthService } from './auth.service';
jest.mock('./highlights.service');
const EXPECTED_JWT_TOKEN = 'Bearer TOKEN';
describe('AuthService', () => {
let httpClient: HttpClient;
let authService: AuthService;
beforeEach(() => {
httpClient = new HttpClient();
authService = new AuthService(httpClient, EXPECTED_JWT_TOKEN);
});
describe('getNewJwtToken()', () => {
describe('when a user is authorised', () => {
it('should store new JWT token', async () => {
jest
.spyOn(httpClient, 'sendRequest')
.mockResolvedValue([{ data: { jwtToken: EXPECTED_JWT_TOKEN } } as AxiosResponse, null]);
await authService.getNewJwtToken();
expect(authService.getJwtToken()).toBe(EXPECTED_JWT_TOKEN);
});
it('should set isLoggedIn$.value to true', async () => {
const mockCallBack = jest.fn();
authService.isLoggedIn$.subscribe(mockCallBack);
jest
.spyOn(httpClient, 'sendRequest')
.mockResolvedValue([{ data: { jwtToken: EXPECTED_JWT_TOKEN } } as AxiosResponse, null]);
await authService.getNewJwtToken();
expect(mockCallBack).toBeCalledWith(true);
});
});
describe('when user is not authorised', () => {
it('should store null as JWT token', async () => {
jest.spyOn(httpClient, 'sendRequest').mockResolvedValue([{ data: {} } as AxiosResponse, null]);
await authService.getNewJwtToken();
expect(authService.getJwtToken()).toBeNull();
});
it('should set isLoggedIn$.value to false', async () => {
const mockCallBack = jest.fn();
authService.isLoggedIn$.subscribe(mockCallBack);
jest.spyOn(httpClient, 'sendRequest').mockResolvedValue([null, new AxiosError()]);
await authService.getNewJwtToken();
expect(mockCallBack).toBeCalledWith(false);
});
});
});
describe('getJwtToken()', () => {
it('should return', () => {
expect(authService.getJwtToken()).toBe(EXPECTED_JWT_TOKEN);
});
});
describe('resetJwtToken()', () => {
it('should return null as a JWT token', () => {
authService.resetJwtToken();
expect(authService.getJwtToken()).toBeNull();
});
});
});