Skip to content

Commit d07f80d

Browse files
committed
fix: Add equality to user attributes classes
* Both and . * Useful when verifying if a mock has been called with the correct parameter.
1 parent be998fd commit d07f80d

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

packages/gotrue/lib/src/types/user_attributes.dart

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// ignore_for_file: public_member_api_docs, sort_constructors_first
2+
import 'package:collection/collection.dart';
3+
14
class UserAttributes {
25
/// The user's email.
36
String? email;
@@ -35,6 +38,26 @@ class UserAttributes {
3538
if (data != null) 'data': data,
3639
};
3740
}
41+
42+
@override
43+
bool operator ==(covariant UserAttributes other) {
44+
if (identical(this, other)) return true;
45+
46+
return other.email == email &&
47+
other.phone == phone &&
48+
other.password == password &&
49+
other.nonce == nonce &&
50+
other.data == data;
51+
}
52+
53+
@override
54+
int get hashCode {
55+
return email.hashCode ^
56+
phone.hashCode ^
57+
password.hashCode ^
58+
nonce.hashCode ^
59+
data.hashCode;
60+
}
3861
}
3962

4063
class AdminUserAttributes extends UserAttributes {
@@ -102,4 +125,25 @@ class AdminUserAttributes extends UserAttributes {
102125
if (banDuration != null) 'ban_duration': banDuration,
103126
};
104127
}
128+
129+
@override
130+
bool operator ==(covariant AdminUserAttributes other) {
131+
if (identical(this, other)) return true;
132+
final mapEquals = const DeepCollectionEquality().equals;
133+
134+
return mapEquals(other.userMetadata, userMetadata) &&
135+
mapEquals(other.appMetadata, appMetadata) &&
136+
other.emailConfirm == emailConfirm &&
137+
other.phoneConfirm == phoneConfirm &&
138+
other.banDuration == banDuration;
139+
}
140+
141+
@override
142+
int get hashCode {
143+
return userMetadata.hashCode ^
144+
appMetadata.hashCode ^
145+
emailConfirm.hashCode ^
146+
phoneConfirm.hashCode ^
147+
banDuration.hashCode;
148+
}
105149
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import 'package:gotrue/src/types/user_attributes.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
late String email;
6+
late String phone;
7+
late String password;
8+
late String nonce;
9+
late Map<String, dynamic> data;
10+
11+
setUp(() {
12+
email = 'john@supabase.com';
13+
phone = '+1234567890';
14+
password = 'password';
15+
nonce = 'nonce';
16+
data = {'first_name': 'John', 'last_name': 'Doe'};
17+
});
18+
19+
group('User attributes', () {
20+
late UserAttributes userAttributesOne;
21+
late UserAttributes userAttributesTwo;
22+
23+
setUp(() {
24+
userAttributesOne = UserAttributes(
25+
email: email,
26+
phone: phone,
27+
password: password,
28+
nonce: nonce,
29+
data: data,
30+
);
31+
32+
userAttributesTwo = UserAttributes(
33+
email: email,
34+
phone: phone,
35+
password: password,
36+
nonce: nonce,
37+
data: data,
38+
);
39+
});
40+
41+
test('Attributes are equals', () {
42+
// assert
43+
expect(userAttributesOne, equals(userAttributesTwo));
44+
});
45+
46+
test('Attributes are not equals', () {
47+
// arrange
48+
final userAttributesThree = UserAttributes(
49+
email: 'email',
50+
phone: phone,
51+
password: password,
52+
nonce: nonce,
53+
data: {'first_name': 'Jane', 'last_name': 'Doe'},
54+
);
55+
56+
// assert
57+
expect(userAttributesOne, isNot(equals(userAttributesThree)));
58+
});
59+
});
60+
61+
group('Admin user attributes', () {
62+
late AdminUserAttributes adminUserAttributesOne;
63+
late AdminUserAttributes adminUserAttributesTwo;
64+
65+
late Map<String, dynamic> userMetadata;
66+
late Map<String, dynamic> appMetadata;
67+
late bool emailConfirm;
68+
late bool phoneConfirm;
69+
late String banDuration;
70+
71+
setUp(() {
72+
userMetadata = {'first_name': 'John', 'last_name': 'Doe'};
73+
appMetadata = {
74+
'roles': ['admin']
75+
};
76+
emailConfirm = true;
77+
phoneConfirm = true;
78+
banDuration = '1d';
79+
80+
adminUserAttributesOne = AdminUserAttributes(
81+
email: email,
82+
phone: phone,
83+
password: password,
84+
data: data,
85+
userMetadata: userMetadata,
86+
appMetadata: appMetadata,
87+
emailConfirm: emailConfirm,
88+
phoneConfirm: phoneConfirm,
89+
banDuration: banDuration,
90+
);
91+
92+
adminUserAttributesTwo = AdminUserAttributes(
93+
email: email,
94+
phone: phone,
95+
password: password,
96+
data: data,
97+
userMetadata: userMetadata,
98+
appMetadata: appMetadata,
99+
emailConfirm: emailConfirm,
100+
phoneConfirm: phoneConfirm,
101+
banDuration: banDuration,
102+
);
103+
});
104+
105+
test('Attributes are equals', () {
106+
// assert
107+
expect(adminUserAttributesOne, equals(adminUserAttributesTwo));
108+
});
109+
110+
test('Attributes are not equals', () {
111+
// arrange
112+
final adminUserAttributesThree = AdminUserAttributes(
113+
email: email,
114+
phone: phone,
115+
password: password,
116+
data: data,
117+
userMetadata: {'first_name': 'Jane', 'last_name': 'Doe'},
118+
appMetadata: appMetadata,
119+
emailConfirm: false,
120+
phoneConfirm: false,
121+
banDuration: 'banDuration',
122+
);
123+
124+
// assert
125+
expect(adminUserAttributesOne, isNot(equals(adminUserAttributesThree)));
126+
});
127+
});
128+
}

0 commit comments

Comments
 (0)