-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings_test.dart
105 lines (90 loc) · 2.73 KB
/
settings_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import 'dart:io';
import 'package:krok_term/src/krok_term/common/settings.dart';
import 'package:krok_term/src/krok_term/core/krok_core.dart';
import 'package:test/test.dart';
File _testFile() => File('tmp/settings.json');
Future _deleteFile() async {
if (!await _testFile().exists()) return;
return await _testFile().delete();
}
void main() {
late PersistentSettings sut;
setUp(() => sut = PersistentSettings(path: 'tmp/settings.json'));
setUp(() async => await _deleteFile());
tearDown(() async => await _deleteFile());
test('starts empty if notifications file is empty', () async {
//when
final actual = await sut.str('null');
//then
expect(actual, isNull);
});
test('restores existing data', () async {
//given
await _testFile().writeAsString('{"null":"not_null"}');
//when
final actual = await sut.str('null');
//then
expect(actual, equals('not_null'));
});
test('updates file when changed', () async {
//given
await sut.set('null', 'not_null');
//when
final actual = await _testFile().readAsString();
//then
expect(actual, equals('{"null":"not_null"}'));
});
test('provides changed data', () async {
//given
await sut.set('null', 'not_null');
//when
final actual = await sut.str('null');
//then
expect(actual, equals('not_null'));
});
test('provides changed data across restarts', () async {
//given
await PersistentSettings(path: _testFile().path).set('null', 'not_null');
//when
final actual = await sut.str('null');
//then
expect(actual, equals('not_null'));
});
test('streams existing value right away', () async {
//given
await _testFile().writeAsString('{"null":"not_null"}');
//when
final actual = await sut.stream('null').first;
//then
expect(actual, equals('not_null'));
}, timeout: Timeout(100.millis));
test('streams initial null value', () async {
//when
final actual = await sut.stream('null').first;
//then
expect(actual, isNull);
}, timeout: Timeout(100.millis));
test('streams latest update', () async {
//given
await sut.set('null', 'null');
await sut.set('null', 'not_null');
await sut.set('null', 'something');
//when
final actual = await sut.stream('null').first;
//then
expect(actual, equals('something'));
}, timeout: Timeout(100.millis));
test('streams updates', () async {
//given
final actual = sut.stream('null').take(4).toList();
//when
await sut.set('null', 'null');
await sut.set('null', 'not_null');
await sut.set('null', 'something');
//then
expect(
await actual,
containsAllInOrder([null, 'null', 'not_null', 'something']),
);
}, timeout: Timeout(100.millis));
}