Skip to content

Commit

Permalink
Feat/929 tests improve notification showcase (#993)
Browse files Browse the repository at this point in the history
* Notification improvement tests

* Hide copy button on error

* Add keys to pushTokenWidget

* Fix the goldens loading state and remove pumps

* Make shimmer type fixed otherwise goldens generated random one every time and fail
  • Loading branch information
nikolay-vasilev-prime authored Feb 4, 2025
1 parent 68b564e commit 5ff0364
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PushTokenWidget extends StatelessWidget {
@override
Widget build(BuildContext context) => InkWell(
onTap: () {
if (value != null) {
if (value != null && error == null) {
Clipboard.setData(ClipboardData(text: value!));
}
},
Expand Down Expand Up @@ -50,6 +50,8 @@ class PushTokenWidget extends StatelessWidget {
SizedBox(height: context.textFieldDialogTheme.spacingXSS),
ShimmerText(
error ?? value,
type:
ShimmerType.fixed(placeholderLength: label.length),
style: context.textFieldDialogTheme
.editFieldTextNotEditedTextStyle
.copyWith(
Expand All @@ -62,7 +64,7 @@ class PushTokenWidget extends StatelessWidget {
),
),
Visibility(
visible: value != null,
visible: value != null && error == null,
replacement: SizedBox(),
child: Padding(
padding: EdgeInsets.only(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,19 @@ class NotificationsPage extends StatelessWidget {
label: context
.l10n.featureNotifications.notificationTokenLabel,
value: pushToken,
key: const Key('pushTokenSuccessWidget'),
),
buildError: (context, error, bloc) => PushTokenWidget(
label: context
.l10n.featureNotifications.notificationTokenLabel,
error: context.l10n.error.notImplemented,
key: const Key('pushTokenErrorWidget'),
),
buildLoading: (context, bloc) => PushTokenWidget(
label: context
.l10n.featureNotifications.notificationTokenLabel,
value: null,
key: const Key('pushTokenLoadingWidget'),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:rx_bloc/rx_bloc.dart';
import 'package:rxdart/rxdart.dart';
import 'package:{{project_name}}/base/models/errors/error_model.dart';
import 'package:{{project_name}}/feature_notifications/blocs/notifications_bloc.dart';

import 'notifications_bloc_mock.mocks.dart';

@GenerateMocks([
NotificationsBlocEvents,
NotificationsBlocStates,
NotificationsBlocType,
])
NotificationsBlocType notificationsBlocMockFactory({
String? pushToken,
ErrorModel? error,
}) {
final notificationsBloc = MockNotificationsBlocType();
final eventsMock = MockNotificationsBlocEvents();
final statesMock = MockNotificationsBlocStates();

when(notificationsBloc.events).thenReturn(eventsMock);
when(notificationsBloc.states).thenReturn(statesMock);

final pushTokenState = (pushToken != null
? Stream.value(Result.success(pushToken))
: error == null
? Stream<Result<String>>.value(Result.loading())
: Stream<Result<String>>.value(Result<String>.error(error)))
.publishReplay(maxSize: 1)
..connect();

when(statesMock.pushToken).thenAnswer(
(_) => pushTokenState,
);

when(statesMock.errors).thenAnswer(
(_) => error != null ? Stream.value(error) : const Stream.empty(),
);

return notificationsBloc;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


class Stubs {
static final pushToken =
'euJaS9wGTQOuzDmVr8VSci:APA91bFHj6T67ns123mStP2PfberpS_srGCMBcFPSOza0lcLtx_231XFRx7kxcVEfYkYaf-YacRVj3ZRk9kFjh9ZC_1PjH6aFUNaWAns4DfcWHzkEO4wSW8';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{> licence.dart }}

import 'package:flutter/material.dart';
import 'package:flutter_rx_bloc/flutter_rx_bloc.dart';
import 'package:provider/provider.dart';
import 'package:{{project_name}}/base/models/errors/error_model.dart';
import 'package:{{project_name}}/feature_notifications/blocs/notifications_bloc.dart';
import 'package:{{project_name}}/feature_notifications/views/notifications_page.dart';

import '../../mocks/notifications_bloc_mock.dart';

/// wraps a [NotificationsPage] in a [Provider] of type [NotificationsBlocType], creating
/// a mocked bloc depending on the values being tested
Widget notificationsPageFactory({
ErrorModel? error,
String? pushToken,
}) =>
MultiProvider(
providers: [
RxBlocProvider<NotificationsBlocType>.value(
value: notificationsBlocMockFactory(
pushToken: pushToken,
error: error,
),
),
],
child: const NotificationsPage(),
);
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,34 @@
{{> licence.dart }}

import 'package:flutter_test/flutter_test.dart';
import 'package:{{project_name}}/base/models/errors/error_model.dart';

import '../../helpers/golden_helper.dart';
import '../stubs.dart';
import 'factories/notifications_page_factory.dart';

void main() {
group(
'NotificationsPage golden tests',
() => runGoldenTests(
[
buildScenario(
scenario: 'success',
widget: notificationsPageFactory(
pushToken: Stubs.pushToken,
),
),
buildScenario(
scenario: 'error',
widget: notificationsPageFactory(
error: NotFoundErrorModel(message: 'Error message'),
),
),
buildScenario(
scenario: 'loading',
widget: notificationsPageFactory(),
),
],
),
);
}

0 comments on commit 5ff0364

Please sign in to comment.