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

Unit Tests file for post_modal widget #2264

Closed
Closed
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
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1834,5 +1834,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.2.0-194.0.dev <3.13.0"
flutter: ">=3.13.0"
dart: ">=3.2.0 <3.13.0"
flutter: ">=3.16.0"
143 changes: 143 additions & 0 deletions test/widget_tests/widgets/post_modal_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:talawa/constants/custom_theme.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/models/post/post_model.dart';
import 'package:talawa/services/navigation_service.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/utils/app_localization.dart';
import 'package:talawa/widgets/post_modal.dart';

import '../../helpers/test_helpers.dart';

const Key modalKey = Key("modalKey");
Widget customModalClass([
Function(Post)? function,
Function(Post)? deletePost,
Post? post,
]) {
return MaterialApp(
locale: const Locale('en'),
localizationsDelegates: [
const AppLocalizationsDelegate(isTest: true),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
themeMode: ThemeMode.light,
theme: TalawaTheme.lightTheme,
home: Scaffold(
body: PostBottomModal(
post: post ?? getPostMockModel(),
function: function,
deletePost: deletePost,
),
),
);
}

void main() {
SizeConfig().test();
locator.registerSingleton(NavigationService());

setUp(() {
registerServices();
registerViewModels();
});

tearDown(() {
unregisterViewModels();
unregisterServices();
});

TestWidgetsFlutterBinding.ensureInitialized();
group("Test for PostModal widget", () {
testWidgets('Test if Post Modal is displayed', (WidgetTester tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(customModalClass());
await tester.pump();
expect(find.byKey(modalKey), findsOneWidget);
});
});
});

testWidgets('Check for PostModal children', (tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(customModalClass());
await tester.pump();
expect(
find.descendant(
of: find.byKey(modalKey),
matching: find
.descendant(
of: find.byType(Column),
matching: find.byType(Center),
)
.first,
),
findsWidgets,
);
expect(
find.descendant(
of: find.byType(Center),
matching: find.byType(Row),
),
findsOneWidget,
);
expect(
find
.descendant(
of: find.byType(Row),
matching: find.descendant(
of: find.byType(Padding),
matching: find.byType(Icon),
),
)
.first,
findsOneWidget,
);
expect(
find.descendant(
of: find.byType(Row),
matching: find.byType(TextButton),
),
findsOneWidget,
);
});
});
testWidgets('Check for delete functionality', (tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(customModalClass());
final deleteButton = find.byIcon(Icons.delete);
expect(deleteButton, findsOneWidget);
await tester.tap(find.text('The post was deleted'));
await tester.pump();
expect(find.byType(AlertDialog), findsOneWidget);
await tester.tap(find.text('No'));
await tester.pump();

expect(find.byType(AlertDialog), findsNothing);
await tester.tap(find.text('Yes'));
await tester.pump();

expect(
find.text('Post was deleted if you had the rights!'),
findsOneWidget,
);
expect(find.byType(AlertDialog), findsNothing);
});
});

testWidgets('Test for Report icon', (WidgetTester tester) async {
await tester.runAsync(() async {
await tester.pumpWidget(customModalClass());
await tester.pump();
await tester.tap(find.text('Report the post to the Admin'));
await tester.pump();
expect(
find.text('Your Report has been sent to the Admin'),
findsOneWidget,
);
});
});
}
Loading