From 37df33f57e92c95048c101fc45818b862a189849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tour=C3=A9=20Holder?= Date: Tue, 11 Jun 2019 00:15:26 -0300 Subject: [PATCH] :white_check_mark: Verify storage and session providers are called --- test/home_bloc_test.dart | 37 ++++++++++++++++++++++++++++++++----- test/login_bloc_test.dart | 33 ++++++++++++++++++++------------- test/test_util/mocks.dart | 6 ++++++ test/user_test.dart | 16 ++++++++++++++++ 4 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 test/user_test.dart diff --git a/test/home_bloc_test.dart b/test/home_bloc_test.dart index e738ca8..431460f 100644 --- a/test/home_bloc_test.dart +++ b/test/home_bloc_test.dart @@ -1,28 +1,40 @@ import 'package:flutter_workshop/feature/home/home_bloc.dart'; import 'package:flutter_workshop/model/donation/donation.dart'; import 'package:flutter_workshop/model/donation/donation_api.dart'; +import 'package:flutter_workshop/model/user/user.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'dart:async'; +import 'test_util/mocks.dart'; + class MockDonationApi extends Mock implements DonationApi {} class MockHomeResponseStreamController extends Mock implements StreamController> {} -class MockHomeResponseStreamSink extends Mock implements StreamSink> {} +class MockHomeResponseStreamSink extends Mock + implements StreamSink> {} -void main () { +void main() { MockHomeResponseStreamController _mockController; MockHomeResponseStreamSink _mockSink; MockDonationApi _mockDonationApi; + MockSessionProvider _mockSessionProvider; + MockDiskStorageProvider _mockDiskStorageProvider; HomeBloc _bloc; setUp(() { _mockController = MockHomeResponseStreamController(); _mockSink = MockHomeResponseStreamSink(); _mockDonationApi = MockDonationApi(); - _bloc = HomeBloc(controller: _mockController, donationApi: _mockDonationApi); + _mockSessionProvider = MockSessionProvider(); + _mockDiskStorageProvider = MockDiskStorageProvider(); + _bloc = HomeBloc( + controller: _mockController, + donationApi: _mockDonationApi, + sessionProvider: _mockSessionProvider, + diskStorageProvider: _mockDiskStorageProvider); when(_mockController.sink).thenReturn(_mockSink); }); @@ -47,7 +59,22 @@ void main () { verify(_mockSink.addError(any)).called(1); }); - tearDown((){ + test('recovers user from disk storage', () async { + when(_mockDiskStorageProvider.getUser()).thenReturn(User.fake()); + expect(await _bloc.loadCurrentUser(), isA()); + }); + + test('logs user out from session', () async { + _bloc.logout(); + verify(_mockSessionProvider.logUserOut()).called(1); + }); + + test('closes stream', () async { + await _bloc.dispose(); + verify(_mockController.close()).called(1); + }); + + tearDown(() { _mockController.close(); }); -} \ No newline at end of file +} diff --git a/test/login_bloc_test.dart b/test/login_bloc_test.dart index eca4277..5d77009 100644 --- a/test/login_bloc_test.dart +++ b/test/login_bloc_test.dart @@ -1,15 +1,14 @@ import 'dart:async'; import 'package:flutter_workshop/feature/login/login_bloc.dart'; +import 'package:flutter_workshop/model/login/login_api.dart'; import 'package:flutter_workshop/model/login/login_response.dart'; import 'package:flutter_workshop/model/user/user.dart'; -import 'package:flutter_workshop/service/session.dart'; -import 'package:flutter_workshop/service/shared_preferences_storage.dart'; import 'package:flutter_workshop/util/http_event.dart'; import 'package:mockito/mockito.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:test/test.dart'; -import 'package:flutter_workshop/model/login/login_api.dart'; + +import 'test_util/mocks.dart'; class MockLoginApi extends Mock implements LoginApi {} @@ -20,26 +19,21 @@ class MockLoginResponseStreamSink extends Mock implements StreamSink> {} void main() async { - SharedPreferences.setMockInitialValues({}); MockLoginResponseStreamController _mockController; MockLoginResponseStreamSink _mockSink; MockLoginApi _mockLoginApi; + MockSessionProvider _mockSessionProvider; LoginBloc _bloc; setUp(() async { - final SharedPreferences sharedPreferences = - await SharedPreferences.getInstance(); - - final session = Session( - diskStorageProvider: SharedPreferencesStorage(sharedPreferences)); - _mockController = MockLoginResponseStreamController(); _mockSink = MockLoginResponseStreamSink(); _mockLoginApi = MockLoginApi(); + _mockSessionProvider = MockSessionProvider(); _bloc = LoginBloc( controller: _mockController, loginApi: _mockLoginApi, - sessionProvider: session); + sessionProvider: _mockSessionProvider); when(_mockController.sink).thenReturn(_mockSink); }); @@ -49,7 +43,7 @@ void main() async { verify(_mockLoginApi.login(any)); }); - test('adds events to stream sink if api returns a LoginReponse', () async { + test('adds loading and success events to stream sink if api returns a LoginReponse', () async { when(_mockLoginApi.login(any)) .thenAnswer((_) async => LoginResponse('token', User.fake())); @@ -64,6 +58,19 @@ void main() async { verify(_mockSink.addError(any)).called(1); }); + test('creates session if api returns a LoginReponse', () async { + when(_mockLoginApi.login(any)) + .thenAnswer((_) async => LoginResponse('token', User.fake())); + + await _bloc.login(email: 'test@test.com', password: '123456'); + verify(_mockSessionProvider.logUserIn(any)); + }); + + test('closes stream', () async { + await _bloc.dispose(); + verify(_mockController.close()).called(1); + }); + tearDown(() { _mockController.close(); }); diff --git a/test/test_util/mocks.dart b/test/test_util/mocks.dart index d2cdc69..2f3db4e 100644 --- a/test/test_util/mocks.dart +++ b/test/test_util/mocks.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_workshop/feature/home/home_bloc.dart'; import 'package:flutter_workshop/feature/login/login_bloc.dart'; import 'package:flutter_workshop/model/login/login_response.dart'; +import 'package:flutter_workshop/service/disk_storage_provider.dart'; +import 'package:flutter_workshop/service/session_provider.dart'; import 'package:flutter_workshop/util/http_event.dart'; import 'package:mockito/mockito.dart'; import 'package:http/http.dart' as http; @@ -16,3 +18,7 @@ class MockLoginResponseStream extends Mock implements Stream> {} class MockNavigatorObserver extends Mock implements NavigatorObserver {} + +class MockSessionProvider extends Mock implements SessionProvider {} + +class MockDiskStorageProvider extends Mock implements DiskStorageProvider {} diff --git a/test/user_test.dart b/test/user_test.dart new file mode 100644 index 0000000..b1aadc4 --- /dev/null +++ b/test/user_test.dart @@ -0,0 +1,16 @@ +import 'package:flutter_workshop/model/user/user.dart'; +import 'package:test/test.dart'; + +void main() { + final _encodedUser = '{"id":1,"name":"Test user","image_url":"https://randomuser.me/api/portraits/women/2.jpg"}'; + + test('decodes user', () { + final User result = User.fromEncodedJson(_encodedUser); + expect(result.name, 'Test user'); + }); + + test('encodes user', () { + final String result = User.fake().toEncodedJson(); + expect(result, _encodedUser); + }); +}