Nikolas Rimikis
2 years ago
1 changed files with 84 additions and 0 deletions
@ -0,0 +1,84 @@
|
||||
import 'package:neon/neon.dart'; |
||||
import 'package:test/test.dart'; |
||||
|
||||
void main() { |
||||
group('Result', () { |
||||
test('Equality', () { |
||||
const data = 'someData'; |
||||
|
||||
const a = Result( |
||||
data, |
||||
null, |
||||
isLoading: true, |
||||
isCached: false, |
||||
); |
||||
const b = Result( |
||||
data, |
||||
null, |
||||
isLoading: true, |
||||
isCached: true, |
||||
); |
||||
|
||||
expect(a, equals(a), reason: 'identical'); |
||||
expect(a, equals(b), reason: 'ignore cached state in equality'); |
||||
|
||||
expect(a.hashCode, equals(a.hashCode), reason: 'identical'); |
||||
expect(a.hashCode, isNot(equals(b.hashCode)), reason: 'hashcode should respect the cached state'); |
||||
}); |
||||
|
||||
test('Transform to loading', () { |
||||
const data = 'someData'; |
||||
|
||||
final a = Result.success(data); |
||||
const b = Result( |
||||
data, |
||||
null, |
||||
isLoading: true, |
||||
isCached: false, |
||||
); |
||||
|
||||
expect(a, isNot(equals(b))); |
||||
expect(a.asLoading(), equals(b)); |
||||
}); |
||||
|
||||
test('data check', () { |
||||
const data = 'someData'; |
||||
|
||||
final a = Result.loading(); |
||||
final b = Result.success(data); |
||||
const c = Result( |
||||
data, |
||||
null, |
||||
isLoading: false, |
||||
isCached: true, |
||||
); |
||||
|
||||
expect(a.hasData, false); |
||||
expect(b.hasData, true); |
||||
|
||||
expect(() => a.requireData, throwsStateError); |
||||
expect(b.requireData, equals(data)); |
||||
|
||||
expect(b.hasUncachedData, true); |
||||
expect(c.hasUncachedData, false); |
||||
}); |
||||
|
||||
test('error check', () { |
||||
const error = 'someError'; |
||||
|
||||
final a = Result.error(error); |
||||
|
||||
expect(a.hasError, true); |
||||
}); |
||||
|
||||
test('transform', () { |
||||
const data = 1; |
||||
|
||||
final a = Result.success(data); |
||||
|
||||
String transformer(final int data) => data.toString(); |
||||
|
||||
expect(a.transform(transformer), equals(Result.success(data.toString()))); |
||||
}); |
||||
}); |
||||
} |
Loading…
Reference in new issue