Skip to content

Commit

Permalink
fix: sub page block review fixes (AppFlowy-IO#6491)
Browse files Browse the repository at this point in the history
* fix: sub page block review fixes

* test: fix tests
  • Loading branch information
Xazin authored Oct 7, 2024
1 parent 9ee39f4 commit 87408fd
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ import '../../shared/util.dart';
// - [x] Redo adding a SubPageBlock (Expect the view to be restored)
// - [x] Redo delete of a SubPageBlock (Expect the view to be moved to trash again)
// - [x] Renaming a child view (Expect the view name to be updated in the document)
// - [x] Deleting a view (to trash) linked to a SubPageBlock shows a hint that the view is in trash (Expect a hint to be shown)
// - [x] Deleting a view (in trash) linked to a SubPageBlock deletes the SubPageBlock (Expect the SubPageBlock to be deleted)
// - [x] Deleting a view (to trash) linked to a SubPageBlock deleted the SubPageBlock (Expect the SubPageBlock to be deleted)
// - [x] Duplicating a SubPageBlock node from Action Menu (Expect a new view is created under current view with same content and name + (copy))
// - [x] Dragging a SubPageBlock node to a new position in the document (Expect everything to be normal)

/// The defaut page name is empty, if we're looking for a "text" we can look for
/// [LocaleKeys.menuAppHeader_defaultNewPageName] but it won't work for eg. hoverOnPageName
/// as it looks at the text provided instead of the actual displayed text.
///
const _defaultPageName = "";

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

Expand Down Expand Up @@ -66,9 +71,9 @@ void main() {
pageName: 'SubPageBlock',
layout: ViewLayoutPB.Document,
);
await tester.pumpAndSettle();

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand All @@ -94,8 +99,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -151,8 +155,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -213,8 +216,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -258,8 +260,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -300,39 +301,6 @@ void main() {
expect(find.text('Child page (copy)'), findsNothing);
});

testWidgets('Undo adding a SubPageBlock', (tester) async {
await tester.initializeAppFlowy();
await tester.tapAnonymousSignInButton();
await tester.createNewPageWithNameUnderParent(name: 'SubPageBlock');

await tester.insertSubPageFromSlashMenu(true);

await tester.expandOrCollapsePage(
pageName: 'SubPageBlock',
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.renamePage('Child page');
await tester.pumpAndSettle();

expect(find.text('Child page'), findsNWidgets(2));

await tester.editor.tapLineOfEditorAt(0);

// Undo
await tester.simulateKeyEvent(
LogicalKeyboardKey.keyZ,
isControlPressed: Platform.isLinux || Platform.isWindows,
isMetaPressed: Platform.isMacOS,
);
await tester.pumpAndSettle();

expect(find.byType(SubPageBlockComponent), findsNothing);
expect(find.text('Child page'), findsNothing);
});

testWidgets('Undo delete of a SubPageBlock', (tester) async {
await tester.initializeAppFlowy();
await tester.tapAnonymousSignInButton();
Expand All @@ -345,8 +313,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand All @@ -370,55 +337,6 @@ void main() {
expect(find.byType(SubPageBlockComponent), findsOneWidget);
});

// Redo: undoing adding a subpage block, then redoing to bring it back
// -> Add a subpage block
// -> Undo
// -> Redo
testWidgets('Redo adding of a SubPageBlock', (tester) async {
await tester.initializeAppFlowy();
await tester.tapAnonymousSignInButton();
await tester.createNewPageWithNameUnderParent(name: 'SubPageBlock');

await tester.insertSubPageFromSlashMenu(true);

await tester.expandOrCollapsePage(
pageName: 'SubPageBlock',
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.renamePage('Child page');
await tester.pumpAndSettle();

expect(find.text('Child page'), findsNWidgets(2));

await tester.editor.tapLineOfEditorAt(0);

// Undo
await tester.simulateKeyEvent(
LogicalKeyboardKey.keyZ,
isControlPressed: Platform.isLinux || Platform.isWindows,
isMetaPressed: Platform.isMacOS,
);
await tester.pumpAndSettle();

expect(find.byType(SubPageBlockComponent), findsNothing);
expect(find.text('Child page'), findsNothing);

// Redo
await tester.simulateKeyEvent(
LogicalKeyboardKey.keyZ,
isShiftPressed: true,
isControlPressed: Platform.isLinux || Platform.isWindows,
isMetaPressed: Platform.isMacOS,
);
await tester.pumpAndSettle();

expect(find.byType(SubPageBlockComponent), findsOneWidget);
expect(find.text('Child page'), findsNWidgets(2));
});

// Redo: undoing deleting a subpage block, then redoing to delete it again
// -> Add a subpage block
// -> Delete
Expand All @@ -436,8 +354,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -476,8 +393,7 @@ void main() {
expect(find.text('Child page'), findsNothing);
});

testWidgets('Delete a view first to trash, then from trash',
(tester) async {
testWidgets('Delete a view from sidebar', (tester) async {
await tester.initializeAppFlowy();
await tester.tapAnonymousSignInButton();
await tester.createNewPageWithNameUnderParent(name: 'SubPageBlock');
Expand All @@ -489,38 +405,17 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

expect(find.text('Child page'), findsNWidgets(2));
expect(find.byType(SubPageBlockComponent), findsOneWidget);

final hintText = LocaleKeys.document_plugins_subPage_inTrashHint.tr();
expect(find.text(hintText), findsNothing);

await tester.hoverOnPageName('Child page');
await tester.tapDeletePageButton();
await tester.pumpAndSettle(const Duration(seconds: 1));

expect(find.text(hintText), findsOne);

// Go to trash
await tester.tapTrashButton();
await tester.pumpAndSettle();

// Tap on delete all button
await tester.tap(find.text(LocaleKeys.trash_deleteAll.tr()));
await tester.pumpAndSettle();

// Tap ok to delete app pages in trash
await tester.tap(find.text(LocaleKeys.button_delete.tr()));
await tester.pumpAndSettle();

await tester.openPage('SubPageBlock');
await tester.pumpAndSettle();

expect(find.text('Child page'), findsNothing);
expect(find.byType(SubPageBlockComponent), findsNothing);
});
Expand All @@ -537,8 +432,7 @@ void main() {
layout: ViewLayoutPB.Document,
);

await tester
.hoverOnPageName(LocaleKeys.menuAppHeader_defaultNewPageName.tr());
await tester.hoverOnPageName(_defaultPageName);
await tester.renamePage('Child page');
await tester.pumpAndSettle();

Expand Down Expand Up @@ -578,10 +472,6 @@ void main() {
expect(afterNode.type, SubPageBlockKeys.type);
expect(afterNode.type, beforeNode.type);
expect(find.byType(SubPageBlockComponent), findsOneWidget);
expect(
find.text(LocaleKeys.document_plugins_subPage_inTrashHint.tr()),
findsNothing,
);
});
});
}
Expand All @@ -602,6 +492,10 @@ extension _SubPageTestHelper on WidgetTester {
offset: 100,
);

// Navigate to the previous page to see the SubPageBlock
await openPage('SubPageBlock');
await pumpAndSettle();

await pumpUntilFound(find.byType(SubPageBlockComponent));
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import 'package:flutter/material.dart';

import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/mobile/application/mobile_router.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/block_transaction_handler/block_transaction_handler.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_page_block.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/sub_page/sub_page_block_component.dart';
import 'package:appflowy/plugins/trash/application/trash_service.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/workspace/application/tabs/tabs_bloc.dart';
import 'package:appflowy/workspace/application/view/view_service.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pbenum.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flowy_infra_ui/style_widget/snap_bar.dart';
import 'package:universal_platform/universal_platform.dart';

class SubPageBlockTransactionHandler extends BlockTransactionHandler {
SubPageBlockTransactionHandler() : super(blockType: SubPageBlockKeys.type);
Expand Down Expand Up @@ -138,19 +142,29 @@ class SubPageBlockTransactionHandler extends BlockTransactionHandler {
final viewOrResult = await ViewBackendService.createView(
layoutType: ViewLayoutPB.Document,
parentViewId: parentViewId,
name: LocaleKeys.menuAppHeader_defaultNewPageName.tr(),
name: '',
);

await viewOrResult.fold(
(view) async {
final transaction = editorState.transaction
..updateNode(node, {SubPageBlockKeys.viewId: view.id});
await editorState.apply(
await editorState
.apply(
transaction,
withUpdateSelection: false,
options: const ApplyOptions(recordUndo: false),
);
editorState.reload();
)
.then((_) async {
editorState.reload();

// Open the new page
if (UniversalPlatform.isDesktop) {
getIt<TabsBloc>().openPlugin(view);
} else {
await context.pushView(view);
}
});
},
(error) async {
Log.error(error);
Expand Down
Loading

0 comments on commit 87408fd

Please sign in to comment.