-
-
Notifications
You must be signed in to change notification settings - Fork 500
/
value_listenable_test.dart
155 lines (130 loc) · 4.08 KB
/
value_listenable_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:provider/provider.dart';
import 'common.dart';
class ValueNotifierMock<T> extends Mock implements ValueNotifier<T> {
ValueNotifierMock(this.fallbackValue);
final T fallbackValue;
@override
T get value => super.noSuchMethod(
Invocation.getter(#value),
returnValue: fallbackValue,
returnValueForMissingStub: fallbackValue,
) as T;
@override
void addListener(VoidCallback? listener) {
super.noSuchMethod(
Invocation.method(#addListener, [listener]),
);
}
@override
void removeListener(VoidCallback? listener) {
super.noSuchMethod(
Invocation.method(#removeListener, [listener]),
);
}
}
void main() {
group('valueListenableProvider', () {
testWidgets('rebuilds when value change', (tester) async {
final listenable = ValueNotifier(0);
final child = Builder(
builder: (context) => Text(Provider.of<int>(context).toString(),
textDirection: TextDirection.ltr));
await tester.pumpWidget(
ValueListenableProvider.value(
value: listenable,
child: child,
),
);
expect(find.text('0'), findsOneWidget);
listenable.value++;
await tester.pump();
expect(find.text('1'), findsOneWidget);
});
testWidgets("don't rebuild dependents by default", (tester) async {
var buildCount = 0;
final listenable = ValueNotifier(0);
final child = Builder(builder: (context) {
buildCount++;
return Container();
});
await tester.pumpWidget(
ValueListenableProvider.value(
value: listenable,
child: child,
),
);
expect(buildCount, 1);
await tester.pumpWidget(
ValueListenableProvider.value(
value: listenable,
child: child,
),
);
expect(buildCount, 1);
});
testWidgets('pass keys', (tester) async {
final key = GlobalKey();
await tester.pumpWidget(
ValueListenableProvider.value(
key: key,
value: ValueNotifier(42),
child: Container(),
),
);
expect(key.currentWidget, isInstanceOf<ValueListenableProvider<int>>());
});
testWidgets("don't listen again if Value instance doesn't change",
(tester) async {
final valueNotifier = ValueNotifierMock<int>(0);
await tester.pumpWidget(
ValueListenableProvider.value(
value: valueNotifier,
child: TextOf<int>(),
),
);
await tester.pumpWidget(
ValueListenableProvider.value(
value: valueNotifier,
child: TextOf<int>(),
),
);
verify(valueNotifier.addListener(any)).called(1);
verify(valueNotifier.value);
verifyNoMoreInteractions(valueNotifier);
});
testWidgets('pass updateShouldNotify', (tester) async {
final shouldNotify = UpdateShouldNotifyMock<int>();
when(shouldNotify(0, 1)).thenReturn(true);
final notifier = ValueNotifier(0);
await tester.pumpWidget(
ValueListenableProvider.value(
value: notifier,
updateShouldNotify: shouldNotify,
child: TextOf<int>(),
),
);
verifyZeroInteractions(shouldNotify);
notifier.value++;
await tester.pump();
verify(shouldNotify(0, 1)).called(1);
verifyNoMoreInteractions(shouldNotify);
});
test('has correct debugFillProperties', () {
final builder = DiagnosticPropertiesBuilder();
final notifier = ValueNotifier(0);
ValueListenableProvider.value(value: notifier, child: const SizedBox())
.debugFillProperties(builder);
final description = builder.properties
.where(
(DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info),
)
.map((DiagnosticsNode node) => node.toString())
.toList();
expect(description, <String>['value: 0']);
});
});
}