fix: replace matches on the same node (#418)

* test: failing test for expected behavior

* test: fix test by removing enter

* fix: replace all fixed

* chore: remove unused code

* test: replace selected called multiple times

* chore: remove unused variable
This commit is contained in:
Mayur Mahajan 2023-08-28 08:32:41 +05:30 • committed by GitHub
commit e38b07d13b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 56 deletions

View file

@ -197,6 +197,44 @@ void main() async {
await editor.dispose();
});
testWidgets('replace match on multiple matches in same path',
(tester) async {
const patternToBeFound = 'a';
const replacePattern = 'test';
const replaceSelectedBtn = Key('replaceSelectedButton');
const multiplier = 5;
final editor = tester.editor;
editor.addParagraph(initialText: patternToBeFound * multiplier);
await editor.startTesting();
await editor.updateSelection(Selection.single(path: [0], startOffset: 0));
await pressFindAndReplaceCommand(editor, openReplace: true);
await tester.pumpAndSettle();
expect(find.byType(FindMenuWidget), findsOneWidget);
//we put the pattern in the find dialog and press enter
await enterInputIntoFindDialog(tester, patternToBeFound);
//now we input some text into the replace text field and try replace all
await enterInputIntoReplaceDialog(
tester,
replacePattern,
);
for (int i = 0; i < multiplier; i++) {
await tester.tap(find.byKey(replaceSelectedBtn));
await tester.pumpAndSettle();
}
//all matches should be replaced
final node = editor.nodeAtPath([0]);
expect(node!.delta!.toPlainText(), replacePattern * multiplier);
await editor.dispose();
});
testWidgets('replace all on found matches', (tester) async {
const patternToBeFound = 'Welcome';
const replacePattern = 'Salute';
@ -239,5 +277,39 @@ void main() async {
}
await editor.dispose();
});
testWidgets('replace all on found matches in same path', (tester) async {
const patternToBeFound = 'x';
const replacePattern = 'Mayur';
const replaceAllBtn = Key('replaceAllButton');
final editor = tester.editor;
editor.addParagraph(initialText: patternToBeFound * 5);
await editor.startTesting();
await editor.updateSelection(Selection.single(path: [0], startOffset: 0));
await pressFindAndReplaceCommand(editor, openReplace: true);
await tester.pumpAndSettle();
expect(find.byType(FindMenuWidget), findsOneWidget);
//we put the pattern in the find dialog and press enter
await enterInputIntoFindDialog(tester, patternToBeFound);
//now we input some text into the replace text field and try replace all
await enterInputIntoReplaceDialog(
tester,
replacePattern,
);
await tester.tap(find.byKey(replaceAllBtn));
await tester.pumpAndSettle();
//all matches should be replaced
final node = editor.nodeAtPath([0]);
expect(node!.delta!.toPlainText(), replacePattern * 5);
await editor.dispose();
});
});
}

View file

@ -75,6 +75,16 @@ Future<void> enterInputIntoFindDialog(
await tester.pumpAndSettle();
}
Future<void> enterInputIntoReplaceDialog(
WidgetTester tester,
String pattern,
) async {
const textInputKey = Key('replaceTextField');
await tester.tap(find.byKey(textInputKey));
await tester.enterText(find.byKey(textInputKey), pattern);
await tester.pumpAndSettle();
}
Future<void> pressFindAndReplaceCommand(
TestableEditor editor, {
bool openReplace = false,