-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from wednesday-solutions/domain-unit-test
Add domain unit tests
- Loading branch information
Showing
19 changed files
with
309 additions
and
36 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
test/domain/weather/get_favorite_cities_stream_use_case_impl_test.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:flutter_template/domain/weather/get_favorite_cities_stream_use_case.dart'; | ||
import 'package:flutter_template/domain/weather/get_favorite_cities_stream_use_case_impl.dart'; | ||
import 'package:flutter_template/foundation/unit.dart'; | ||
import 'package:flutter_template/repository/weather/weather_repository.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../exception/test_exceptions.dart'; | ||
import '../../extensions/stream_extensions.dart'; | ||
import '../../mocks/mocks.dart'; | ||
import '../../test_models/city_models.dart'; | ||
|
||
void main() { | ||
late WeatherRepository weatherRepository; | ||
late GetFavoriteCitiesStreamUseCase getFavoriteCitiesStreamUseCase; | ||
|
||
setUp(() { | ||
weatherRepository = MockWeatherRepository(); | ||
getFavoriteCitiesStreamUseCase = GetFavoriteCitiesStreamUseCaseImpl( | ||
weatherRepository: weatherRepository); | ||
}); | ||
|
||
tearDown(() { | ||
resetMocktailState(); | ||
}); | ||
|
||
test( | ||
"Given get favorite cities stream use case called, When no error occurs, Then correct data is emitted", | ||
() { | ||
// Given | ||
final testCityList = singleCityList; | ||
when(() => weatherRepository.getFavoriteCitiesStream()) | ||
.thenAnswer((invocation) => Stream.value(testCityList)); | ||
|
||
// When | ||
expect( | ||
getFavoriteCitiesStreamUseCase(unit), | ||
// Then | ||
emitsInOrder([testCityList]), | ||
); | ||
}, | ||
); | ||
|
||
test( | ||
"Given get favorite cities stream use case called, When error occurs, Then no data is emitted", | ||
() { | ||
// Given | ||
final testException = TestException(); | ||
when(() => weatherRepository.getFavoriteCitiesStream()) | ||
.thenAnswer((invocation) => Stream.error(testException)); | ||
|
||
// When | ||
getFavoriteCitiesStreamUseCase(unit) | ||
// Then | ||
.emitsNothing; | ||
}, | ||
); | ||
} |
61 changes: 61 additions & 0 deletions
61
test/domain/weather/set_city_favorite_use_case_impl_test.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:flutter_template/domain/entity/base/result/result.dart'; | ||
import 'package:flutter_template/domain/weather/set_city_favorite_use_case_impl.dart'; | ||
import 'package:flutter_template/repository/weather/weather_repository.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../exception/test_exceptions.dart'; | ||
import '../../extensions/mock_extensions.dart'; | ||
import '../../mocks/mocks.dart'; | ||
import '../../test_models/city_models.dart'; | ||
|
||
void main() { | ||
late WeatherRepository weatherRepository; | ||
late SetCityFavoriteUseCaseImpl setCityFavoriteUseCase; | ||
|
||
setUp(() { | ||
weatherRepository = MockWeatherRepository(); | ||
setCityFavoriteUseCase = SetCityFavoriteUseCaseImpl( | ||
weatherRepository: weatherRepository, | ||
); | ||
}); | ||
|
||
tearDown(() { | ||
resetMocktailState(); | ||
}); | ||
|
||
test( | ||
"Given set city favorite use case is called, When no error occurs, Then Result.success is returned", | ||
() async { | ||
// Given | ||
final testCity = city1; | ||
when(() => weatherRepository.setCityAsFavorite(testCity)).justRun(); | ||
|
||
// When | ||
final result = await setCityFavoriteUseCase(param: testCity); | ||
|
||
// Then | ||
expect(result, isA<Success>()); | ||
verify(() => weatherRepository.setCityAsFavorite(testCity)).called(1); | ||
}, | ||
); | ||
|
||
test( | ||
"Given set city favorite use case is called, When error occurs, Then Result.failure is returned", | ||
() async { | ||
// Given | ||
final testCity = city1; | ||
final testException = TestException(); | ||
when(() => weatherRepository.setCityAsFavorite(testCity)) | ||
.thenThrow(testException); | ||
|
||
// When | ||
final result = await setCityFavoriteUseCase(param: testCity); | ||
|
||
// Then | ||
expect(result, isA<Error>()); | ||
expect((result as Error).exception, same(testException)); | ||
verify(() => weatherRepository.setCityAsFavorite(testCity)).called(1); | ||
}, | ||
); | ||
} |
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
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
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
101 changes: 101 additions & 0 deletions
101
test/repository/weather/domain_city_mapper_impl_test.dart
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import 'package:collection/collection.dart'; | ||
import 'package:flutter_template/domain/entity/weather/city.dart'; | ||
import 'package:flutter_template/repository/weather/domain_city_mapper.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../test_models/city_models.dart'; | ||
import '../../test_models/local_city_data_models.dart'; | ||
import '../../test_models/remote_city_models.dart'; | ||
|
||
void main() { | ||
late DomainCityMapper domainCityMapper; | ||
|
||
setUp(() { | ||
domainCityMapper = DomainCityMapperImpl(); | ||
}); | ||
|
||
tearDown(() { | ||
resetMocktailState(); | ||
}); | ||
|
||
test( | ||
"Given local city data, When map called, Then city is returned", | ||
() { | ||
// Given | ||
final localCityData = localCityData1; | ||
final city = city1; | ||
|
||
// When | ||
final result = domainCityMapper.map(localCityData); | ||
|
||
// Then | ||
expect(result, isA<City>()); | ||
expect(result.title, city.title); | ||
expect(result.location, city.location); | ||
expect(result.id, city.id); | ||
expect(result.locationType, city.locationType); | ||
}, | ||
); | ||
|
||
test( | ||
"Given local city data list, When map list called, Then city list is returned", | ||
() { | ||
// Given | ||
final localCityData = allLocalCityDataList; | ||
final cityList = allCityList; | ||
|
||
// When | ||
final result = domainCityMapper.mapList(localCityData); | ||
|
||
// Then | ||
expect(result, isA<List<City>>()); | ||
result.forEachIndexed((index, city) { | ||
expect(city.title, cityList[index].title); | ||
expect(city.location, cityList[index].location); | ||
expect(city.id, cityList[index].id); | ||
expect(city.locationType, cityList[index].locationType); | ||
}); | ||
}, | ||
); | ||
|
||
test( | ||
"Given remote city, When map remote city called, Then city is returned", | ||
() { | ||
// Given | ||
const remoteCityData = remoteCity1; | ||
final city = city1; | ||
|
||
// When | ||
final result = domainCityMapper.mapRemoteCity(remoteCityData); | ||
|
||
// Then | ||
expect(result, isA<City>()); | ||
expect(result.title, city.title); | ||
expect(result.location, city.location); | ||
expect(result.id, city.id); | ||
expect(result.locationType, city.locationType); | ||
}, | ||
); | ||
|
||
test( | ||
"Given local city data list, When map list called, Then city list is returned", | ||
() { | ||
// Given | ||
const remoteCityList = allRemoteCityList; | ||
final cityList = allCityList; | ||
|
||
// When | ||
final result = domainCityMapper.mapRemoteCityList(remoteCityList); | ||
|
||
// Then | ||
expect(result, isA<List<City>>()); | ||
result.forEachIndexed((index, city) { | ||
expect(city.title, cityList[index].title); | ||
expect(city.location, cityList[index].location); | ||
expect(city.id, cityList[index].id); | ||
expect(city.locationType, cityList[index].locationType); | ||
}); | ||
}, | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
test/repository/weather/models/local_city_data_models.dart
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:flutter_template/domain/entity/weather/city.dart'; | ||
|
||
final singleCityList = [ | ||
city1, | ||
]; | ||
|
||
final allCityList = [ | ||
city1, | ||
city2, | ||
]; | ||
|
||
final city1 = City( | ||
id: 1, | ||
title: "title 1", | ||
locationType: "locationType 1", | ||
location: "location 1", | ||
); | ||
|
||
final city2 = City( | ||
id: 2, | ||
title: "title 2", | ||
locationType: "locationType 2", | ||
location: "location 2", | ||
); |
Oops, something went wrong.