1
+ import 'dart:convert' ;
2
+
3
+ import 'package:http/http.dart' as http;
4
+ import 'package:checks/checks.dart' ;
5
+ import 'package:flutter_test/flutter_test.dart' ;
6
+ import 'package:zulip/api/model/events.dart' ;
7
+ import 'package:zulip/api/route/messages.dart' ;
8
+ import 'package:zulip/api/route/typing.dart' ;
9
+
10
+ import '../../stdlib_checks.dart' ;
11
+ import '../fake_api.dart' ;
12
+
13
+ void main () {
14
+ const streamId = 123 ;
15
+ const topic = 'topic' ;
16
+ const userIds = [101 , 102 , 103 ];
17
+
18
+ Future <void > checkSetTypingStatus (FakeApiConnection connection, {
19
+ required TypingOp op,
20
+ required MessageDestination destination,
21
+ required Map <String , String > expectedBodyFields,
22
+ }) async {
23
+ connection.prepare (json: {});
24
+ await setTypingStatus (connection,
25
+ op: op,
26
+ destination: destination,
27
+ );
28
+ check (connection.lastRequest).isA< http.Request > ()
29
+ ..method.equals ('POST' )
30
+ ..url.path.equals ('/api/v1/typing' )
31
+ ..bodyFields.deepEquals (expectedBodyFields);
32
+ }
33
+
34
+ final testCases = [
35
+ (TypingOp .start, 'start' ),
36
+ (TypingOp .stop, 'stop' ),
37
+ ];
38
+
39
+ for (final (op, expectedOp) in testCases) {
40
+ test ('send typing status $expectedOp to topic' , () {
41
+ return FakeApiConnection .with_ ((connection) async {
42
+ checkSetTypingStatus (connection,
43
+ op: op,
44
+ destination: const StreamDestination (streamId, topic),
45
+ expectedBodyFields: {
46
+ 'type' : 'channel' ,
47
+ 'op' : expectedOp,
48
+ 'stream_id' : streamId.toString (),
49
+ 'topic' : topic,
50
+ });
51
+ });
52
+ });
53
+
54
+ test ('send typing status $expectedOp to dm' , () {
55
+ return FakeApiConnection .with_ ((connection) async {
56
+ checkSetTypingStatus (connection,
57
+ op: op,
58
+ destination: const DmDestination (userIds: userIds),
59
+ expectedBodyFields: {
60
+ 'op' : expectedOp,
61
+ 'to' : jsonEncode (userIds),
62
+ });
63
+ });
64
+ });
65
+ }
66
+
67
+ test ('use stream on legacy server' , () {
68
+ return FakeApiConnection .with_ (zulipFeatureLevel: 247 , (connection) async {
69
+ checkSetTypingStatus (connection,
70
+ op: TypingOp .start,
71
+ destination: const StreamDestination (streamId, topic),
72
+ expectedBodyFields: {
73
+ 'type' : 'stream' ,
74
+ 'op' : 'start' ,
75
+ 'stream_id' : streamId.toString (),
76
+ 'topic' : topic,
77
+ },
78
+ );
79
+ });
80
+ });
81
+ }
0 commit comments