You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
samples/compass_app/app/testing/mocks.dart

55 lines
2.1 KiB

import 'dart:convert';
import 'dart:io';
Compass App: Search form screen (#2353) Part of the implementation of the Compass App for the Architecture sample. **Merge to `compass-app`** This PR introduces the Search Form Screen, in which users can select a region, a date range and the number of guests. The feature is split in 5 different widgets, each one depending on the `SearchFormViewModel`. The architecture follows the same patterns implemented in the previous PR https://github.com/flutter/samples/pull/2342 TODO later on: - Error handling is yet not implemented, we need to introduce a "logger" and a way to handle error responses. - All repositories return local data, next steps include creating the dart server app. - The search query at the moment only takes the "continent" and not the dates and number of guests, that would be implemented later on as well. ## Demo [Screencast from 2024-07-12 14-30-48.webm](https://github.com/user-attachments/assets/afbcdd4e-617a-49cc-894c-8e082748e572) ## Pre-launch Checklist - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [x] I read the [Contributors Guide]. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-devrel channel on [Discord]. <!-- Links --> [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [Contributors Guide]: https://github.com/flutter/samples/blob/main/CONTRIBUTING.md
4 months ago
import 'package:go_router/go_router.dart';
import 'package:mocktail/mocktail.dart';
class MockGoRouter extends Mock implements GoRouter {}
class MockHttpClient extends Mock implements HttpClient {}
class MockHttpHeaders extends Mock implements HttpHeaders {}
class MockHttpClientRequest extends Mock implements HttpClientRequest {}
class MockHttpClientResponse extends Mock implements HttpClientResponse {}
extension HttpMethodMocks on MockHttpClient {
void mockGet(String path, Object object) {
when(() => get(any(), any(), path)).thenAnswer((invocation) {
final request = MockHttpClientRequest();
final response = MockHttpClientResponse();
when(() => request.close()).thenAnswer((_) => Future.value(response));
when(() => request.headers).thenReturn(MockHttpHeaders());
when(() => response.statusCode).thenReturn(200);
when(() => response.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(jsonEncode(object)));
return Future.value(request);
});
}
void mockPost(String path, Object object, [int statusCode = 201]) {
when(() => post(any(), any(), path)).thenAnswer((invocation) {
final request = MockHttpClientRequest();
final response = MockHttpClientResponse();
when(() => request.close()).thenAnswer((_) => Future.value(response));
when(() => request.headers).thenReturn(MockHttpHeaders());
when(() => response.statusCode).thenReturn(statusCode);
when(() => response.transform(utf8.decoder))
.thenAnswer((_) => Stream.value(jsonEncode(object)));
return Future.value(request);
});
}
void mockDelete(String path) {
when(() => delete(any(), any(), path)).thenAnswer((invocation) {
final request = MockHttpClientRequest();
final response = MockHttpClientResponse();
when(() => request.close()).thenAnswer((_) => Future.value(response));
when(() => request.headers).thenReturn(MockHttpHeaders());
when(() => response.statusCode).thenReturn(204);
return Future.value(request);
});
}
}