Skip to content

Commit

Permalink
test: wrote content and accessibility tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DE7924 committed Nov 14, 2024
1 parent 6c2ed11 commit cd843a6
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/src/components/breadcrumb/breadcrumb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ZetaBreadcrumb extends ZetaStatefulWidget {
/// Breadcrumb children
final List<ZetaBreadcrumbItem> children;

/// Label passed to wrapping [Semantics] widget.
/// Semantic ;abel passed to [_TruncatedItem].
/// {@macro zeta-widget-semantic-label}
final String? moreSemanticLabel;

Expand Down Expand Up @@ -119,7 +119,8 @@ class _ZetaBreadcrumbsState extends State<ZetaBreadcrumb> {
for (final (index, element) in children.sublist(1, children.length - (widget.maxItemsShown - 1)).indexed) {
truncatedChildren.add(createBreadCrumb(element, index + 1));
}
returnList.add(_TruncatedItem(semanticLabel: widget.moreSemanticLabel, children: truncatedChildren));
returnList
.add(_TruncatedItem(semanticLabel: widget.moreSemanticLabel ?? 'View More', children: truncatedChildren));

for (final (index, element) in children.sublist(children.length - (widget.maxItemsShown - 1)).indexed) {
returnList.add(createBreadCrumb(element, index + children.length - (widget.maxItemsShown)));
Expand Down Expand Up @@ -252,7 +253,7 @@ class _TruncatedItem extends StatefulWidget {
///Constructor for [_TruncatedItem]
const _TruncatedItem({
required this.children,
this.semanticLabel,
required this.semanticLabel,
});

///Breadcrumb children
Expand All @@ -261,7 +262,7 @@ class _TruncatedItem extends StatefulWidget {
/// Label passed to wrapping [Semantics] widget.
///
/// {@macro zeta-widget-semantic-label}
final String? semanticLabel;
final String semanticLabel;

@override
State<_TruncatedItem> createState() => _TruncatedItemState();
Expand All @@ -287,6 +288,7 @@ class _TruncatedItemState extends State<_TruncatedItem> {
child: Semantics(
label: widget.semanticLabel,
button: true,
excludeSemantics: true,
child: FilledButton(
onPressed: () {
setState(() {
Expand Down
165 changes: 165 additions & 0 deletions test/src/components/breadcrumb/breadcrumb_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:zeta_flutter/zeta_flutter.dart';

import '../../../test_utils/test_app.dart';
import '../../../test_utils/tolerant_comparator.dart';
import '../../../test_utils/utils.dart';

void main() {
const String parentFolder = 'breadcrumb';

const goldenFile = GoldenFiles(component: parentFolder);
setUpAll(() {
goldenFileComparator = TolerantComparator(goldenFile.uri);
});

final List<ZetaBreadcrumbItem> children = [
ZetaBreadcrumbItem(
label: 'Breadcrumb',
onPressed: () {},
icon: ZetaIcons.star,
),
ZetaBreadcrumbItem(label: 'Item 1', onPressed: () {}),
ZetaBreadcrumbItem(label: 'Item 2', onPressed: () {}),
ZetaBreadcrumbItem(
label: 'Item 3',
onPressed: () {},
icon: ZetaIcons.star,
),
ZetaBreadcrumbItem(label: 'Item 4', onPressed: () {}),
ZetaBreadcrumbItem(label: 'Item 5', onPressed: () {}),
ZetaBreadcrumbItem(label: 'Item 6', onPressed: () {}),
];

group('Accessibility Tests', () {
testWidgets('ZetaBreadcrumb meets accessibility standards', (tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
await tester.pumpWidget(
TestApp(
home: ZetaBreadcrumb(
children: children,
),
),
);

await expectLater(tester, meetsGuideline(androidTapTargetGuideline));
await expectLater(tester, meetsGuideline(iOSTapTargetGuideline));
await expectLater(tester, meetsGuideline(labeledTapTargetGuideline));
await expectLater(tester, meetsGuideline(textContrastGuideline));

handle.dispose();
});
});

group('Content Tests', () {
final debugFillProperties = {
'children': children.toString().replaceAll('[', '').replaceAll(']', ''),
};
debugFillPropertiesTest(
ZetaBreadcrumb(children: children),
debugFillProperties,
);

// has the correct number of children
testWidgets('ZetaBreadcrumb has the correct number of children', (tester) async {
const maxItemsShown = 2;

await tester.pumpWidget(
TestApp(
home: ZetaBreadcrumb(
maxItemsShown: maxItemsShown,
children: children,
),
),
);
expect(find.byType(ZetaBreadcrumbItem), findsNWidgets(maxItemsShown));

// ignore: deprecated_member_use_from_same_package
await tester.tap(find.byType(TruncatedItem));
await tester.pumpAndSettle();

expect(find.byType(ZetaBreadcrumbItem), findsNWidgets(children.length));
});

// children have the correct labels
testWidgets('ZetaBreadcrumb children have the correct labels', (tester) async {
await tester.pumpWidget(
TestApp(
home: ZetaBreadcrumb(
children: children,
),
),
);

expect(find.text('Breadcrumb'), findsOneWidget);
expect(find.text('Item ${children.length - 1}'), findsOneWidget);

// ignore: deprecated_member_use_from_same_package
await tester.tap(find.byType(TruncatedItem));
await tester.pumpAndSettle();

for (int i = 0; i < children.length; i++) {
expect(find.text(children[i].label), findsOneWidget);
}
});

// children have the correct icons (if any)
testWidgets(' ZetaBreadcrumb children have the correct icons', (tester) async {
await tester.pumpWidget(
TestApp(
home: ZetaBreadcrumb(
children: children,
),
),
);

final iconFinder = find.byWidgetPredicate((widget) {
if (widget is ZetaIcon) {
return widget.icon == ZetaIcons.star;
}
return false;
});
expect(iconFinder, findsOneWidget);

// ignore: deprecated_member_use_from_same_package
await tester.tap(find.byType(TruncatedItem));
await tester.pumpAndSettle();

final iconFinder1 = find.byWidgetPredicate((widget) {
if (widget is ZetaIcon) {
return widget.icon == ZetaIcons.star;
}
return false;
});
expect(iconFinder1, findsNWidgets(2));
});
});

group('Dimensions Tests', () {
// each item is equally spaced
});

group('Styling Tests', () {
// label and icon colors are correct
// label font styles are correct
// icon sizes are correct

// hovered state label and icon colors are correct
// selected state label and icon colors are correct
});

group('Interaction Tests', () {
// clicking on an item calls the onPressed callback
// clicking on an item removes the children list after it
});

group('Golden Tests', () {
// goldenTest(goldenFile, widget, 'PNG_FILE_NAME');
});

group('Performance Tests', () {});
}

0 comments on commit cd843a6

Please sign in to comment.