|
| 1 | +import 'package:ht_shared/src/models/role.dart'; |
1 | 2 | import 'package:ht_shared/src/models/user.dart'; |
2 | 3 | import 'package:test/test.dart'; |
3 | 4 |
|
4 | 5 | void main() { |
5 | 6 | group('User Model', () { |
6 | 7 | const id = 'test-id'; |
7 | 8 | const email = 'test@example.com'; |
| 9 | + const standardRole = Role(name: 'standard_user'); |
| 10 | + const guestRole = Role(name: 'guest_user'); |
| 11 | + const adminRole = Role(name: 'admin'); |
8 | 12 |
|
9 | 13 | test('supports value equality', () { |
10 | 14 | expect( |
11 | | - const User(id: id, email: email, role: 'standard_user'), |
12 | | - equals(const User(id: id, email: email, role: 'standard_user')), |
| 15 | + const User(id: id, email: email, role: standardRole), |
| 16 | + equals(const User(id: id, email: email, role: standardRole)), |
13 | 17 | ); |
14 | 18 | expect( |
15 | | - const User(id: id, email: email, role: 'standard_user'), |
| 19 | + const User(id: id, email: email, role: standardRole), |
16 | 20 | isNot( |
17 | 21 | equals( |
18 | | - const User(id: 'other-id', email: email, role: 'standard_user'), |
| 22 | + const User(id: 'other-id', email: email, role: standardRole), |
19 | 23 | ), |
20 | 24 | ), |
21 | 25 | ); |
22 | 26 | expect( |
23 | | - const User(id: id, email: email, role: 'standard_user'), |
| 27 | + const User(id: id, email: email, role: standardRole), |
24 | 28 | isNot( |
25 | 29 | equals( |
26 | 30 | const User( |
27 | 31 | id: id, |
28 | 32 | email: 'other@example.com', |
29 | | - role: 'standard_user', |
| 33 | + role: standardRole, |
30 | 34 | ), |
31 | 35 | ), |
32 | 36 | ), |
33 | 37 | ); |
34 | 38 | expect( |
35 | | - const User(id: id, email: email, role: 'standard_user'), |
36 | | - isNot(equals(const User(id: id, email: email, role: 'guest_user'))), |
| 39 | + const User(id: id, email: email, role: standardRole), |
| 40 | + isNot(equals(const User(id: id, email: email, role: guestRole))), |
37 | 41 | ); |
38 | 42 | expect( |
39 | | - const User(id: id, email: email, role: 'standard_user'), |
40 | | - isNot(equals(const User(id: id, email: email, role: 'admin'))), |
| 43 | + const User(id: id, email: email, role: standardRole), |
| 44 | + isNot(equals(const User(id: id, email: email, role: adminRole))), |
41 | 45 | ); |
42 | 46 | }); |
43 | 47 |
|
44 | 48 | test('has correct toString', () { |
45 | 49 | expect( |
46 | | - const User(id: id, email: email, role: 'standard_user').toString(), |
47 | | - equals('User(id: $id, email: $email, role: standard_user)'), |
| 50 | + const User(id: id, email: email, role: standardRole).toString(), |
| 51 | + equals('User(id: $id, email: $email, role: ${standardRole.toString()})'), |
48 | 52 | ); |
49 | 53 | expect( |
50 | | - const User(id: id, role: 'guest_user').toString(), |
51 | | - equals('User(id: $id, email: null, role: guest_user)'), |
| 54 | + const User(id: id, role: guestRole).toString(), |
| 55 | + equals('User(id: $id, email: null, role: ${guestRole.toString()})'), |
52 | 56 | ); |
53 | 57 | expect( |
54 | | - const User(id: id, role: 'admin').toString(), |
55 | | - equals('User(id: $id, email: null, role: admin)'), |
| 58 | + const User(id: id, role: adminRole).toString(), |
| 59 | + equals('User(id: $id, email: null, role: ${adminRole.toString()})'), |
56 | 60 | ); |
57 | 61 | }); |
58 | 62 |
|
59 | 63 | // Basic test for JSON serialization - assumes build_runner generated correctly |
60 | 64 | test('can be serialized and deserialized', () { |
61 | | - const user = User(id: id, email: email, role: 'standard_user'); |
| 65 | + const user = User(id: id, email: email, role: standardRole); |
62 | 66 | final json = user.toJson(); |
63 | 67 | final deserializedUser = User.fromJson(json); |
64 | 68 | expect(deserializedUser, equals(user)); |
65 | 69 |
|
66 | | - const anonUser = User(id: id, role: 'guest_user'); |
| 70 | + const anonUser = User(id: id, role: guestRole); |
67 | 71 | final anonJson = anonUser.toJson(); |
68 | 72 | final deserializedAnonUser = User.fromJson(anonJson); |
69 | 73 | expect(deserializedAnonUser, equals(anonUser)); |
70 | 74 |
|
71 | | - const adminUser = User(id: id, email: email, role: 'admin'); |
| 75 | + const adminUser = User(id: id, email: email, role: adminRole); |
72 | 76 | final adminJson = adminUser.toJson(); |
73 | 77 | final deserializedAdminUser = User.fromJson(adminJson); |
74 | 78 | expect(deserializedAdminUser, equals(adminUser)); |
|
0 commit comments