forked from rrousselGit/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider_test.dart
184 lines (166 loc) · 5.07 KB
/
provider_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart' hide TypeMatcher;
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:provider/src/provider.dart';
import 'package:test_api/test_api.dart' show TypeMatcher;
void main() {
group('Provider', () {
test('cloneWithChild works', () {
final provider = Provider.value(
value: 42,
child: Container(),
key: const ValueKey(42),
updateShouldNotify: (int _, int __) => true,
);
final newChild = Container();
final clone = provider.cloneWithChild(newChild);
expect(clone.child, newChild);
expect(clone.value, provider.value);
expect(clone.key, provider.key);
expect(provider.updateShouldNotify, clone.updateShouldNotify);
});
testWidgets('simple usage', (tester) async {
var buildCount = 0;
int value;
double second;
// We voluntarily reuse the builder instance so that later call to pumpWidget
// don't call builder again unless subscribed to an inheritedWidget
final builder = Builder(
builder: (context) {
buildCount++;
value = Provider.of(context);
second = Provider.of(context, listen: false);
return Container();
},
);
await tester.pumpWidget(
Provider<double>.value(
value: 24.0,
child: Provider<int>.value(
value: 42,
child: builder,
),
),
);
expect(value, equals(42));
expect(second, equals(24.0));
expect(buildCount, equals(1));
// nothing changed
await tester.pumpWidget(
Provider<double>.value(
value: 24.0,
child: Provider<int>.value(
value: 42,
child: builder,
),
),
);
// didn't rebuild
expect(buildCount, equals(1));
// changed a value we are subscribed to
await tester.pumpWidget(
Provider<double>.value(
value: 24.0,
child: Provider<int>.value(
value: 43,
child: builder,
),
),
);
expect(value, equals(43));
expect(second, equals(24.0));
// got rebuilt
expect(buildCount, equals(2));
// changed a value we are _not_ subscribed to
await tester.pumpWidget(
Provider<double>.value(
value: 20.0,
child: Provider<int>.value(
value: 43,
child: builder,
),
),
);
// didn't get rebuilt
expect(buildCount, equals(2));
});
testWidgets('throws an error if no provider found', (tester) async {
await tester.pumpWidget(Builder(builder: (context) {
Provider.of<String>(context);
return Container();
}));
expect(
tester.takeException(),
const TypeMatcher<ProviderNotFoundError>()
.having((err) => err.valueType, 'valueType', String)
.having((err) => err.widgetType, 'widgetType', Builder)
.having((err) => err.toString(), 'toString()', '''
Error: Could not find the correct Provider<String> above this Builder Widget
To fix, please:
* Ensure the Provider<String> is an ancestor to this Builder Widget
* Provide types to Provider<String>
* Provide types to Consumer<String>
* Provide types to Provider.of<String>()
* Always use package imports. Ex: `import 'package:my_app/my_code.dart';
* Ensure the correct `context` is being used.
If none of these solutions work, please file a bug at:
https://github.com/rrousselGit/provider/issues
'''),
);
});
testWidgets('update should notify', (tester) async {
int old;
int curr;
var callCount = 0;
final updateShouldNotify = (int o, int c) {
callCount++;
old = o;
curr = c;
return o != c;
};
var buildCount = 0;
int buildValue;
final builder = Builder(builder: (BuildContext context) {
buildValue = Provider.of(context);
buildCount++;
return Container();
});
await tester.pumpWidget(
Provider<int>.value(
value: 24,
updateShouldNotify: updateShouldNotify,
child: builder,
),
);
expect(callCount, equals(0));
expect(buildCount, equals(1));
expect(buildValue, equals(24));
// value changed
await tester.pumpWidget(
Provider<int>.value(
value: 25,
updateShouldNotify: updateShouldNotify,
child: builder,
),
);
expect(callCount, equals(1));
expect(old, equals(24));
expect(curr, equals(25));
expect(buildCount, equals(2));
expect(buildValue, equals(25));
// value didnt' change
await tester.pumpWidget(
Provider<int>.value(
value: 25,
updateShouldNotify: updateShouldNotify,
child: builder,
),
);
expect(callCount, equals(2));
expect(old, equals(25));
expect(curr, equals(25));
expect(buildCount, equals(2));
});
});
}