Skip to content

Commit

Permalink
test: simple table content operation - clear content
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Nov 28, 2024
1 parent b9559bf commit b9c9310
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ Node simpleTableBlockNode({
Node createSimpleTableBlockNode({
required int columnCount,
required int rowCount,
String? defaultContent,
}) {
final rows = List.generate(rowCount, (_) {
final cells = List.generate(
columnCount,
(_) => simpleTableCellBlockNode(children: [paragraphNode()]),
(_) => simpleTableCellBlockNode(
children: [paragraphNode(text: defaultContent)],
),
);
return simpleTableRowBlockNode(children: cells);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ extension TableContentOperation on EditorState {
return;
}

if (index < 0 || index >= node.rowLength) {
Log.warn('clear content in row: index out of range: $index');
if (index < 0 || index >= node.columnLength) {
Log.warn('clear content in column: index out of range: $index');
return;
}

Log.info('clear content in row: $index in table ${node.id}');
Log.info('clear content in column: $index in table ${node.id}');

final transaction = this.transaction;
for (var i = 0; i < node.columnLength; i++) {
for (var i = 0; i < node.rowLength; i++) {
final row = node.children[i];
final cell = index >= row.children.length
? row.children.last
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/table/table_operations/table_operations.dart';
import 'package:appflowy_backend/log.dart';
import 'package:flutter_test/flutter_test.dart';

import 'simple_table_test_helper.dart';

void main() {
group('Simple table content operation:', () {
setUpAll(() {
Log.shared.disableLog = true;
});

tearDownAll(() {
Log.shared.disableLog = false;
});

test('clear content at row 1', () async {
const defaultContent = 'default content';
final (editorState, tableNode) = createEditorStateAndTable(
rowCount: 2,
columnCount: 3,
defaultContent: defaultContent,
);
await editorState.clearContentAtRowIndex(tableNode, 0);
for (var i = 0; i < tableNode.rowLength; i++) {
for (var j = 0; j < tableNode.columnLength; j++) {
expect(
tableNode
.getTableCellNode(rowIndex: i, columnIndex: j)
?.children
.first
.delta
?.toPlainText(),
i == 0 ? '' : defaultContent,
);
}
}
});

test('clear content at row 3', () async {
const defaultContent = 'default content';
final (editorState, tableNode) = createEditorStateAndTable(
rowCount: 3,
columnCount: 4,
defaultContent: defaultContent,
);
await editorState.clearContentAtRowIndex(tableNode, 2);
for (var i = 0; i < tableNode.rowLength; i++) {
for (var j = 0; j < tableNode.columnLength; j++) {
expect(
tableNode
.getTableCellNode(rowIndex: i, columnIndex: j)
?.children
.first
.delta
?.toPlainText(),
i == 2 ? '' : defaultContent,
);
}
}
});

test('clear content at column 1', () async {
const defaultContent = 'default content';
final (editorState, tableNode) = createEditorStateAndTable(
rowCount: 2,
columnCount: 3,
defaultContent: defaultContent,
);
await editorState.clearContentAtColumnIndex(tableNode, 0);
for (var i = 0; i < tableNode.rowLength; i++) {
for (var j = 0; j < tableNode.columnLength; j++) {
expect(
tableNode
.getTableCellNode(rowIndex: i, columnIndex: j)
?.children
.first
.delta
?.toPlainText(),
j == 0 ? '' : defaultContent,
);
}
}
});

test('clear content at column 4', () async {
const defaultContent = 'default content';
final (editorState, tableNode) = createEditorStateAndTable(
rowCount: 3,
columnCount: 4,
defaultContent: defaultContent,
);
await editorState.clearContentAtColumnIndex(tableNode, 3);
for (var i = 0; i < tableNode.rowLength; i++) {
for (var j = 0; j < tableNode.columnLength; j++) {
expect(
tableNode
.getTableCellNode(rowIndex: i, columnIndex: j)
?.children
.first
.delta
?.toPlainText(),
j == 3 ? '' : defaultContent,
);
}
}
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:appflowy_editor/appflowy_editor.dart';
(EditorState editorState, Node tableNode) createEditorStateAndTable({
required int rowCount,
required int columnCount,
String? defaultContent,
}) {
final document = Document.blank()
..insert(
Expand All @@ -12,6 +13,7 @@ import 'package:appflowy_editor/appflowy_editor.dart';
createSimpleTableBlockNode(
columnCount: columnCount,
rowCount: rowCount,
defaultContent: defaultContent,
),
],
);
Expand Down

0 comments on commit b9c9310

Please sign in to comment.