Last active
January 9, 2023 10:17
-
-
Save StanislawNagorski/42005bc584d2356328fb4d5f13bbad75 to your computer and use it in GitHub Desktop.
mock remote data source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const String _mockDataJson = 'assets/mock_data/events_spots_localization_mock_data.json'; | |
const int _mockCallMillisecondsDuration = 500; | |
@Injectable( | |
as: RemoteDataSourceMock, | |
env: [Flavor.DEVELOP, Flavor.STAGE], | |
) | |
class RemoteDataSourceMock implements RemoteDataSource { | |
@override | |
Future<List<DataModel>> getData({required GeoPoint userLocation}) async => | |
await Future.delayed(Duration(milliseconds: _mockCallMillisecondsDuration)) | |
.then((_) => File(_mockDataJson) | |
.readAsString() | |
.then((json) => jsonDecode(json)) | |
.then((data) => (data as Iterable) | |
.map((data) => DataModel.fromJson(data)) | |
.toList()) | |
); | |
} | |
/// TEST | |
void main() { | |
group('json decoding', () { | |
setUpAll(() => WidgetsFlutterBinding.ensureInitialized()); | |
test('should decode json', () async { | |
//Given | |
final fakeUserLocation = GeoPoint(0, 0); | |
final mockDataSource = RemoteDataSourceMock(); | |
//When | |
final result = await mockDataSource.getData(userLocation: fakeUserLocation); | |
//Then | |
expect(result, isList); | |
}); | |
test('return list should not be empty', () async { | |
//Given | |
final fakeUserLocation = GeoPoint(0, 0); | |
final mockDataSource = RemoteDataSourceMock(); | |
//When | |
final result = await mockDataSource.getData(userLocation: fakeUserLocation); | |
//Then | |
expect(result, isNotEmpty); | |
}); | |
test('should map json to Spot Location Model', () async { | |
//Given | |
final fakeUserLocation = GeoPoint(0, 0); | |
final mockDataSource = RemoteDataSourceMock(); | |
//When | |
final responseData = await mockDataSource.getData(userLocation: fakeUserLocation); | |
final result = responseData.every((element) => element is DataModel); | |
//Then | |
expect(result, isTrue); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment