From 61b66291cb8cd567f4843699dffba5be75b5cb20 Mon Sep 17 00:00:00 2001 From: Mathias Mogensen Date: Wed, 9 Oct 2024 16:18:07 +0200 Subject: [PATCH] test: filter+calculations --- .../desktop/grid/grid_calculations_test.dart | 109 +++++++++++++++++- .../integration_test/desktop_runner_4.dart | 2 + .../widgets/calculations/calculate_cell.dart | 5 +- 3 files changed, 109 insertions(+), 7 deletions(-) diff --git a/frontend/appflowy_flutter/integration_test/desktop/grid/grid_calculations_test.dart b/frontend/appflowy_flutter/integration_test/desktop/grid/grid_calculations_test.dart index 2eaa7ea6a511..8bc6e8eb29ee 100644 --- a/frontend/appflowy_flutter/integration_test/desktop/grid/grid_calculations_test.dart +++ b/frontend/appflowy_flutter/integration_test/desktop/grid/grid_calculations_test.dart @@ -1,6 +1,9 @@ +import 'package:flutter/services.dart'; + +import 'package:appflowy/plugins/database/grid/presentation/widgets/filter/choicechip/number.dart'; import 'package:appflowy_backend/protobuf/flowy-database2/protobuf.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/view.pbenum.dart'; -import 'package:flutter/services.dart'; +import 'package:flowy_infra_ui/flowy_infra_ui.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -19,9 +22,6 @@ void main() { // Change one Field to Number await tester.changeFieldTypeOfFieldWithName('Type', FieldType.Number); - - expect(find.text('Calculate'), findsOneWidget); - await tester.changeCalculateAtIndex(1, CalculationType.Sum); // Enter values in cells @@ -55,7 +55,7 @@ void main() { await tester.changeFieldTypeOfFieldWithName('Type', FieldType.Number); await tester.changeFieldTypeOfFieldWithName('Done', FieldType.Number); - expect(find.text('Calculate'), findsNWidgets(2)); + expect(find.text('Calculate'), findsNWidgets(3)); await tester.changeCalculateAtIndex(1, CalculationType.Sum); await tester.changeCalculateAtIndex(2, CalculationType.Min); @@ -103,5 +103,104 @@ void main() { expect(find.text('150'), findsNWidgets(2)); expect(find.text('100'), findsNWidgets(2)); }); + + testWidgets('Calculations with filter', (tester) async { + await tester.initializeAppFlowy(); + await tester.tapAnonymousSignInButton(); + + await tester.createNewPageWithNameUnderParent(layout: ViewLayoutPB.Grid); + + // Change two Fields to Number + await tester.changeFieldTypeOfFieldWithName('Type', FieldType.Number); + await tester.changeFieldTypeOfFieldWithName('Done', FieldType.Number); + + expect(find.text('Calculate'), findsNWidgets(3)); + + await tester.changeCalculateAtIndex(1, CalculationType.Sum); + await tester.changeCalculateAtIndex(2, CalculationType.Min); + + // Enter values in cells + await tester.editCell( + rowIndex: 0, + fieldType: FieldType.Number, + input: '100', + ); + await tester.editCell( + rowIndex: 1, + fieldType: FieldType.Number, + input: '150', + ); + await tester.editCell( + rowIndex: 2, + fieldType: FieldType.Number, + input: '100', + ); + await tester.editCell( + rowIndex: 0, + fieldType: FieldType.Number, + input: '150', + cellIndex: 1, + ); + await tester.editCell( + rowIndex: 1, + fieldType: FieldType.Number, + input: '100', + cellIndex: 1, + ); + await tester.editCell( + rowIndex: 2, + fieldType: FieldType.Number, + input: '50', + cellIndex: 1, + ); + await tester.pumpAndSettle(); + + // Dismiss edit cell + await tester.sendKeyDownEvent(LogicalKeyboardKey.enter); + await tester.pumpAndSettle(const Duration(seconds: 1)); + + // Expect sum to be 100 + 150 + 100 = 350 + expect(find.text('350'), findsOneWidget); + + // Expect min to be 50 + expect(find.text('50'), findsNWidgets(2)); + + await tester.tapDatabaseFilterButton(); + await tester.tapCreateFilterByFieldType(FieldType.Number, 'Type'); + await tester.pumpAndSettle(); + + await tester.tap(find.byType(NumberFilterChoiceChip)); + await tester.pumpAndSettle(); + + await tester.enterText( + find.descendant( + of: find.byType(NumberFilterEditor), + matching: find.byType(FlowyTextField), + ), + '100', + ); + await tester.pumpAndSettle(const Duration(seconds: 2)); + + // Expect the sum to be 100+100 = 200 + expect(find.text('200'), findsOneWidget); + + // Expect the min to be 50 + expect(find.text('50'), findsNWidgets(2)); + + await tester.enterText( + find.descendant( + of: find.byType(NumberFilterEditor), + matching: find.byType(FlowyTextField), + ), + '150', + ); + await tester.pumpAndSettle(const Duration(seconds: 2)); + + // Expect the sum to be 150 (3 times, text field, cell, and calculate cell) + expect(find.text('150'), findsNWidgets(3)); + + // Expect the min to be 100 + expect(find.text('100'), findsNWidgets(2)); + }); }); } diff --git a/frontend/appflowy_flutter/integration_test/desktop_runner_4.dart b/frontend/appflowy_flutter/integration_test/desktop_runner_4.dart index 26963d0e72ee..6e39e51de113 100644 --- a/frontend/appflowy_flutter/integration_test/desktop_runner_4.dart +++ b/frontend/appflowy_flutter/integration_test/desktop_runner_4.dart @@ -1,6 +1,7 @@ import 'package:integration_test/integration_test.dart'; import 'desktop/document/document_test_runner_2.dart' as document_test_runner_2; +import 'desktop/grid/grid_calculations_test.dart' as grid_calculations_test; import 'desktop/uncategorized/empty_test.dart' as first_test; Future main() async { @@ -14,4 +15,5 @@ Future runIntegration4OnDesktop() async { first_test.main(); document_test_runner_2.main(); + grid_calculations_test.main(); } diff --git a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/calculations/calculate_cell.dart b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/calculations/calculate_cell.dart index 1040081d518b..32783a859779 100644 --- a/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/calculations/calculate_cell.dart +++ b/frontend/appflowy_flutter/lib/plugins/database/grid/presentation/widgets/calculations/calculate_cell.dart @@ -1,3 +1,5 @@ +import 'package:flutter/material.dart'; + import 'package:appflowy/plugins/database/application/calculations/calculation_type_ext.dart'; import 'package:appflowy/plugins/database/application/field/field_info.dart'; import 'package:appflowy/plugins/database/application/field/type_option/number_format_bloc.dart'; @@ -13,7 +15,6 @@ import 'package:appflowy_backend/protobuf/flowy-database2/number_entities.pb.dar import 'package:appflowy_popover/appflowy_popover.dart'; import 'package:flowy_infra/theme_extension.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class CalculateCell extends StatefulWidget { @@ -126,7 +127,7 @@ class _CalculateCellState extends State { } Widget _showCalculateValue(BuildContext context, String? prefix) { - prefix = prefix != null ? '$prefix ' : ''; + prefix = prefix != null && prefix.isNotEmpty ? '$prefix ' : ''; final calculateValue = '$prefix${_withoutTrailingZeros(widget.calculation!.value)}';