-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f095447
commit c0f4d4b
Showing
6 changed files
with
151 additions
and
96 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,56 +1,89 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter_jokes/src/features/jokes/logic/jokes_state.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
|
||
import 'package:errors/errors.dart'; | ||
import 'package:jokes/jokes.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../../lib/src/features/jokes/logic/jokes_provider.dart' | ||
show JokesNotifier; | ||
show JokesNotifier, JokesState; | ||
|
||
class GetJokeMock extends Mock implements GetJoke {} | ||
|
||
void main() { | ||
test('test state of state notifier', () async { | ||
//Setup | ||
final _getJoke = GetJokeMock(); | ||
|
||
final joke = JokeModel( | ||
category: 'Programing', | ||
type: 'twopart', | ||
setup: 'How do you generate a random string?', | ||
delivery: 'Put a Windows user in front of Vim and tell him to exit.', | ||
id: 10, | ||
safe: true, | ||
lang: 'en', | ||
); | ||
|
||
final tJokeStates = <String>[ | ||
JokesState.initial().toString(), | ||
JokesState.loading().toString(), | ||
JokesState(joke: joke).toString() | ||
]; | ||
|
||
when(_getJoke.call()).thenAnswer( | ||
(_) => Future.value( | ||
Right<Failure, JokeModel>(joke), | ||
), | ||
); | ||
|
||
final _jokesNotifier = JokesNotifier(_getJoke); | ||
|
||
final List<String> jokesStates = []; | ||
_jokesNotifier.addListener((state) { | ||
jokesStates.add(state.toString()); | ||
group('JokesNotifier', () { | ||
late GetJokeMock _getJoke; | ||
|
||
setUp(() { | ||
_getJoke = GetJokeMock(); | ||
}); | ||
|
||
///Act | ||
await _jokesNotifier.getJoke(); | ||
test('Emit Initial, Loading, Data when successful', () async { | ||
final joke = JokeModel( | ||
category: 'Programing', | ||
type: 'twopart', | ||
setup: 'How do you generate a random string?', | ||
delivery: 'Put a Windows user in front of Vim and tell him to exit.', | ||
id: 10, | ||
safe: true, | ||
lang: 'en', | ||
); | ||
|
||
final tJokeStates = <JokesState>[ | ||
JokesState.initial(), | ||
JokesState.loading(), | ||
JokesState.data(joke: joke) | ||
]; | ||
|
||
when(() => _getJoke()).thenAnswer( | ||
(_) => Future.value( | ||
Right<Failure, Joke>(joke), | ||
), | ||
); | ||
|
||
final _jokesNotifier = JokesNotifier(getJoke: _getJoke); | ||
|
||
final List<JokesState> jokesStates = []; | ||
_jokesNotifier.addListener((state) { | ||
jokesStates.add(state); | ||
}); | ||
|
||
///Act | ||
await _jokesNotifier.getJoke(); | ||
|
||
///Expect | ||
expect(jokesStates, tJokeStates); | ||
}); | ||
|
||
///Expect | ||
expect(jokesStates, tJokeStates); | ||
test('Emit Initial, Loading, Error when Error Occurs', () async { | ||
//Setup | ||
|
||
final tJokeStates = <JokesState>[ | ||
JokesState.initial(), | ||
JokesState.loading(), | ||
JokesState.error(ServerFailure().toString()), | ||
]; | ||
|
||
when(() => _getJoke()).thenAnswer( | ||
(_) => Future.value( | ||
Left<Failure, Joke>(ServerFailure()), | ||
), | ||
); | ||
|
||
final _jokesNotifier = JokesNotifier(getJoke: _getJoke); | ||
|
||
final List<JokesState> jokesStates = []; | ||
_jokesNotifier.addListener((state) { | ||
jokesStates.add(state); | ||
}); | ||
|
||
///Act | ||
await _jokesNotifier.getJoke(); | ||
|
||
///Expect | ||
expect(jokesStates, tJokeStates); | ||
}); | ||
}); | ||
} | ||
|
||
class GetJokeMock extends Mock implements GetJoke {} |
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 |
---|---|---|
@@ -1,68 +1,92 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:errors/errors.dart'; | ||
import 'package:jokes/jokes.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
import '../../../lib/src/app.dart'; | ||
import '../../../lib/src/features/jokes/logic/jokes_provider.dart'; | ||
import '../../../lib/src/features/jokes/views/joke_page.dart'; | ||
import '../../../lib/src/features/jokes/views/joke_page.i18n.dart'; | ||
|
||
class MockRepository extends Mock implements IJokesRepository {} | ||
class MockGetJoke extends Mock implements GetJoke {} | ||
|
||
void main() { | ||
final getJokeButton = find.byKey(getJokeButtonKey); | ||
final getLoadingIndicator = find.byKey(loadingIndicatorKey); | ||
|
||
testWidgets('Render default get joke message', (tester) async { | ||
// Setup | ||
await tester.pumpWidget(ProviderScope(child: JokesApp())); | ||
//Expect | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsWidgets); | ||
}); | ||
group('JokesPage', () { | ||
testWidgets('Render default get joke message', (tester) async { | ||
// Setup | ||
await tester.pumpWidget(ProviderScope(child: JokesApp())); | ||
//Expect | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsWidgets); | ||
}); | ||
|
||
testWidgets('Loading Indicator when LoadingState', (tester) async { | ||
///act | ||
await tester.pumpWidget( | ||
ProviderScope( | ||
overrides: [ | ||
jokesNotifierProvider.overrideWithProvider( | ||
StateNotifierProvider<JokesNotifier, JokesState>( | ||
(_) => JokesNotifier( | ||
getJoke: MockGetJoke(), | ||
initialState: JokesState.loading(), | ||
), | ||
)) | ||
], | ||
child: JokesApp(), | ||
), | ||
); | ||
|
||
//Validate initial state | ||
expect(getLoadingIndicator, findsWidgets); | ||
}); | ||
|
||
testWidgets('Press button to get a joke', (tester) async { | ||
//Setup | ||
IJokesRepository mockRepository = MockRepository(); | ||
testWidgets('Press button to get a joke', (tester) async { | ||
//Setup | ||
MockGetJoke _getJoke = MockGetJoke(); | ||
|
||
final joke = JokeModel( | ||
category: 'Programing', | ||
type: 'twopart', | ||
setup: 'How do you generate a random string?', | ||
delivery: 'Put a Windows user in front of Vim and tell him to exit.', | ||
id: 10, | ||
safe: true, | ||
lang: 'en', | ||
); | ||
final joke = JokeModel( | ||
category: 'Programing', | ||
type: 'twopart', | ||
setup: 'How do you generate a random string?', | ||
delivery: 'Put a Windows user in front of Vim and tell him to exit.', | ||
id: 10, | ||
safe: true, | ||
lang: 'en', | ||
); | ||
|
||
when(mockRepository.getJoke()).thenAnswer( | ||
(_) => Future.value( | ||
Right<Failure, JokeModel>(joke), | ||
), | ||
); | ||
when(() => _getJoke()).thenAnswer( | ||
(_) => Future.value( | ||
Right<Failure, Joke>(joke), | ||
), | ||
); | ||
|
||
await tester.pumpWidget( | ||
ProviderScope( | ||
overrides: [ | ||
repositoryProvider.overrideWithProvider( | ||
Provider((ref) => mockRepository), | ||
) | ||
], | ||
child: JokesApp(), | ||
), | ||
); | ||
await tester.pumpWidget( | ||
ProviderScope( | ||
overrides: [ | ||
getJokeProvider.overrideWithProvider( | ||
Provider((ref) => _getJoke), | ||
) | ||
], | ||
child: JokesApp(), | ||
), | ||
); | ||
|
||
//Validate initial state | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsWidgets); | ||
expect(getJokeButton, findsWidgets); | ||
//Validate initial state | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsWidgets); | ||
expect(getJokeButton, findsWidgets); | ||
|
||
///Act | ||
await tester.tap(getJokeButton); | ||
await tester.pump(); | ||
///Act | ||
await tester.tap(getJokeButton); | ||
await tester.pump(); | ||
|
||
///Validate new joke | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsNothing); | ||
expect(find.text('How do you generate a random string?'), findsWidgets); | ||
///Validate new joke | ||
expect(find.text('${kTellJokeMessage.i18n}'), findsNothing); | ||
expect(find.text('How do you generate a random string?'), findsWidgets); | ||
}); | ||
}); | ||
} |