-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Verify storage and session providers are called
- Loading branch information
1 parent
4714da1
commit 37df33f
Showing
4 changed files
with
74 additions
and
18 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
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 {} | ||
|
||
|
@@ -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); | ||
}); | ||
|
@@ -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: '[email protected]', password: '123456'); | ||
verify(_mockSessionProvider.logUserIn(any)); | ||
}); | ||
|
||
test('closes stream', () async { | ||
await _bloc.dispose(); | ||
verify(_mockController.close()).called(1); | ||
}); | ||
|
||
tearDown(() { | ||
_mockController.close(); | ||
}); | ||
|
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,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); | ||
}); | ||
} |