Skip to content

Commit

Permalink
✅ Verify storage and session providers are called
Browse files Browse the repository at this point in the history
  • Loading branch information
toureholder committed Jun 18, 2019
1 parent 4714da1 commit 37df33f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 18 deletions.
37 changes: 32 additions & 5 deletions test/home_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -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<List<Donation>> {}

class MockHomeResponseStreamSink extends Mock implements StreamSink<List<Donation>> {}
class MockHomeResponseStreamSink extends Mock
implements StreamSink<List<Donation>> {}

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);
});
Expand All @@ -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<User>());
});

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();
});
}
}
33 changes: 20 additions & 13 deletions test/login_bloc_test.dart
Original file line number Diff line number Diff line change
@@ -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 {}

Expand All @@ -20,26 +19,21 @@ class MockLoginResponseStreamSink extends Mock
implements StreamSink<HttpEvent<LoginResponse>> {}

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);
});
Expand All @@ -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()));

Expand All @@ -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: '[email protected]', password: '123456');
verify(_mockSessionProvider.logUserIn(any));
});

test('closes stream', () async {
await _bloc.dispose();
verify(_mockController.close()).called(1);
});

tearDown(() {
_mockController.close();
});
Expand Down
6 changes: 6 additions & 0 deletions test/test_util/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,3 +18,7 @@ class MockLoginResponseStream extends Mock
implements Stream<HttpEvent<LoginResponse>> {}

class MockNavigatorObserver extends Mock implements NavigatorObserver {}

class MockSessionProvider extends Mock implements SessionProvider {}

class MockDiskStorageProvider extends Mock implements DiskStorageProvider {}
16 changes: 16 additions & 0 deletions test/user_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}

0 comments on commit 37df33f

Please sign in to comment.