-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.test.ts
58 lines (41 loc) · 1.42 KB
/
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
import { makeAuthenticationService } from '../src/service';
type User = {
username: string;
};
describe('AuthenticationService', () => {
test('login should alter state and inform subscribers', () => {
expect.assertions(2);
const service = makeAuthenticationService<User>();
const user: User = { username: 'Henk' };
service.login(user);
service.subscribe((state) => {
expect(state.currentUser).toBe(user);
expect(state.isLoggedIn).toBe(true);
});
});
test('logout should alter state and inform subscribers', () => {
expect.assertions(2);
const service = makeAuthenticationService<User>();
service.logout();
service.subscribe((state) => {
expect(state.currentUser).toBe(undefined);
expect(state.isLoggedIn).toBe(false);
});
});
test('subscription lifecycle', () => {
const service = makeAuthenticationService<User>();
// Subscribe a subscriber.
const subscriber = jest.fn();
service.subscribe(subscriber);
// It should immediately receive the state after subscribing.
expect(subscriber).toBeCalledTimes(1);
// Call logout which should inform the subscriber.
service.logout();
expect(subscriber).toBeCalledTimes(2);
// Unsubscribe the subscriber, and call logout.
service.unsubscribe(subscriber);
service.logout();
// It should not have been informed anymore.
expect(subscriber).toBeCalledTimes(2);
});
});