forked from VeryGoodOpenSource/formz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformz_test.dart
225 lines (190 loc) · 7.09 KB
/
formz_test.dart
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ignore_for_file: prefer_const_constructors
import 'package:formz/formz.dart';
import 'package:test/test.dart';
import 'helpers/helpers.dart';
void main() {
group('Formz', () {
group('FormzMixin', () {
test('status returns pure', () {
expect(NameInputFormzMixin().status, FormzStatus.pure);
});
test('status returns invalid', () {
expect(
NameInputFormzMixin(name: const NameInput.dirty(value: '')).status,
FormzStatus.invalid,
);
});
test('status returns valid', () {
expect(
NameInputFormzMixin(name: const NameInput.dirty(value: 'joe')).status,
FormzStatus.valid,
);
});
});
group('FormzInput', () {
test('value is correct', () {
expect(NameInput.pure(value: 'joe').value, 'joe');
});
test('pure is true when super.pure is used', () {
expect(NameInput.pure(value: 'joe').pure, isTrue);
});
test('pure is false when super.dirty is used', () {
expect(NameInput.dirty(value: 'joe').pure, isFalse);
});
test('status is pure if super.pure is used', () {
final status = NameInput.pure(value: 'joe').status;
expect(status, FormzInputStatus.pure);
});
test('status is valid if super.dirty is used and input is valid', () {
final status = NameInput.dirty(value: 'joe').status;
expect(status, FormzInputStatus.valid);
});
test('status is invalid if super.dirty is used and input is invalid', () {
final status = NameInput.dirty(value: '').status;
expect(status, FormzInputStatus.invalid);
});
test('error is NameInputError.empty if pure is used', () {
expect(NameInput.pure(value: '').error, NameInputError.empty);
});
test('error is null if super.dirty is used and input is valid', () {
expect(NameInput.dirty(value: 'joe').error, isNull);
});
test(
'error is NameInputError.empty '
'if super.dirty is used and input is invalid', () {
expect(NameInput.dirty(value: '').error, NameInputError.empty);
});
test('valid is true if super.pure is used and input is valid', () {
expect(NameInput.pure(value: 'joe').valid, isTrue);
});
test('valid is true if super.dirty is used and input is valid', () {
expect(NameInput.dirty(value: 'joe').valid, isTrue);
});
test('valid is false if super.pure is used and input is invalid', () {
expect(NameInput.pure(value: '').valid, isFalse);
});
test('valid is false if super.dirty is used and input is invalid', () {
expect(NameInput.dirty(value: '').valid, isFalse);
});
test('invalid is true if super.dirty is used and input is invalid', () {
expect(NameInput.dirty(value: '').invalid, isTrue);
});
test('hashCode is correct', () {
expect(
NameInput.pure(value: '').hashCode,
''.hashCode ^ true.hashCode,
);
expect(
NameInput.dirty(value: 'joe').hashCode,
'joe'.hashCode ^ false.hashCode,
);
});
test('== is value based', () {
expect(NameInput.pure(), equals(NameInput.pure()));
expect(
NameInput.dirty(value: 'joe'),
equals(NameInput.dirty(value: 'joe')),
);
expect(
NameInput.dirty(value: 'joe'),
equals(NameInput.dirty(value: 'joe')),
);
expect(
NameInput.dirty(value: 'bob'),
isNot(equals(NameInput.dirty(value: 'joe'))),
);
expect(
NameInput.pure(value: 'joe'),
isNot(equals(NameInput.dirty(value: 'joe'))),
);
});
test('toString is overridden correctly', () {
expect(NameInput.pure().toString(), equals('NameInput(, true)'));
expect(
NameInput.pure(value: 'joe').toString(),
equals('NameInput(joe, true)'),
);
expect(
NameInput.dirty(value: 'joe').toString(),
equals('NameInput(joe, false)'),
);
});
});
group('validate', () {
test('throws AssertionError for null inputs', () {
expect(() => Formz.validate(null), throwsA(isA<AssertionError>()));
});
test('returns pure for empty inputs', () {
final status = Formz.validate([]);
expect(status, equals(FormzStatus.pure));
expect(status.isValid, isFalse);
expect(status.isPure, isTrue);
});
test('returns valid for single valid input', () {
final status = Formz.validate([NameInput.dirty(value: 'joe')]);
expect(status, equals(FormzStatus.valid));
expect(status.isValid, isTrue);
});
test('returns valid for multiple valid inputs', () {
final status = Formz.validate([
NameInput.dirty(value: 'jen'),
NameInput.dirty(value: 'bob'),
NameInput.dirty(value: 'alex'),
]);
expect(status, equals(FormzStatus.valid));
expect(status.isValid, isTrue);
});
test('returns invalid for single invalid input', () {
final status = Formz.validate([NameInput.dirty(value: '')]);
expect(status, equals(FormzStatus.invalid));
expect(status.isInvalid, isTrue);
});
test('returns invalid for multiple invalid inputs', () {
final status = Formz.validate([
NameInput.dirty(value: ''),
NameInput.dirty(value: ''),
NameInput.dirty(value: ''),
]);
expect(status, equals(FormzStatus.invalid));
expect(status.isInvalid, isTrue);
});
test('returns invalid when at least one input is invalid', () {
final status = Formz.validate([
NameInput.dirty(value: 'jan'),
NameInput.dirty(value: 'jim'),
NameInput.dirty(value: ''),
]);
expect(status, equals(FormzStatus.invalid));
expect(status.isInvalid, isTrue);
});
});
group('FormzStatusX', () {
test('isPure returns true', () {
expect(FormzStatus.pure.isPure, isTrue);
});
test('isValid returns true', () {
expect(FormzStatus.valid.isValid, isTrue);
});
test('isValidated is correct', () {
expect(FormzStatus.pure.isValidated, isFalse);
expect(FormzStatus.invalid.isValidated, isFalse);
expect(FormzStatus.valid.isValidated, isTrue);
expect(FormzStatus.submissionInProgress.isValidated, isTrue);
expect(FormzStatus.submissionFailure.isValidated, isTrue);
expect(FormzStatus.submissionSuccess.isValidated, isTrue);
});
test('isInvalid returns true', () {
expect(FormzStatus.invalid.isInvalid, isTrue);
});
test('isSubmissionInProgress returns true', () {
expect(FormzStatus.submissionInProgress.isSubmissionInProgress, isTrue);
});
test('isSubmissionFailure returns true', () {
expect(FormzStatus.submissionFailure.isSubmissionFailure, isTrue);
});
test('isSubmissionSuccess returns true', () {
expect(FormzStatus.submissionSuccess.isSubmissionSuccess, isTrue);
});
});
});
}