forked from vedartm/rick-and-morty-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_repository_test.dart
151 lines (135 loc) 路 5.06 KB
/
home_repository_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import 'package:dartz/dartz.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:rick_and_morty_info/core/error/exceptions.dart';
import 'package:rick_and_morty_info/core/error/failures.dart';
import 'package:rick_and_morty_info/core/network/network_info.dart';
import 'package:rick_and_morty_info/core/types/gender.dart';
import 'package:rick_and_morty_info/core/types/vital_status.dart';
import 'package:rick_and_morty_info/features/home/data/datasources/home_local_datasource.dart';
import 'package:rick_and_morty_info/features/home/data/datasources/home_remote_datasource.dart';
import 'package:rick_and_morty_info/features/home/data/models/character_model.dart';
import 'package:rick_and_morty_info/features/home/data/repositories/home_repository.dart';
import 'package:rick_and_morty_info/features/home/domain/entities/character.dart';
class MockHomeRemoteDataSource extends Mock implements IHomeRemoteDataSource {}
class MockHomeLocalDataSource extends Mock implements IHomeLocalDataSource {}
class MockNetworkInfo extends Mock implements NetworkInfo {}
void main() {
MockHomeLocalDataSource mockLocalDataSource;
MockHomeRemoteDataSource mockRemoteDataSource;
MockNetworkInfo mockNetworkInfo;
HomeRepository repository;
setUp(() {
mockLocalDataSource = MockHomeLocalDataSource();
mockRemoteDataSource = MockHomeRemoteDataSource();
mockNetworkInfo = MockNetworkInfo();
repository = HomeRepository(
mockNetworkInfo,
mockRemoteDataSource,
mockLocalDataSource,
);
});
void runTestsOnline(Function body) {
group('device is online', () {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => true);
});
body();
});
}
void runTestsOffline(Function body) {
group('device is offline', () {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => false);
});
body();
});
}
group('getCharacters', () {
final tPage = 1;
// final tCharacters = [
// Character(
// id: 1,
// name: 'Rick Sanchez',
// vitalStatus: const VitalStatus.alive(),
// gender: const Gender.male(),
// type: '',
// species: 'Human',
// image: 'https://rickandmortyapi.com/api/character/avatar/1.jpeg',
// )
// ];
final tCharacterModels = [
CharacterModel(
id: '1',
name: 'Rick Sanchez',
vitalStatus: const VitalStatus.alive(),
gender: const Gender.male(),
type: '',
species: 'Human',
image: 'https://rickandmortyapi.com/api/character/avatar/1.jpeg',
)
];
test('should check if the device is online', () async {
// arrange
when(mockNetworkInfo.isConnected).thenAnswer((_) async => true);
when(mockRemoteDataSource.getCharacters(tPage))
.thenAnswer((_) async => await tCharacterModels);
// act
await repository.getCharacters(tPage);
// assert
verify(mockNetworkInfo.isConnected);
});
runTestsOnline(() {
test('should get characters when page number is passed', () async {
// arrange
when(mockRemoteDataSource.getCharacters(tPage))
.thenAnswer((_) async => await tCharacterModels);
// act
final result = await repository.getCharacters(tPage);
// assert
expect(result, isA<Right<Failure, List<Character>>>());
});
test('should cache characters when remote data is recieved', () async {
// arrange
when(mockRemoteDataSource.getCharacters(tPage))
.thenAnswer((_) async => await tCharacterModels);
// act
await repository.getCharacters(tPage);
// assert
verify(mockRemoteDataSource.getCharacters(tPage));
verify(mockLocalDataSource.cacheCharacters(tCharacterModels, tPage));
});
test('should return failure when there is an exception', () async {
// arrange
when(mockRemoteDataSource.getCharacters(tPage))
.thenThrow(ServerException());
// act
final result = await repository.getCharacters(tPage);
// assert
verify(mockRemoteDataSource.getCharacters(tPage));
verifyZeroInteractions(mockLocalDataSource);
expect(result, isA<Left<Failure, List<Character>>>());
});
});
runTestsOffline(() {
test('should return cached data when cache is hit', () async {
// arrange
when(mockLocalDataSource.getLastCharacters(tPage))
.thenReturn(tCharacterModels);
// act
final result = await repository.getCharacters(tPage);
// assert
expect(result, isA<Right<Failure, List<Character>>>());
});
test('should return cache failure when cache is miss', () async {
// arrange
when(mockLocalDataSource.getLastCharacters(tPage))
.thenThrow(CacheException());
// act
final result = await repository.getCharacters(tPage);
// assert
expect(result, isA<Left<Failure, List<Character>>>());
});
});
});
}