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

Unittest for pinned post #2177

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
51 changes: 29 additions & 22 deletions lib/widgets/pinned_post.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/models/post/post_model.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/views/after_auth_screens/feed/pinned_post_screen.dart';

/// a_line_ending_with_end_punctuation.
///
Expand Down Expand Up @@ -86,6 +86,7 @@ class PinnedPost extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
key: const Key('hello'),
child: pinnedPosts.isNotEmpty
? SizedBox(
height: SizeConfig.screenHeight! * 0.25,
Expand All @@ -100,37 +101,43 @@ class PinnedPost extends StatelessWidget {
child: GestureDetector(
onTap: () {
// final Map<String, dynamic> arg = {"index": "$index","post": pinnedPosts};
navigationService.pushScreen(
'/pinnedpostscreen',
arguments: pinnedPosts[index],
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
PinnedPostScreen(post: pinnedPosts[index]),
),
);
},
child: SizedBox(
width: SizeConfig.screenWidth! / 4.1,
child: Column(
children: [
CachedNetworkImage(
cacheKey: pinnedPosts[index]['postId'],
imageUrl:
(pinnedPosts[index]['imageUrl'] ?? '').isEmpty
? 'placeHolderUrl'
: pinnedPosts[index]['imageUrl']!,
errorWidget: (context, url, error) {
return const CircularProgressIndicator();
},
height: SizeConfig.screenHeight! * 0.15,
fit: BoxFit.cover,
Expanded(
child: CachedNetworkImage(
cacheKey: pinnedPosts[index]['postId'],
imageUrl:
(pinnedPosts[index]['imageUrl'] ?? '').isEmpty
? 'placeHolderUrl'
: pinnedPosts[index]['imageUrl']!,
errorWidget: (context, url, error) {
return const CircularProgressIndicator();
},
height: SizeConfig.screenHeight! * 0.15,
fit: BoxFit.cover,
),
),
const SizedBox(height: 5),
Row(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${pinnedPosts[index]['time']!}hr',
style: const TextStyle(
color: Colors.grey,
fontWeight: FontWeight.w200,
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
'${pinnedPosts[index]['time']!}hr',
style: const TextStyle(
color: Colors.grey,
fontWeight: FontWeight.w200,
),
),
),
),
Expand Down
87 changes: 87 additions & 0 deletions test/widget_tests/widgets/pinned_post_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:talawa/demo_server_data/pinned_post_demo_data.dart';
import 'package:talawa/locator.dart';
import 'package:talawa/models/post/post_model.dart';
import 'package:talawa/services/size_config.dart';
import 'package:talawa/views/after_auth_screens/feed/pinned_post_screen.dart';
import 'package:talawa/widgets/pinned_post.dart';

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

/// List of pinned posts.
List<Post> _pinnedPosts =
pinnedPostsDemoData.map((e) => Post.fromJson(e)).toList();

/// getter for pinned post.
///
/// **params**:

List<Post> get pinnedPosts {
return _pinnedPosts;
}

/// main function.
///
/// **params**:
/// None
///
/// **returns**:
/// None
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setupLocator();
locator<SizeConfig>().test();
setUp(() {
registerServices();
locator<SizeConfig>().test();
});
tearDown(() {
unregisterServices();
});

testWidgets('If conatiner is coming on calling pinnedwidget',
(widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: PinnedPost(pinnedPost: pinnedPosts),
),
);
await widgetTester.pumpAndSettle();
expect(find.byKey(const Key('hello')), findsOneWidget);
});

testWidgets('Text widget is present when there are pinned posts',
(widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: PinnedPost(pinnedPost: pinnedPosts),
),
);
await widgetTester.pumpAndSettle();
expect(find.byType(Text), findsWidgets);
});

testWidgets('Text widget displays the correct text', (widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: PinnedPost(pinnedPost: pinnedPosts),
),
);
await widgetTester.pumpAndSettle();
expect(find.text('Church Meeting'), findsOneWidget);
});

testWidgets('Tapping on a post triggers navigation', (widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: PinnedPost(pinnedPost: pinnedPosts),
),
);
await widgetTester.pump();
await widgetTester.tap(find.text('Church Meeting'));
await widgetTester.pumpAndSettle();
expect(find.byType(PinnedPost), findsNothing);
expect(find.byType(PinnedPostScreen), findsOneWidget);
});
}
Loading