-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifications_repo_test.dart
83 lines (73 loc) · 2.34 KB
/
notifications_repo_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:io';
import 'package:krok_term/src/krok_term/core/krok_core.dart';
import 'package:krok_term/src/krok_term/repository/notifications_repo.dart';
import 'package:test/test.dart';
File _testFile() => File('tmp/notifications');
Future _deleteFile() async {
final f = _testFile();
if (await f.exists()) await f.delete();
}
// only to have some async test...
void main() {
final Storage storage = Storage(path: 'tmp');
setUp(() async => await _deleteFile());
tearDown(() async => await _deleteFile());
test('starts empty if notifications file is empty', () async {
//when
final sut = NotificationsRepo(storage);
final actual = await sut.subscribe().first;
//then
await sut.close();
expect(actual, isEmpty);
});
test('appends json lines', () async {
//given
final sut = NotificationsRepo(storage);
//when
sut.add(NotificationData(1, '1', 'desc', ('msg', 'null')));
sut.add(NotificationData(2, '2', 'desc', ('msg', 'null')));
//then
await sut.close();
final actual = await _testFile().readAsLines();
expect(
actual,
containsAllInOrder([
'[1,"1","desc","msg","null"]',
'[2,"2","desc","msg","null"]',
]));
});
test('restores notifications upon creation', () async {
//given
await storage.append('notifications', '[1,"1","desc","msg","null"]\n');
await storage.append('notifications', '[2,"2","desc","msg","null"]\n');
//when
final sut = NotificationsRepo(storage);
final actual = await sut.subscribe().first;
//then
await sut.close();
expect(
actual,
containsAllInOrder([
NotificationData(1, "1", "desc", ("msg", "null")),
NotificationData(2, "2", "desc", ("msg", "null")),
]));
});
test('restores added notifications', () async {
//given
final pre = NotificationsRepo(storage);
pre.add(NotificationData(1, '1', 'desc', ('msg', 'null')));
pre.add(NotificationData(2, '2', 'desc', ('msg', 'null')));
await pre.close();
//when
final sut = NotificationsRepo(storage);
final actual = await sut.subscribe().first;
//then
await sut.close();
expect(
actual,
containsAllInOrder([
NotificationData(1, "1", "desc", ("msg", "null")),
NotificationData(2, "2", "desc", ("msg", "null")),
]));
});
}