Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed Jan 6, 2024
1 parent da744b1 commit 44dc327
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/fleather/test/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EditorSandBox {
FleatherThemeData? fleatherTheme,
bool autofocus = false,
FakeSpellCheckService? spellCheckService,
FleatherEmbedBuilder embedBuilder = defaultFleatherEmbedBuilder,
}) {
focusNode ??= FocusNode();
document ??= ParchmentDocument.fromDelta(delta);
Expand All @@ -30,6 +31,7 @@ class EditorSandBox {
focusNode: focusNode,
autofocus: autofocus,
spellCheckService: spellCheckService,
embedBuilder: embedBuilder,
);

if (fleatherTheme != null) {
Expand Down Expand Up @@ -141,11 +143,14 @@ class _FleatherSandbox extends StatefulWidget {
required this.focusNode,
this.autofocus = false,
this.spellCheckService,
this.embedBuilder = defaultFleatherEmbedBuilder,
}) : super(key: key);

final FleatherController controller;
final FocusNode focusNode;
final bool autofocus;
final FakeSpellCheckService? spellCheckService;
final FleatherEmbedBuilder embedBuilder;

@override
_FleatherSandboxState createState() => _FleatherSandboxState();
Expand All @@ -158,6 +163,7 @@ class _FleatherSandboxState extends State<_FleatherSandbox> {
Widget build(BuildContext context) {
return Material(
child: FleatherField(
embedBuilder: widget.embedBuilder,
controller: widget.controller,
focusNode: widget.focusNode,
readOnly: !_enabled,
Expand Down
138 changes: 138 additions & 0 deletions packages/fleather/test/widgets/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,144 @@ void main() {
extentOffset: 50,
affinity: TextAffinity.upstream));
}, [TargetPlatform.linux]);

testWidgets(
'Arrow keys move cursor to next/previous line at correct position',
(tester) async {
const text =
'This is a relatively long paragraph with multiple lines and an inline embed: ';
final document = ParchmentDocument.fromJson([
{'insert': text},
{
'insert': {
'_type': 'icon',
'_inline': true,
'codePoint': '0xf0653',
'fontFamily': 'MaterialIcons',
'color': '0xFF2196F3'
}
},
{'insert': '\n'},
]);
final editor = EditorSandBox(
tester: tester,
document: document,
autofocus: true,
embedBuilder: (BuildContext context, EmbedNode node) {
if (node.value.type == 'icon') {
final data = node.value.data;
return Icon(
IconData(int.parse(data['codePoint']),
fontFamily: data['fontFamily']),
color: Color(int.parse(data['color'])),
size: 100,
);
}
throw UnimplementedError();
},
);
await editor.pump();
editor.controller.updateSelection(
const TextSelection.collapsed(offset: text.length));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
expect(editor.selection, const TextSelection.collapsed(offset: 27));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
expect(editor.selection,
const TextSelection.collapsed(offset: text.length));
});

testWidgets(
'Arrow keys move cursor to next/previous block at correct position',
(tester) async {
const text =
'This is a relatively long paragraph with multiple lines with inline embeds in next paragraph.';
final document = ParchmentDocument.fromJson([
{'insert': '$text\n'},
{
'insert': {
'_type': 'something',
'_inline': true,
}
},
{
'insert': {
'_type': 'something',
'_inline': true,
}
},
{'insert': '\n'},
]);
final editor = EditorSandBox(
tester: tester,
document: document,
autofocus: true,
embedBuilder: (BuildContext context, EmbedNode node) {
if (node.value.type == 'something') {
return const Padding(
padding: EdgeInsets.only(left: 4, right: 2, top: 2, bottom: 2),
child: SizedBox(
width: 300,
height: 300,
),
);
}
throw UnimplementedError();
},
);
await editor.pump();
editor.controller.updateSelection(
const TextSelection.collapsed(offset: text.length + 2));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
expect(editor.selection, const TextSelection.collapsed(offset: 69));
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
expect(editor.selection,
const TextSelection.collapsed(offset: text.length + 2));
});

testWidgets('Arrow down moves cursor to lower line at correct position',
(tester) async {
const text =
'This is a relatively long paragraph with multiple lines and an inline embed: ';
final document = ParchmentDocument.fromJson([
{'insert': text},
{
'insert': {
'_type': 'icon',
'_inline': true,
'codePoint': '0xf0653',
'fontFamily': 'MaterialIcons',
'color': '0xFF2196F3'
}
},
{'insert': '\n'},
]);
final editor = EditorSandBox(
tester: tester,
document: document,
autofocus: true,
embedBuilder: (BuildContext context, EmbedNode node) {
if (node.value.type == 'icon') {
final data = node.value.data;
return Icon(
IconData(int.parse(data['codePoint']),
fontFamily: data['fontFamily']),
color: Color(int.parse(data['color'])),
size: 100,
);
}
throw UnimplementedError();
},
);
await editor.pump();
editor.controller
.updateSelection(const TextSelection.collapsed(offset: 27));
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
expect(editor.selection,
const TextSelection.collapsed(offset: text.length));
});
});

group('Spell check', () {
Expand Down

0 comments on commit 44dc327

Please sign in to comment.