|
1 | | -import 'dart:ffi'; |
2 | | - |
3 | 1 | import 'package:flutter_bloc_app_template/data/network/api_result.dart'; |
4 | 2 | import 'package:flutter_test/flutter_test.dart'; |
5 | 3 |
|
6 | 4 | void main() { |
7 | 5 | group('ApiResult', () { |
8 | | - test('success should contain correct data', () { |
9 | | - final result = const ApiResult.success('Test Data'); |
| 6 | + test('should return success result with correct data', () { |
| 7 | + const result = ApiResult.success(42); |
| 8 | + |
| 9 | + final output = ApiResultWhen(result).when( |
| 10 | + success: (data) => 'Success with $data', |
| 11 | + error: (message) => 'Error: $message', |
| 12 | + loading: () => 'Loading...', |
| 13 | + ); |
10 | 14 |
|
11 | | - expect(result, isA<Success<String>>()); |
12 | | - expect((result as Success<String>).data, 'Test Data'); |
| 15 | + expect(output, 'Success with 42'); |
13 | 16 | }); |
14 | 17 |
|
15 | | - test('error should contain correct message', () { |
16 | | - final result = const ApiResult<String>.error('Test Error'); |
| 18 | + test('should return error result with correct message', () { |
| 19 | + const result = ApiResult<String>.error('Something went wrong'); |
| 20 | + |
| 21 | + final output = ApiResultWhen(result).when( |
| 22 | + success: (data) => 'Success with $data', |
| 23 | + error: (message) => 'Error: $message', |
| 24 | + loading: () => 'Loading...', |
| 25 | + ); |
17 | 26 |
|
18 | | - expect(result, isA<Error<String>>()); |
19 | | - expect((result as Error<String>).message, 'Test Error'); |
| 27 | + expect(output, 'Error: Something went wrong'); |
20 | 28 | }); |
21 | 29 |
|
22 | | - test('loading should be of type Loading', () { |
23 | | - final result = const ApiResult<Void>.loading(); |
| 30 | + test('should return loading result', () { |
| 31 | + const result = ApiResult<String>.loading(); |
24 | 32 |
|
25 | | - expect(result, isA<Loading<Void>>()); |
| 33 | + final output = ApiResultWhen(result).when( |
| 34 | + success: (data) => 'Success with $data', |
| 35 | + error: (message) => 'Error: $message', |
| 36 | + loading: () => 'Loading...', |
| 37 | + ); |
| 38 | + |
| 39 | + expect(output, 'Loading...'); |
| 40 | + }); |
| 41 | + |
| 42 | + test('should throw assertion error for unknown type', () { |
| 43 | + final invalid = const _InvalidApiResult<int>(); |
| 44 | + |
| 45 | + expect( |
| 46 | + () => ApiResultWhen(invalid).when( |
| 47 | + success: (data) => 'Success with $data', |
| 48 | + error: (message) => 'Error: $message', |
| 49 | + loading: () => 'Loading...', |
| 50 | + ), |
| 51 | + throwsA(isA<AssertionError>()), |
| 52 | + ); |
26 | 53 | }); |
27 | 54 | }); |
28 | 55 | } |
| 56 | + |
| 57 | +/// A fake invalid implementation to test the fallback case in `when` |
| 58 | +class _InvalidApiResult<T> implements ApiResult<T> { |
| 59 | + const _InvalidApiResult(); |
| 60 | +} |
0 commit comments