Merge pull request #232 from BrawlerXull/custom-markdown-tests

Added tests for CustomMarkDown
This commit is contained in:
Ashita Prasad
2024-03-08 11:50:16 +05:30
committed by GitHub
2 changed files with 84 additions and 11 deletions

View File

@ -9,9 +9,12 @@ class CustomMarkdown extends StatelessWidget {
super.key,
required this.data,
this.padding = const EdgeInsets.all(16.0),
this.onTapLink,
});
final String data;
final EdgeInsets padding;
final void Function(String text, String? href, String title)? onTapLink;
@override
Widget build(BuildContext context) {
@ -25,9 +28,10 @@ class CustomMarkdown extends StatelessWidget {
data: data,
selectable: true,
extensionSet: md.ExtensionSet.gitHubFlavored,
onTapLink: (text, href, title) {
launchUrl(Uri.parse(href ?? ""));
},
onTapLink: onTapLink ??
(text, href, title) {
launchUrl(Uri.parse(href ?? ""));
},
builders: {
"inlineButton": InlineButton(),
},

View File

@ -1,16 +1,85 @@
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:apidash/widgets/markdown.dart';
void main() {
testWidgets('Testing CustomMarkdown', (tester) async {
const markdown = CustomMarkdown(data: """Is a markdown ~`star on github`~
group('CustomMarkdown Widget Tests', () {
testWidgets('Testing CustomMarkdown buttons and tags renders',
(tester) async {
const markdown = CustomMarkdown(
data: """Is a markdown ~`star on github`~
#br
#br
#br
#br
~`github repo`~ ~`Discord Server`~""");
await tester.pumpWidget(markdown);
//expectTextStrings(tester.allWidgets, <String>['Data1']);
}, skip: true);
~`github repo`~ ~`Discord Server`~""",
);
await tester.pumpWidget(const MaterialApp(home: markdown));
expect(find.byIcon(Icons.star), findsOneWidget);
expect(find.text("star on github"), findsOneWidget);
expect(find.byIcon(Icons.discord), findsOneWidget);
expect(find.text("Discord Server"), findsOneWidget);
expect(find.byIcon(Icons.code_rounded), findsOneWidget);
expect(find.text("github repo"), findsOneWidget);
expect(find.text('#br'), findsNothing);
});
testWidgets('CustomMarkdown renders correctly',
(WidgetTester tester) async {
await tester.pumpWidget(const MaterialApp(
home: CustomMarkdown(
data: '# Hello World\nThis is some *markdown* text.',
),
));
final headlineTextFinder = find.text('Hello World');
final markdownTextFinder = find.text('This is some markdown text.');
expect(headlineTextFinder, findsOneWidget);
expect(markdownTextFinder, findsOneWidget);
});
testWidgets('CustomMarkdown has proper text rendered',
(WidgetTester tester) async {
await tester.pumpWidget(MaterialApp(
home: GestureDetector(
child: const CustomMarkdown(
data: '[Link Text](https://apidash.dev/)',
),
),
));
await tester.tap(find.text('Link Text'));
await tester.pump();
expect(find.text('Link Text'), findsOneWidget);
expect(find.text('https://apidash.dev/'), findsNothing);
});
testWidgets('CustomMarkdown creates hyperlink',
(WidgetTester tester) async {
bool linkTapped = false;
await tester.pumpWidget(MaterialApp(
home: CustomMarkdown(
data: '[Link Text](https://apidash.dev/)',
onTapLink: (text, href, title) {
linkTapped = true;
expect(text, 'Link Text');
expect(href, 'https://apidash.dev/');
},
),
));
expect(find.byType(Markdown), findsOneWidget);
final markdownWidget = tester.widget<Markdown>(find.byType(Markdown));
expect(markdownWidget.data, '[Link Text](https://apidash.dev/)');
await tester.tap(find.text('Link Text'));
expect(linkTapped, true);
});
});
}