Skip to content

Commit

Permalink
test: for cursor left sentence delete
Browse files Browse the repository at this point in the history
  • Loading branch information
MayurSMahajan committed Apr 19, 2023
1 parent 902475e commit 54c6ac1
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ShortcutEventHandler cursorLeftWordDelete = (editorState, event) {

final textNode = textNodes.first;

//we store the position of the index where the current word ends.
var startOfWord =
selection.end.goLeft(editorState, selectionRange: SelectionRange.word);

Expand All @@ -33,12 +34,13 @@ ShortcutEventHandler cursorLeftWordDelete = (editorState, event) {
startOffset: startOfWord.offset,
);

//we need to check if this selection is not null
//we need to check if this position is not null
final newStartOfWord = newSelection.end.goLeft(
editorState,
selectionRange: SelectionRange.word,
);

//this handles the edge case where the textNode only consists single space.
if (newStartOfWord != null) {
startOfWord = newStartOfWord;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ void main() async {
final editor = tester.editor..insertTextNode(text);

await editor.startTesting();
var selection = Selection.single(path: [0], startOffset: text.length);
await editor.updateSelection(selection);
await editor.updateSelection(
Selection.single(path: [0], startOffset: text.length),
);

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
Expand All @@ -37,12 +38,11 @@ void main() async {
var nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
var textNode = nodes.whereType<TextNode>().first;
var newText = textNode.toPlainText();

words.removeLast();
//expected: Welcome_to_Appflowy_
//here _ actually represents ' '
expect(newText, words.join());
expect(textNode.toPlainText(), words.join());

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
Expand All @@ -60,13 +60,11 @@ void main() async {
nodes = editor.editorState.service.selectionService.currentSelectedNodes;
textNode = nodes.whereType<TextNode>().first;

newText = textNode.toPlainText();

//removes the whitespace
words.removeLast();
words.removeLast();
//expected is: Welcome_to_
expect(newText, words.join());
expect(textNode.toPlainText(), words.join());

//we divide words.length by 2 becuase we know half words are whitespaces.
for (var i = 0; i < words.length / 2; i++) {
Expand All @@ -86,18 +84,17 @@ void main() async {
nodes = editor.editorState.service.selectionService.currentSelectedNodes;
textNode = nodes.whereType<TextNode>().toList(growable: false).first;

newText = textNode.toPlainText();

expect(newText, '');
expect(textNode.toPlainText(), '');
});

testWidgets('ctrl+backspace in the middle of a word', (tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor..insertTextNode(text);

await editor.startTesting();
var selection = Selection.single(path: [0], startOffset: 0);
await editor.updateSelection(selection);
await editor.updateSelection(
Selection.single(path: [0], startOffset: 0),
);

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
Expand All @@ -115,13 +112,13 @@ void main() async {
var nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
var textNode = nodes.whereType<TextNode>().first;
var newText = textNode.toPlainText();

//nothing happens
expect(newText, text);
//nothing happens when there is no words to the left of the cursor
expect(textNode.toPlainText(), text);

selection = Selection.single(path: [0], startOffset: 14);
await editor.updateSelection(selection);
await editor.updateSelection(
Selection.single(path: [0], startOffset: 14),
);
//Welcome to App|flowy 😁

if (Platform.isWindows || Platform.isLinux) {
Expand All @@ -139,10 +136,9 @@ void main() async {
//fetching all the text that is still on the editor.
nodes = editor.editorState.service.selectionService.currentSelectedNodes;
textNode = nodes.whereType<TextNode>().first;
newText = textNode.toPlainText();

const expectedText = 'Welcome to flowy 😁';
expect(newText, expectedText);
expect(textNode.toPlainText(), expectedText);
});

testWidgets('Removes space and word after ctrl + backspace',
Expand All @@ -152,13 +148,45 @@ void main() async {

await editor.startTesting();

await editor.updateSelection(
Selection.single(path: [0], startOffset: 11),
);
//Welcome to |Appflowy 😁

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isControlPressed: true,
);
} else {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isAltPressed: true,
);
}

//fetching all the text that is still on the editor.
var nodes =
final nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
final textNode = nodes.whereType<TextNode>().first;

final selection = Selection.single(path: [0], startOffset: 11);
await editor.updateSelection(selection);
//Welcome to |Appflowy 😁
const expectedText = 'Welcome Appflowy 😁';
expect(textNode.toPlainText(), expectedText);
});

testWidgets('ctrl + backspace works properly with only single whitespace',
(tester) async {
//edge case that checks if pressing ctrl+backspace on null value
//after removing a whitespace, does not throw an exception.
const text = ' ';
final editor = tester.editor..insertTextNode(text);

await editor.startTesting();

await editor.updateSelection(
Selection.single(path: [0], startOffset: text.length),
);
// |

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
Expand All @@ -173,12 +201,79 @@ void main() async {
}

//fetching all the text that is still on the editor.
nodes = editor.editorState.service.selectionService.currentSelectedNodes;
final nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
final textNode = nodes.whereType<TextNode>().first;
final newText = textNode.toPlainText();

const expectedText = 'Welcome Appflowy 😁';
expect(newText, expectedText);
expect(textNode.toPlainText().isEmpty, true);
});

testWidgets('ctrl + alt + backspace works properly and deletes a sentence',
(tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor
..insertTextNode(text)
..insertTextNode(text)
..insertTextNode(text);

await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: text.length),
);
//Welcome to Appflowy 😁|

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isControlPressed: true,
isAltPressed: true,
);
} else {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isMetaPressed: true,
);
}

//fetching all the text that is still on the editor.
final nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
final textNode = nodes.whereType<TextNode>().first;

expect(textNode.toPlainText().isEmpty, true);
});

testWidgets('ctrl + alt + backspace works in the middle of a word',
(tester) async {
const text = 'Welcome to Appflowy 😁';
final editor = tester.editor..insertTextNode(text);

await editor.startTesting();
await editor.updateSelection(
Selection.single(path: [0], startOffset: 14),
);
//Welcome to App|flowy 😁

if (Platform.isWindows || Platform.isLinux) {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isControlPressed: true,
isAltPressed: true,
);
} else {
await editor.pressLogicKey(
key: LogicalKeyboardKey.backspace,
isMetaPressed: true,
);
}

//fetching all the text that is still on the editor.
final nodes =
editor.editorState.service.selectionService.currentSelectedNodes;
final textNode = nodes.whereType<TextNode>().first;

const expectedText = 'flowy 😁';
expect(textNode.toPlainText(), expectedText);
});
});
}

0 comments on commit 54c6ac1

Please sign in to comment.