test: increase coverage for document.dart

This commit is contained in:
Mathias Mogensen 2023-08-07 11:21:35 +02:00 • committed by Lucas.Xu
commit b2f6c9c206

View file

@ -11,9 +11,16 @@ void main() async {
final node0 = Node(type: '0');
final node1 = Node(type: '1');
final node2 = Node(type: '2');
expect(document.insert([0], [node0, node1]), true);
expect(document.nodeAtPath([0])?.type, '0');
expect(document.nodeAtPath([1])?.type, '1');
// Insert with an existing node at path
expect(document.insert([0], [node2]), true);
expect(document.nodeAtPath([0])?.type, '2');
expect(document.nodeAtPath([1])?.type, '0');
expect(document.nodeAtPath([2])?.type, '1');
});
test('delete', () {
@ -32,14 +39,20 @@ void main() async {
});
test('update', () {
final firstRootAttr = {'b': 'c'};
final node = Node(type: 'example', attributes: {'a': 'a'});
final document = Document(root: Node(type: 'root'));
final document =
Document(root: Node(type: 'root', attributes: firstRootAttr));
document.insert([0], [node]);
final attributes = {
'a': 'b',
'b': 'c',
};
final rootAttributes = {'b': 'b'};
final attributes = {'a': 'b', 'b': 'c'};
// If path is empty, should update attributes of root node
expect(document.nodeAtPath([])?.attributes, firstRootAttr);
expect(document.update([], rootAttributes), true);
expect(document.nodeAtPath([])?.attributes, rootAttributes);
expect(document.update([0], attributes), true);
expect(document.nodeAtPath([0])?.attributes, attributes);
@ -140,4 +153,71 @@ void main() async {
);
});
});
test('first', () {
const firstLine = 'Welcome to AppFlowy!';
final document = Document.fromJson({
'document': {
'type': 'page',
'children': [
{
'type': 'paragraph',
'data': {
'delta': [
{'insert': firstLine}
],
}
}
],
}
});
final first = document.first;
expect(first!.delta!.toPlainText(), firstLine);
});
test('last', () {
const firstLine = 'Welcome to AppFlowy!';
const firstChild = 'Hello';
const secondChild = 'World';
final document = Document.fromJson({
'document': {
'type': 'page',
'children': [
{
'type': 'paragraph',
'data': {
'delta': [
{'insert': firstLine}
],
},
'children': [
{
'type': 'paragraph',
'data': {
'delta': [
{'insert': firstChild}
],
},
},
{
'type': 'paragraph',
'data': {
'delta': [
{'insert': secondChild}
],
},
}
],
}
],
}
});
final last = document.last!;
expect(last.delta!.toPlainText(), secondChild);
});
}