|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:dio/dio.dart'; |
| 4 | +import 'package:flutter_news_app/config/base_url_config.dart'; |
| 5 | +import 'package:flutter_news_app/config/constant_config.dart'; |
| 6 | +import 'package:flutter_news_app/feature/data/datasource/news/news_remote_data_source.dart'; |
| 7 | +import 'package:flutter_news_app/feature/data/model/topheadlinesnews/top_headlines_news_response_model.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:matcher/matcher.dart'; |
| 10 | +import 'package:mockito/mockito.dart'; |
| 11 | + |
| 12 | +import '../../../../fixture/fixture_reader.dart'; |
| 13 | + |
| 14 | +class MockDioAdapter extends Mock implements HttpClientAdapter {} |
| 15 | + |
| 16 | +class MockDio extends Mock implements Dio {} |
| 17 | + |
| 18 | +void main() { |
| 19 | + NewsRemoteDataSourceImpl newsRemoteDataSource; |
| 20 | + ConstantConfig constantConfig; |
| 21 | + MockDio mockDio; |
| 22 | + MockDioAdapter mockDioAdapter; |
| 23 | + |
| 24 | + setUp(() { |
| 25 | + mockDio = MockDio(); |
| 26 | + constantConfig = ConstantConfig(); |
| 27 | + mockDioAdapter = MockDioAdapter(); |
| 28 | + mockDio.httpClientAdapter = mockDioAdapter; |
| 29 | + mockDio.options = BaseOptions( |
| 30 | + baseUrl: BaseUrlConfig().baseUrlDevelopment, |
| 31 | + ); |
| 32 | + newsRemoteDataSource = NewsRemoteDataSourceImpl( |
| 33 | + dio: mockDio, |
| 34 | + constantConfig: constantConfig, |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + group('getTopHeadlinesNews', () { |
| 39 | + final tTopHeadlinesNewsResponseModel = TopHeadlinesNewsResponseModel.fromJson( |
| 40 | + json.decode( |
| 41 | + fixture('top_headlines_news_response_model.json'), |
| 42 | + ), |
| 43 | + ); |
| 44 | + |
| 45 | + void setUpMockDioSuccess() { |
| 46 | + final responsePayload = json.decode(fixture('top_headlines_news_response_model.json')); |
| 47 | + final response = Response( |
| 48 | + data: responsePayload, |
| 49 | + statusCode: 200, |
| 50 | + headers: Headers.fromMap({ |
| 51 | + Headers.contentTypeHeader: [Headers.jsonContentType], |
| 52 | + }), |
| 53 | + ); |
| 54 | + when( |
| 55 | + mockDio.get( |
| 56 | + any, |
| 57 | + queryParameters: anyNamed('queryParameters'), |
| 58 | + ), |
| 59 | + ).thenAnswer((_) async => response); |
| 60 | + } |
| 61 | + |
| 62 | + test( |
| 63 | + 'make sure there is a GET request to endpoint /v2/top-headlines?country=:country&apiKey=:apiKey', |
| 64 | + () async { |
| 65 | + // arrange |
| 66 | + setUpMockDioSuccess(); |
| 67 | + |
| 68 | + // act |
| 69 | + await newsRemoteDataSource.getTopHeadlinesNews(); |
| 70 | + |
| 71 | + // assert |
| 72 | + verify( |
| 73 | + mockDio.get( |
| 74 | + '/v2/top-headlines', |
| 75 | + queryParameters: { |
| 76 | + 'country': 'id', |
| 77 | + 'apiKey': constantConfig.apiKeyNewsApi, |
| 78 | + }, |
| 79 | + ), |
| 80 | + ); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + test( |
| 85 | + 'make sure to return the TopHeadlinesNewsResponseModel object when the ' |
| 86 | + 'response code is successful from the endpoint', |
| 87 | + () async { |
| 88 | + // arrange |
| 89 | + setUpMockDioSuccess(); |
| 90 | + |
| 91 | + // act |
| 92 | + final result = await newsRemoteDataSource.getTopHeadlinesNews(); |
| 93 | + |
| 94 | + // assert |
| 95 | + expect(result, tTopHeadlinesNewsResponseModel); |
| 96 | + }, |
| 97 | + ); |
| 98 | + |
| 99 | + test( |
| 100 | + 'make sure to receive DioError when the response code fails from the endpoint', |
| 101 | + () async { |
| 102 | + // arrange |
| 103 | + final response = Response( |
| 104 | + data: 'Bad Request', |
| 105 | + statusCode: 400, |
| 106 | + ); |
| 107 | + when(mockDio.get(any, queryParameters: anyNamed('queryParameters'))).thenAnswer((_) async => response); |
| 108 | + |
| 109 | + // act |
| 110 | + final call = newsRemoteDataSource.getTopHeadlinesNews(); |
| 111 | + |
| 112 | + // assert |
| 113 | + expect(() => call, throwsA(TypeMatcher<DioError>())); |
| 114 | + }, |
| 115 | + ); |
| 116 | + }); |
| 117 | +} |
0 commit comments