Skip to content

Commit

Permalink
Add golden tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Dec 5, 2024
1 parent e6a06a2 commit 8aa9561
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 10 deletions.
28 changes: 19 additions & 9 deletions forui/lib/src/widgets/sheet/sheets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:forui/forui.dart';
import 'package:forui/src/widgets/sheet/sheet.dart';
import 'package:meta/meta.dart';

/// Shows a sheet that appears above the current widget.
/// Shows a sheet that appears above the current widget. It should have a [FSheets] or [FScaffold] ancestor.
///
/// A closely related widget is a modal sheet which prevents the user from interacting with the rest of the app.
///
Expand Down Expand Up @@ -43,7 +43,7 @@ FSheetController showFSheet({
bool keepAliveOffstage = false,
Key? key,
}) {
final state = context.findAncestorStateOfType<_FSheetsState>();
final state = context.findAncestorStateOfType<FSheetsState>();
if (state == null) {
throw FlutterError.fromParts([
ErrorSummary(
Expand Down Expand Up @@ -86,6 +86,8 @@ FSheetController showFSheet({
style: style,
side: side,
mainAxisMaxRatio: mainAxisMaxRatio,
constraints: constraints,
draggable: draggable,
anchorPoint: anchorPoint,
useSafeArea: useSafeArea,
builder: builder,
Expand Down Expand Up @@ -174,17 +176,19 @@ class FSheets extends StatefulWidget {
const FSheets({required this.child, super.key});

@override
State<FSheets> createState() => _FSheetsState();
State<FSheets> createState() => FSheetsState();
}

class _FSheetsState extends State<FSheets> with TickerProviderStateMixin {
final Map<Key, (FSheetController, Sheet)> _sheets = {};
@visibleForTesting
@internal
class FSheetsState extends State<FSheets> with TickerProviderStateMixin {
final Map<Key, (FSheetController, Sheet)> sheets = {};

@override
Widget build(BuildContext context) => Stack(
children: [
widget.child,
for (final (controller, sheet) in _sheets.values)
for (final (controller, sheet) in sheets.values)
if (controller.shown || controller.keepAliveOffstage || controller._controller.status.isAnimating) sheet,
],
);
Expand All @@ -194,7 +198,7 @@ class _FSheetsState extends State<FSheets> with TickerProviderStateMixin {
return;
}

if (_sheets.containsKey(controller.key)) {
if (sheets.containsKey(controller.key)) {
throw FlutterError.fromParts([
ErrorSummary('showFSheet(...) called with a key that already exists.'),
ErrorDescription(
Expand All @@ -207,12 +211,18 @@ class _FSheetsState extends State<FSheets> with TickerProviderStateMixin {
]);
}

setState(() => _sheets[controller.key] = (controller, sheet));
setState(() => sheets[controller.key] = (controller, sheet));
}

void _remove(Key key) {
if (mounted) {
setState(() => _sheets.remove(key));
setState(() => sheets.remove(key));
}
}

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(DiagnosticsProperty<Map<Key, (FSheetController, Sheet)>>('sheets', sheets));
}
}
2 changes: 1 addition & 1 deletion forui/lib/widgets/sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
library forui.widgets.sheet;

export '../src/widgets/sheet/modal_sheet.dart';
export '../src/widgets/sheet/sheets.dart';
export '../src/widgets/sheet/sheets.dart' hide FSheetsState;
export '../src/widgets/sheet/sheet.dart' show FSheetStyle;
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.
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.
139 changes: 139 additions & 0 deletions forui/test/src/widgets/sheet/sheets_golden_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
@Tags(['golden'])
library;

import 'package:flutter/material.dart';

import 'package:flutter_test/flutter_test.dart';

import 'package:forui/forui.dart';
import '../../test_scaffold.dart';

void main() {
FSheetController? controller;

group('showFSheet', () {
for (final side in Layout.values) {
testWidgets('default - $side', (tester) async {
await tester.pumpWidget(
TestScaffold.app(
child: FSheets(
child: Builder(
builder: (context) => Center(
child: FButton.icon(
child: FIcon(FAssets.icons.chevronRight),
onPress: () {
controller = showFSheet(
context: context,
side: side,
builder: (context) => Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: context.theme.colorScheme.primary),
color: context.theme.colorScheme.background,
),
child: const Center(child: Text('Sheet')),
),
);
},
),
),
),
),
),
);

await tester.tap(find.byType(FButton));
await tester.pumpAndSettle();

await expectLater(find.byType(TestScaffold), matchesGoldenFile('sheet/sheets/default-$side.png'));
});

testWidgets('constrained - $side', (tester) async {
await tester.pumpWidget(
TestScaffold.app(
child: FSheets(
child: Builder(
builder: (context) => Center(
child: FButton.icon(
child: FIcon(FAssets.icons.chevronRight),
onPress: () {
controller = showFSheet(
context: context,
side: side,
constraints: const BoxConstraints(maxHeight: 200, maxWidth: 200),
builder: (context) => Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: context.theme.colorScheme.primary),
color: context.theme.colorScheme.background,
),
child: const Center(child: Text('Sheet')),
),
);
},
),
),
),
),
),
);

await tester.tap(find.byType(FButton));
await tester.pumpAndSettle();

await expectLater(find.byType(TestScaffold), matchesGoldenFile('sheet/sheets/constrained-$side.png'));
});

testWidgets('scrollable - $side', (tester) async {
await tester.pumpWidget(
TestScaffold.app(
child: FSheets(
child: Builder(
builder: (context) => Center(
child: FButton.icon(
child: FIcon(FAssets.icons.chevronRight),
onPress: () {
controller = showFSheet(
context: context,
side: side,
mainAxisMaxRatio: null,
builder: (context) => Container(
height: double.infinity,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: context.theme.colorScheme.primary),
color: context.theme.colorScheme.background,
),
child: ListView.builder(
scrollDirection: side.vertical ? Axis.vertical : Axis.horizontal,
itemBuilder: (context, index) => Padding(
padding: const EdgeInsets.all(8.0),
child: Text('Tile $index'),
),
itemCount: 20,
),
),
);
},
),
),
),
),
),
);

await tester.tap(find.byType(FButton));
await tester.pumpAndSettle();

await expectLater(find.byType(TestScaffold), matchesGoldenFile('sheet/sheets/scrollable-$side.png'));
});
}
});

tearDown(() {
controller?.dispose();
controller = null;
});
}
Loading

0 comments on commit 8aa9561

Please sign in to comment.