Skip to content

Commit 93d683c

Browse files
committed
store test: Expose TestGlobalStore.useCachedApiConnections; default false
1 parent 0491f8d commit 93d683c

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

test/model/store_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ void main() {
247247
test('smoke', () => awaitFakeAsync((async) async {
248248
await prepareStore();
249249
final users = [eg.selfUser, eg.otherUser];
250+
251+
globalStore.useCachedApiConnections = true;
250252
connection.prepare(json: eg.initialSnapshot(realmUsers: users).toJson());
251253
final updateMachine = await UpdateMachine.load(
252254
globalStore, eg.selfAccount.id);
@@ -274,6 +276,7 @@ void main() {
274276
..zulipMergeBase.equals('6.0')
275277
..zulipFeatureLevel.equals(123);
276278

279+
globalStore.useCachedApiConnections = true;
277280
connection.prepare(json: eg.initialSnapshot(
278281
zulipVersion: '8.0+g9876',
279282
zulipMergeBase: '8.0',
@@ -292,6 +295,7 @@ void main() {
292295
await prepareStore();
293296

294297
// Try to load, inducing an error in the request.
298+
globalStore.useCachedApiConnections = true;
295299
connection.prepare(exception: Exception('failed'));
296300
final future = UpdateMachine.load(globalStore, eg.selfAccount.id);
297301
bool complete = false;

test/model/test_store.dart

+26-13
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,40 @@ class TestGlobalStore extends GlobalStore {
2929
FakeApiConnection
3030
> _apiConnections = {};
3131

32-
/// Get or construct a [FakeApiConnection] with the given arguments.
33-
///
34-
/// This breaches the base method's contract slightly, in that on repeated
35-
/// calls with the same arguments it returns the same connection, rather than
36-
/// a fresh one each time. That breach is very convenient for tests,
37-
/// enabling a test to get access to the same [FakeApiConnection] that the
38-
/// code under test will get, so as to use [FakeApiConnection.prepare]
39-
/// and [FakeApiConnection.lastRequest].
32+
/// Whether [apiConnection] should return a cached connection.
4033
///
41-
/// However, if the connection returned by a previous call has been closed
42-
/// with [ApiConnection.close], then that connection will be ignored in favor
34+
/// If true, [apiConnection] will return a cached [FakeApiConnection]
35+
/// from a previous call, if it is still open ([FakeApiConnection.isOpen]).
36+
/// If there is a cached connection but it has been closed
37+
/// with [ApiConnection.close], that connection will be ignored in favor
4338
/// of returning (and saving for next time) a fresh connection after all.
39+
///
40+
/// If false (the default), returns a fresh connection each time.
41+
///
42+
/// Setting this to true is useful if a test needs to access the same
43+
/// [FakeApiConnection] that the code under test will get, so as to use
44+
/// [FakeApiConnection.prepare] or [FakeApiConnection.lastRequest].
45+
/// The behavior with `true` breaches the base method's contract slightly --
46+
/// the base method would return a fresh connection each time --
47+
/// but that breach is sometimes convenient for tests.
48+
bool useCachedApiConnections = false;
49+
50+
/// Get or construct a [FakeApiConnection] with the given arguments.
51+
///
52+
/// To access the same [FakeApiConnection] that the code under test will get,
53+
/// so as to use [FakeApiConnection.prepare] or [FakeApiConnection.lastRequest],
54+
/// see [useCachedApiConnections].
4455
@override
4556
FakeApiConnection apiConnection({
4657
required Uri realmUrl, required int? zulipFeatureLevel,
4758
String? email, String? apiKey}) {
4859
final key = (realmUrl: realmUrl, zulipFeatureLevel: zulipFeatureLevel,
4960
email: email, apiKey: apiKey);
50-
final connection = _apiConnections[key];
51-
if (connection != null && connection.isOpen) {
52-
return connection;
61+
if (useCachedApiConnections) {
62+
final connection = _apiConnections[key];
63+
if (connection != null && connection.isOpen) {
64+
return connection;
65+
}
5366
}
5467
return (_apiConnections[key] = FakeApiConnection(
5568
realmUrl: realmUrl, zulipFeatureLevel: zulipFeatureLevel,

test/widgets/login_test.dart

+3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void main() {
124124

125125
await tester.enterText(findUsernameInput, eg.selfAccount.email);
126126
await tester.enterText(findPasswordInput, 'p455w0rd');
127+
testBinding.globalStore.useCachedApiConnections = true;
127128
connection.prepare(json: FetchApiKeyResult(
128129
apiKey: eg.selfAccount.apiKey,
129130
email: eg.selfAccount.email,
@@ -144,6 +145,7 @@ void main() {
144145

145146
await tester.enterText(findUsernameInput, ' ${eg.selfAccount.email} ');
146147
await tester.enterText(findPasswordInput, 'p455w0rd');
148+
testBinding.globalStore.useCachedApiConnections = true;
147149
connection.prepare(json: FetchApiKeyResult(
148150
apiKey: eg.selfAccount.apiKey,
149151
email: eg.selfAccount.email,
@@ -165,6 +167,7 @@ void main() {
165167

166168
await tester.enterText(findUsernameInput, eg.selfAccount.email);
167169
await tester.enterText(findPasswordInput, 'p455w0rd');
170+
testBinding.globalStore.useCachedApiConnections = true;
168171
connection.prepare(json: FetchApiKeyResult(
169172
apiKey: eg.selfAccount.apiKey,
170173
email: eg.selfAccount.email,

0 commit comments

Comments
 (0)