Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/onboarding goldens #998

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
);
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;
}
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),
),
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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'),
),
),
),
]);
}