Skip to content

Commit 4e8f503

Browse files
committed
Test EmbedRegistry
1 parent 0348cb5 commit 4e8f503

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import 'package:fleather/fleather.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('$EmbedRegistry', () {
7+
test('create fallback', () {
8+
final registry = EmbedRegistry.fallback();
9+
final hr = registry.blockEmbed(EmbeddableObject('hr', inline: false));
10+
expect(hr, isA<HorizontalRule>());
11+
});
12+
13+
test('create with configurations', () {
14+
final registry = EmbedRegistry.withConfigurations([FakeBlockEmbed()]);
15+
final fakeBlock =
16+
registry.blockEmbed(EmbeddableObject('fake_block', inline: false));
17+
expect(fakeBlock, isA<FakeBlockEmbed>());
18+
});
19+
20+
test('create fallback with configurations', () {
21+
final registry =
22+
EmbedRegistry.fallbackWithConfigurations([FakeBlockEmbed()]);
23+
final fakeBlock =
24+
registry.blockEmbed(EmbeddableObject('fake_block', inline: false));
25+
expect(fakeBlock, isA<FakeBlockEmbed>());
26+
final hr = registry.blockEmbed(EmbeddableObject('hr', inline: false));
27+
expect(hr, isA<HorizontalRule>());
28+
});
29+
30+
test('cannot register twice the same key', () {
31+
try {
32+
final registry = EmbedRegistry.withConfigurations([
33+
FakeBlockEmbed(), FakeBlockEmbed()
34+
]);
35+
} on ArgumentError catch (_) {
36+
return;
37+
}
38+
fail('Should throw an argument error');
39+
});
40+
41+
test('spanEmbed - find nothing', () {
42+
final registry = EmbedRegistry();
43+
try {
44+
registry.spanEmbed(EmbeddableObject('fake_span', inline: true));
45+
} on StateError catch (_) {
46+
return;
47+
}
48+
fail('Should throw an assertion error');
49+
});
50+
51+
test('spanEmbed - finds a block embed', () {
52+
final registry =
53+
EmbedRegistry.fallbackWithConfigurations([FakeBlockEmbed()]);
54+
try {
55+
registry
56+
.spanEmbed(EmbeddableObject(FakeBlockEmbed().key, inline: true));
57+
} on AssertionError catch (_) {
58+
return;
59+
}
60+
fail('Should throw an assertion error');
61+
});
62+
63+
test('blockEmbed - find nothing', () {
64+
final registry = EmbedRegistry();
65+
try {
66+
registry.blockEmbed(EmbeddableObject('fake_block', inline: false));
67+
} on StateError catch (_) {
68+
return;
69+
}
70+
fail('Should throw an assertion error');
71+
});
72+
73+
test('blockEmbed - finds a span embed', () {
74+
final registry = EmbedRegistry.withConfigurations([FakeSpanEmbed()]);
75+
try {
76+
registry
77+
.blockEmbed(EmbeddableObject(FakeSpanEmbed().key, inline: false));
78+
} on AssertionError catch (_) {
79+
return;
80+
}
81+
fail('Should throw an assertion error');
82+
});
83+
});
84+
}
85+
86+
class FakeBlockEmbed extends BlockEmbedConfiguration {
87+
FakeBlockEmbed() : super(key: 'fake_block');
88+
89+
@override
90+
Widget build(BuildContext context, Map<String, dynamic> data) {
91+
return Text('Span');
92+
}
93+
}
94+
95+
class FakeSpanEmbed extends SpanEmbedConfiguration {
96+
FakeSpanEmbed() : super(key: 'fake_span');
97+
98+
@override
99+
Widget build(BuildContext context, Map<String, dynamic> data) {
100+
return Text('Block');
101+
}
102+
}

0 commit comments

Comments
 (0)