-
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
Showing
4 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...icks/feature_onboarding/__brick__/test/feature_onboarding/factory/onboarding_factory.dart
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,35 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_rx_bloc/flutter_rx_bloc.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:testapp/base/models/errors/error_model.dart'; | ||
import 'package:testapp/base/models/user_with_auth_token_model.dart'; | ||
import 'package:testapp/feature_onboarding/blocs/onboarding_bloc.dart'; | ||
import 'package:testapp/feature_onboarding/views/onboarding_page.dart'; | ||
|
||
import '../mock/onboarding_mock.dart'; | ||
|
||
/// Change the parameters according the the needs of the test | ||
Widget onboardingFactory({ | ||
String? email, | ||
String? password, | ||
UserWithAuthTokenModel? registered, | ||
bool? showFieldErrors, | ||
bool? isLoading, | ||
ErrorModel? errors, | ||
ErrorModel? resumeOnboardingErrors, | ||
}) => | ||
Scaffold( | ||
body: MultiProvider(providers: [ | ||
RxBlocProvider<OnboardingBlocType>.value( | ||
value: onboardingMockFactory( | ||
email: email, | ||
password: password, | ||
registered: registered, | ||
showFieldErrors: showFieldErrors, | ||
isLoading: isLoading, | ||
errors: errors, | ||
resumeOnboardingErrors: resumeOnboardingErrors, | ||
), | ||
), | ||
], child: const OnboardingPage()), | ||
); |
71 changes: 71 additions & 0 deletions
71
...tes/bricks/feature_onboarding/__brick__/test/feature_onboarding/mock/onboarding_mock.dart
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,71 @@ | ||
import 'package:mockito/annotations.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
import 'package:testapp/base/models/errors/error_model.dart'; | ||
import 'package:testapp/base/models/user_with_auth_token_model.dart'; | ||
import 'package:testapp/feature_onboarding/blocs/onboarding_bloc.dart'; | ||
|
||
import 'onboarding_mock.mocks.dart'; | ||
|
||
@GenerateMocks([OnboardingBlocStates, OnboardingBlocEvents, OnboardingBlocType]) | ||
OnboardingBlocType onboardingMockFactory({ | ||
String? email, | ||
String? password, | ||
UserWithAuthTokenModel? registered, | ||
bool? showFieldErrors, | ||
bool? isLoading, | ||
ErrorModel? errors, | ||
ErrorModel? resumeOnboardingErrors, | ||
}) { | ||
final blocMock = MockOnboardingBlocType(); | ||
final eventsMock = MockOnboardingBlocEvents(); | ||
final statesMock = MockOnboardingBlocStates(); | ||
|
||
when(blocMock.events).thenReturn(eventsMock); | ||
when(blocMock.states).thenReturn(statesMock); | ||
|
||
final emailState = email != null | ||
? Stream.value(email).shareReplay(maxSize: 1) | ||
: const Stream<String>.empty(); | ||
|
||
final passwordState = password != null | ||
? Stream.value(password).shareReplay(maxSize: 1) | ||
: const Stream<String>.empty(); | ||
|
||
final registeredState = (registered != null | ||
? Stream.value(registered) | ||
: const Stream<UserWithAuthTokenModel>.empty()) | ||
.publishReplay(maxSize: 1) | ||
..connect(); | ||
|
||
final showFieldErrorsState = showFieldErrors != null | ||
? Stream.value(showFieldErrors).shareReplay(maxSize: 1) | ||
: const Stream<bool>.empty(); | ||
|
||
final isLoadingState = isLoading != null | ||
? Stream.value(isLoading).shareReplay(maxSize: 1) | ||
: const Stream<bool>.empty(); | ||
|
||
final errorsState = errors != null | ||
? Stream.value(errors).shareReplay(maxSize: 1) | ||
: const Stream<ErrorModel>.empty(); | ||
|
||
final resumeOnboardingErrorsState = resumeOnboardingErrors != null | ||
? Stream.value(resumeOnboardingErrors).shareReplay(maxSize: 1) | ||
: const Stream<ErrorModel>.empty(); | ||
|
||
final onRoutingState = const Stream<void>.empty().publishReplay(maxSize: 1) | ||
..connect(); | ||
|
||
when(statesMock.email).thenAnswer((_) => emailState); | ||
when(statesMock.password).thenAnswer((_) => passwordState); | ||
when(statesMock.registered).thenAnswer((_) => registeredState); | ||
when(statesMock.showFieldErrors).thenAnswer((_) => showFieldErrorsState); | ||
when(statesMock.isLoading).thenAnswer((_) => isLoadingState); | ||
when(statesMock.errors).thenAnswer((_) => errorsState); | ||
when(statesMock.resumeOnboardingErrors) | ||
.thenAnswer((_) => resumeOnboardingErrorsState); | ||
when(statesMock.onRouting).thenAnswer((_) => onRoutingState); | ||
|
||
return blocMock; | ||
} |
23 changes: 23 additions & 0 deletions
23
...li/mason_templates/bricks/feature_onboarding/__brick__/test/feature_onboarding/stubs.dart
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,23 @@ | ||
import 'package:testapp/base/models/confirmed_credentials_model.dart'; | ||
import 'package:testapp/base/models/user_model.dart'; | ||
import 'package:testapp/base/models/user_role.dart'; | ||
import 'package:testapp/base/models/user_with_auth_token_model.dart'; | ||
import 'package:testapp/lib_auth/models/auth_token_model.dart'; | ||
|
||
class Stubs { | ||
static const email = 'example@example.com'; | ||
|
||
static const password = 'password'; | ||
|
||
static final user = UserWithAuthTokenModel( | ||
authToken: AuthTokenModel('token', 'refreshToken'), | ||
user: UserModel( | ||
id: 'id', | ||
email: email, | ||
phoneNumber: password, | ||
role: UserRole.user, | ||
confirmedCredentials: | ||
ConfirmedCredentialsModel(email: true, phone: false), | ||
), | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
...cks/feature_onboarding/__brick__/test/feature_onboarding/view/onboarding_golden_test.dart
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,38 @@ | ||
import 'package:testapp/base/models/errors/error_model.dart'; | ||
|
||
import '../../helpers/golden_helper.dart'; | ||
import '../factory/onboarding_factory.dart'; | ||
import '../stubs.dart'; | ||
|
||
void main() { | ||
runGoldenTests([ | ||
buildScenario( | ||
customPumpBeforeTest: (widgetTester) => widgetTester.pump( | ||
const Duration(milliseconds: 350), | ||
), | ||
scenario: 'onboarding_success', | ||
widget: onboardingFactory( | ||
email: Stubs.email, | ||
password: Stubs.password, | ||
registered: Stubs.user, | ||
), | ||
), | ||
buildScenario( | ||
customPumpBeforeTest: (widgetTester) => widgetTester.pump( | ||
const Duration(milliseconds: 350), | ||
), | ||
scenario: 'onboarding_loading', | ||
widget: onboardingFactory( | ||
isLoading: true, | ||
), | ||
), | ||
buildScenario( | ||
scenario: 'onboarding_error', | ||
widget: onboardingFactory( | ||
errors: UnknownErrorModel( | ||
exception: Exception('Something went wrong'), | ||
), | ||
), | ||
), | ||
]); | ||
} |