-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.spec.ts
93 lines (74 loc) · 3.21 KB
/
user.spec.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
81
82
83
84
85
86
87
88
89
90
91
92
93
import { User } from './user';
import { NewUserRegistered } from './events/new-user-registered';
import { UserPasswordChanged } from './events/user-password-changed';
import { UserEmailChanged } from './events/user-email-changed';
import { UserNameUpdated } from './events/user-name-updated';
import { UserLoggedIn } from './events/user-logged-in';
describe('User', () => {
it('should register a new user and add a NewUserRegistered event', () => {
const user = User.register({
name: 'John Doe',
email: '[email protected]',
password: 'securepassword',
});
expect(user.name).toBe('John Doe');
expect(user.email).toBe('[email protected]');
expect(user.password).toBe('securepassword');
expect(user.createdAt).toBeInstanceOf(Date);
expect(user.updatedAt).toBeInstanceOf(Date);
expect(user.getEvents().length).toBe(1);
const event = user.getEvents()[0] as NewUserRegistered;
expect(event).toBeInstanceOf(NewUserRegistered);
expect(event.data['id']).toBe(user.id);
expect(event.data['name']).toBe('John Doe');
expect(event.data['email']).toBe('[email protected]');
});
it('should change the user password and add a UserPasswordChanged event', () => {
const user = new User();
user.password = 'oldpassword';
user.updatedAt = new Date();
user.changePassword('newpassword');
expect(user.password).toBe('newpassword');
expect(user.updatedAt).toBeInstanceOf(Date);
expect(user.getEvents().length).toBe(1);
const event = user.getEvents()[0] as UserPasswordChanged;
expect(event).toBeInstanceOf(UserPasswordChanged);
expect(event.data['userId']).toBe(user.id);
expect(event.data['newPassword']).toBe('newpassword');
});
it('should change the user email and add a UserEmailChanged event', () => {
const user = new User();
user.email = '[email protected]';
user.updatedAt = new Date();
user.changeEmail('[email protected]');
expect(user.email).toBe('[email protected]');
expect(user.updatedAt).toBeInstanceOf(Date);
expect(user.getEvents().length).toBe(1);
const event = user.getEvents()[0] as UserEmailChanged;
expect(event).toBeInstanceOf(UserEmailChanged);
expect(event.data['userId']).toBe(user.id);
expect(event.data['newEmail']).toBe('[email protected]');
});
it('should update the user name and add a UserNameUpdated event', () => {
const user = new User();
user.name = 'Old Name';
user.updatedAt = new Date();
user.updateName('New Name');
expect(user.name).toBe('New Name');
expect(user.updatedAt).toBeInstanceOf(Date);
expect(user.getEvents().length).toBe(1);
const event = user.getEvents()[0] as UserNameUpdated;
expect(event).toBeInstanceOf(UserNameUpdated);
expect(event.data['userId']).toBe(user.id);
expect(event.data['newName']).toBe('New Name');
});
it('should set lastLoginAt and add a UserLoggedIn event', () => {
const user = new User();
user.login();
expect(user.lastLoginAt).toBeInstanceOf(Date);
expect(user.getEvents().length).toBe(1);
const event = user.getEvents()[0] as UserLoggedIn;
expect(event).toBeInstanceOf(UserLoggedIn);
expect(event.data['userId']).toBe(user.id);
});
});