57 lines
2.4 KiB
Dart
57 lines
2.4 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:fotodocumentation/controller/picture_controller.dart';
|
|
import 'package:fotodocumentation/dto/picture_dto.dart';
|
|
import 'package:fotodocumentation/utils/di_container.dart';
|
|
import 'package:fotodocumentation/utils/http_client_utils.dart';
|
|
import 'package:fotodocumentation/utils/jwt_token_storage.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import '../testing/test_http_client_utils.dart';
|
|
import '../testing/test_utils.mocks.dart';
|
|
|
|
void main() {
|
|
DiContainer.instance.initState();
|
|
var jwtTokenStorage = MockJwtTokenStorage();
|
|
when(jwtTokenStorage.getAccessToken()).thenAnswer((_) => null);
|
|
DiContainer.instance.put(JwtTokenStorage, jwtTokenStorage);
|
|
|
|
PictureController controller = PictureControllerImpl();
|
|
|
|
group('PictureControllerTest', () {
|
|
test('returns true if delete success', () async {
|
|
_testDelete(controller, 200, true);
|
|
});
|
|
|
|
test('returns false if delete failed', () async {
|
|
_testDelete(controller, 404, false);
|
|
});
|
|
test('returns false if update evaluation failed', () async {
|
|
_testUpdateEvaluate(controller, 200, true);
|
|
});
|
|
});
|
|
}
|
|
|
|
void _testDelete(PictureController controller, int response, bool expected) async {
|
|
final client = MockClient();
|
|
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
|
|
|
|
when(client.delete(Uri.parse('http://localhost:8080/api/picture/4'), headers: {"Accept-Language": "en-US"})).thenAnswer((_) async => http.Response("", response));
|
|
|
|
var dto = await controller
|
|
.delete(PictureDto(id: 4, pictureDate: DateTime.now(), category: "", comment: "", evaluation: 1, username: "", imageUrl: "", normalSizeUrl: "", thumbnailSizeUrl: ""));
|
|
expect(dto, expected);
|
|
}
|
|
|
|
void _testUpdateEvaluate(PictureController controller, int response, bool expected) async {
|
|
final client = MockClient();
|
|
DiContainer.instance.put(HttpClientUtils, TestHttpCLientUtilsImpl(client));
|
|
|
|
when(client.put(Uri.parse('http://localhost:8080/api/picture/evaluation/4?evaluation=1'), headers: {"Accept-Language": "en-US"}))
|
|
.thenAnswer((_) async => http.Response("", response));
|
|
|
|
var dto = await controller.updateEvaluation(
|
|
PictureDto(id: 4, pictureDate: DateTime.now(), category: "", comment: "", evaluation: 1, username: "", imageUrl: "", normalSizeUrl: "", thumbnailSizeUrl: ""));
|
|
expect(dto, expected);
|
|
}
|