Skip to content

Commit 6a66699

Browse files
gnpricechrisbobbe
authored andcommitted
store test: Test UpdateMachine.poll in happy cases
1 parent 2d97b55 commit 6a66699

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/model/store_test.dart

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import 'package:checks/checks.dart';
44
import 'package:flutter/foundation.dart';
55
import 'package:http/http.dart' as http;
66
import 'package:test/scaffolding.dart';
7+
import 'package:zulip/api/model/events.dart';
8+
import 'package:zulip/api/model/model.dart';
9+
import 'package:zulip/api/route/events.dart';
710
import 'package:zulip/api/route/messages.dart';
811
import 'package:zulip/model/store.dart';
912
import 'package:zulip/notifications.dart';
@@ -131,6 +134,75 @@ void main() {
131134
});
132135
});
133136

137+
group('UpdateMachine.poll', () {
138+
late UpdateMachine updateMachine;
139+
late PerAccountStore store;
140+
late FakeApiConnection connection;
141+
142+
void prepareStore({int? lastEventId}) {
143+
updateMachine = eg.updateMachine(initialSnapshot: eg.initialSnapshot(
144+
lastEventId: lastEventId,
145+
));
146+
store = updateMachine.store;
147+
connection = store.connection as FakeApiConnection;
148+
}
149+
150+
void checkLastRequest({required int lastEventId}) {
151+
check(connection.lastRequest).isA<http.Request>()
152+
..method.equals('GET')
153+
..url.path.equals('/api/v1/events')
154+
..url.queryParameters.deepEquals({
155+
'queue_id': updateMachine.queueId,
156+
'last_event_id': lastEventId.toString(),
157+
});
158+
}
159+
160+
test('loops on success', () async {
161+
prepareStore(lastEventId: 1);
162+
check(updateMachine.lastEventId).equals(1);
163+
164+
updateMachine.debugPauseLoop();
165+
updateMachine.poll();
166+
167+
// Loop makes first request, and processes result.
168+
connection.prepare(json: GetEventsResult(events: [
169+
HeartbeatEvent(id: 2),
170+
], queueId: null).toJson());
171+
updateMachine.debugAdvanceLoop();
172+
await null;
173+
checkLastRequest(lastEventId: 1);
174+
await Future.delayed(Duration.zero);
175+
check(updateMachine.lastEventId).equals(2);
176+
177+
// Loop makes second request, and processes result.
178+
connection.prepare(json: GetEventsResult(events: [
179+
HeartbeatEvent(id: 3),
180+
], queueId: null).toJson());
181+
updateMachine.debugAdvanceLoop();
182+
await null;
183+
checkLastRequest(lastEventId: 2);
184+
await Future.delayed(Duration.zero);
185+
check(updateMachine.lastEventId).equals(3);
186+
});
187+
188+
test('handles events', () async {
189+
prepareStore();
190+
updateMachine.debugPauseLoop();
191+
updateMachine.poll();
192+
193+
// Pick some arbitrary event and check it gets processed on the store.
194+
check(store.userSettings!.twentyFourHourTime).isFalse();
195+
connection.prepare(json: GetEventsResult(events: [
196+
UserSettingsUpdateEvent(id: 2,
197+
property: UserSettingName.twentyFourHourTime, value: true),
198+
], queueId: null).toJson());
199+
updateMachine.debugAdvanceLoop();
200+
await null;
201+
await Future.delayed(Duration.zero);
202+
check(store.userSettings!.twentyFourHourTime).isTrue();
203+
});
204+
});
205+
134206
group('UpdateMachine.registerNotificationToken', () {
135207
late UpdateMachine updateMachine;
136208
late FakeApiConnection connection;

0 commit comments

Comments
 (0)