@amityco/ts-sdk 0.0.1-beta.27 → 0.0.1-beta.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/.eslintrc.json +1 -0
  2. package/dist/@types/core/payload.d.ts +15 -13
  3. package/dist/@types/core/payload.d.ts.map +1 -1
  4. package/dist/@types/domains/channel.d.ts +17 -1
  5. package/dist/@types/domains/channel.d.ts.map +1 -1
  6. package/dist/@types/domains/client.d.ts +13 -0
  7. package/dist/@types/domains/client.d.ts.map +1 -1
  8. package/dist/@types/domains/community.d.ts +24 -2
  9. package/dist/@types/domains/community.d.ts.map +1 -1
  10. package/dist/@types/domains/content.d.ts +27 -3
  11. package/dist/@types/domains/content.d.ts.map +1 -1
  12. package/dist/@types/domains/file.d.ts +36 -3
  13. package/dist/@types/domains/file.d.ts.map +1 -1
  14. package/dist/@types/domains/follow.d.ts +11 -0
  15. package/dist/@types/domains/follow.d.ts.map +1 -1
  16. package/dist/@types/domains/group.d.ts +1 -0
  17. package/dist/@types/domains/group.d.ts.map +1 -1
  18. package/dist/@types/domains/message.d.ts +13 -0
  19. package/dist/@types/domains/message.d.ts.map +1 -1
  20. package/dist/@types/global.d.ts +1 -0
  21. package/dist/@types/global.d.ts.map +1 -1
  22. package/dist/@types/index.d.ts +3 -3
  23. package/dist/@types/index.d.ts.map +1 -1
  24. package/dist/channel/api/addChannelMembers.d.ts.map +1 -1
  25. package/dist/channel/api/addChannelMembersRole.d.ts.map +1 -1
  26. package/dist/channel/api/banChannelMembers.d.ts.map +1 -1
  27. package/dist/channel/api/createChannel.d.ts.map +1 -1
  28. package/dist/channel/api/getChannel.d.ts.map +1 -1
  29. package/dist/channel/api/getChannels.d.ts.map +1 -1
  30. package/dist/channel/api/joinChannel.d.ts.map +1 -1
  31. package/dist/channel/api/leaveChannel.d.ts.map +1 -1
  32. package/dist/channel/api/queryChannelMembers.d.ts.map +1 -1
  33. package/dist/channel/api/queryChannels.d.ts +1 -10
  34. package/dist/channel/api/queryChannels.d.ts.map +1 -1
  35. package/dist/channel/api/removeChannelMembers.d.ts.map +1 -1
  36. package/dist/channel/api/removeChannelMembersRole.d.ts.map +1 -1
  37. package/dist/channel/api/unbanChannelMembers.d.ts.map +1 -1
  38. package/dist/channel/api/updateChannel.d.ts.map +1 -1
  39. package/dist/channel/events/index.d.ts +1 -0
  40. package/dist/channel/events/index.d.ts.map +1 -1
  41. package/dist/channel/events/onChannelCreated.d.ts.map +1 -1
  42. package/dist/channel/events/onChannelDeleted.d.ts.map +1 -1
  43. package/dist/channel/events/onChannelJoined.d.ts.map +1 -1
  44. package/dist/channel/events/onChannelLeft.d.ts.map +1 -1
  45. package/dist/channel/events/onChannelMemberAdded.d.ts.map +1 -1
  46. package/dist/channel/events/onChannelMemberRemoved.d.ts.map +1 -1
  47. package/dist/channel/events/onChannelUpdated.d.ts.map +1 -1
  48. package/dist/channel/observers/index.d.ts +2 -0
  49. package/dist/channel/observers/index.d.ts.map +1 -1
  50. package/dist/channel/observers/liveChannels.d.ts +20 -0
  51. package/dist/channel/observers/liveChannels.d.ts.map +1 -0
  52. package/dist/channel/observers/observeChannel.d.ts +1 -1
  53. package/dist/channel/observers/observeChannel.d.ts.map +1 -1
  54. package/dist/channel/observers/observeChannels.d.ts +17 -0
  55. package/dist/channel/observers/observeChannels.d.ts.map +1 -0
  56. package/dist/channel/observers/tests/liveChannels.test.d.ts +2 -0
  57. package/dist/channel/observers/tests/liveChannels.test.d.ts.map +1 -0
  58. package/dist/channel/tests/utils/prepareChannelPayload.test.d.ts +2 -0
  59. package/dist/channel/tests/utils/prepareChannelPayload.test.d.ts.map +1 -0
  60. package/dist/channel/utils/index.d.ts +2 -0
  61. package/dist/channel/utils/index.d.ts.map +1 -0
  62. package/dist/channel/utils/prepareChannelPayload.d.ts +3 -0
  63. package/dist/channel/utils/prepareChannelPayload.d.ts.map +1 -0
  64. package/dist/client/api/connectClient.d.ts +1 -8
  65. package/dist/client/api/connectClient.d.ts.map +1 -1
  66. package/dist/client/api/createClient.d.ts +8 -1
  67. package/dist/client/api/createClient.d.ts.map +1 -1
  68. package/dist/client/api/getFeedSettings.d.ts +16 -0
  69. package/dist/client/api/getFeedSettings.d.ts.map +1 -0
  70. package/dist/client/api/index.d.ts +1 -0
  71. package/dist/client/api/index.d.ts.map +1 -1
  72. package/dist/community/api/createCommunity.d.ts +1 -1
  73. package/dist/community/api/createCommunity.d.ts.map +1 -1
  74. package/dist/community/api/getCommunities.d.ts.map +1 -1
  75. package/dist/community/api/getCommunity.d.ts.map +1 -1
  76. package/dist/community/api/getRecommendedCommunities.d.ts.map +1 -1
  77. package/dist/community/api/getTopTrendingCommunities.d.ts.map +1 -1
  78. package/dist/community/api/queryCommunities.d.ts.map +1 -1
  79. package/dist/community/api/updateCommunity.d.ts +1 -1
  80. package/dist/community/api/updateCommunity.d.ts.map +1 -1
  81. package/dist/community/events/utils.d.ts.map +1 -1
  82. package/dist/community/utils/index.d.ts +2 -0
  83. package/dist/community/utils/index.d.ts.map +1 -0
  84. package/dist/community/utils/payload.d.ts +11 -0
  85. package/dist/community/utils/payload.d.ts.map +1 -0
  86. package/dist/core/events.d.ts +3 -3
  87. package/dist/core/events.d.ts.map +1 -1
  88. package/dist/core/query/query.d.ts +1 -1
  89. package/dist/core/tests/query/query/runQuery.test.d.ts +2 -0
  90. package/dist/core/tests/query/query/runQuery.test.d.ts.map +1 -0
  91. package/dist/file/api/createVideo.d.ts +2 -1
  92. package/dist/file/api/createVideo.d.ts.map +1 -1
  93. package/dist/follow/api/queryFollowers.d.ts +1 -5
  94. package/dist/follow/api/queryFollowers.d.ts.map +1 -1
  95. package/dist/follow/api/queryFollowings.d.ts +1 -5
  96. package/dist/follow/api/queryFollowings.d.ts.map +1 -1
  97. package/dist/follow/observers/index.d.ts +2 -0
  98. package/dist/follow/observers/index.d.ts.map +1 -1
  99. package/dist/follow/observers/liveFollowers.d.ts +20 -0
  100. package/dist/follow/observers/liveFollowers.d.ts.map +1 -0
  101. package/dist/follow/observers/liveFollowings.d.ts +20 -0
  102. package/dist/follow/observers/liveFollowings.d.ts.map +1 -0
  103. package/dist/follow/tests/observers/liveFollowers.test.d.ts +2 -0
  104. package/dist/follow/tests/observers/liveFollowers.test.d.ts.map +1 -0
  105. package/dist/follow/tests/observers/liveFollowings.test.d.ts +2 -0
  106. package/dist/follow/tests/observers/liveFollowings.test.d.ts.map +1 -0
  107. package/dist/index.cjs.js +751 -128
  108. package/dist/index.d.ts +1 -1
  109. package/dist/index.d.ts.map +1 -1
  110. package/dist/index.esm.js +737 -129
  111. package/dist/index.umd.js +4 -4
  112. package/dist/message/api/queryMessages.d.ts +1 -10
  113. package/dist/message/api/queryMessages.d.ts.map +1 -1
  114. package/dist/message/observers/index.d.ts +1 -0
  115. package/dist/message/observers/index.d.ts.map +1 -1
  116. package/dist/message/observers/liveMessages.d.ts +20 -0
  117. package/dist/message/observers/liveMessages.d.ts.map +1 -0
  118. package/dist/message/observers/observeMessages.d.ts +0 -1
  119. package/dist/message/observers/observeMessages.d.ts.map +1 -1
  120. package/dist/message/observers/tests/liveMessages.test.d.ts +2 -0
  121. package/dist/message/observers/tests/liveMessages.test.d.ts.map +1 -0
  122. package/dist/post/observers/livePosts.d.ts.map +1 -1
  123. package/dist/utils/constants.d.ts +1 -0
  124. package/dist/utils/constants.d.ts.map +1 -1
  125. package/dist/utils/tests/client.d.ts +5 -2
  126. package/dist/utils/tests/client.d.ts.map +1 -1
  127. package/dist/utils/tests/dummy/channel.d.ts +33 -0
  128. package/dist/utils/tests/dummy/channel.d.ts.map +1 -0
  129. package/dist/utils/tests/dummy/follow.d.ts +12 -0
  130. package/dist/utils/tests/dummy/follow.d.ts.map +1 -0
  131. package/dist/utils/tests/dummy/index.d.ts +11 -0
  132. package/dist/utils/tests/dummy/index.d.ts.map +1 -0
  133. package/dist/utils/tests/dummy/message.d.ts +8 -0
  134. package/dist/utils/tests/dummy/message.d.ts.map +1 -0
  135. package/dist/utils/tests/{dummy.d.ts → dummy/post.d.ts} +3 -2
  136. package/dist/utils/tests/dummy/post.d.ts.map +1 -0
  137. package/dist/utils/tests/dummy/user.d.ts +5 -0
  138. package/dist/utils/tests/dummy/user.d.ts.map +1 -0
  139. package/dist/utils/tests/index.d.ts +3 -2
  140. package/dist/utils/tests/index.d.ts.map +1 -1
  141. package/dist/utils/tests/utils.d.ts +2 -0
  142. package/dist/utils/tests/utils.d.ts.map +1 -0
  143. package/package.json +1 -1
  144. package/src/@types/core/payload.ts +19 -13
  145. package/src/@types/domains/channel.ts +34 -1
  146. package/src/@types/domains/client.ts +17 -0
  147. package/src/@types/domains/community.ts +27 -2
  148. package/src/@types/domains/content.ts +35 -3
  149. package/src/@types/domains/file.ts +42 -3
  150. package/src/@types/domains/follow.ts +28 -0
  151. package/src/@types/domains/group.ts +1 -0
  152. package/src/@types/domains/message.ts +20 -0
  153. package/src/@types/global.ts +2 -0
  154. package/src/@types/index.ts +4 -3
  155. package/src/channel/api/addChannelMembers.ts +6 -5
  156. package/src/channel/api/addChannelMembersRole.ts +5 -1
  157. package/src/channel/api/banChannelMembers.ts +7 -3
  158. package/src/channel/api/createChannel.ts +6 -2
  159. package/src/channel/api/getChannel.ts +5 -1
  160. package/src/channel/api/getChannels.ts +5 -1
  161. package/src/channel/api/joinChannel.ts +5 -1
  162. package/src/channel/api/leaveChannel.ts +5 -1
  163. package/src/channel/api/queryChannelMembers.ts +16 -12
  164. package/src/channel/api/queryChannels.ts +12 -14
  165. package/src/channel/api/removeChannelMembers.ts +6 -5
  166. package/src/channel/api/removeChannelMembersRole.ts +5 -1
  167. package/src/channel/api/unbanChannelMembers.ts +7 -3
  168. package/src/channel/api/updateChannel.ts +5 -1
  169. package/src/channel/events/index.ts +1 -0
  170. package/src/channel/events/onChannelCreated.ts +6 -2
  171. package/src/channel/events/onChannelDeleted.ts +6 -2
  172. package/src/channel/events/onChannelJoined.ts +6 -2
  173. package/src/channel/events/onChannelLeft.ts +6 -2
  174. package/src/channel/events/onChannelMemberAdded.ts +6 -2
  175. package/src/channel/events/onChannelMemberRemoved.ts +6 -2
  176. package/src/channel/events/onChannelUpdated.ts +6 -2
  177. package/src/channel/observers/index.ts +2 -0
  178. package/src/channel/observers/liveChannels.ts +178 -0
  179. package/src/channel/observers/observeChannel.ts +1 -11
  180. package/src/channel/observers/observeChannels.ts +59 -0
  181. package/src/channel/observers/tests/liveChannels.test.ts +173 -0
  182. package/src/channel/tests/utils/prepareChannelPayload.test.ts +120 -0
  183. package/src/channel/utils/index.ts +1 -0
  184. package/src/channel/utils/prepareChannelPayload.ts +31 -0
  185. package/src/client/api/connectClient.ts +4 -9
  186. package/src/client/api/createClient.ts +19 -4
  187. package/src/client/api/getFeedSettings.ts +29 -0
  188. package/src/client/api/index.ts +2 -0
  189. package/src/community/api/createCommunity.ts +9 -3
  190. package/src/community/api/getCommunities.ts +10 -3
  191. package/src/community/api/getCommunity.ts +5 -1
  192. package/src/community/api/getRecommendedCommunities.ts +5 -2
  193. package/src/community/api/getTopTrendingCommunities.ts +4 -1
  194. package/src/community/api/queryCommunities.ts +5 -2
  195. package/src/community/api/updateCommunity.ts +7 -3
  196. package/src/community/events/utils.ts +13 -7
  197. package/src/community/utils/index.ts +1 -0
  198. package/src/community/utils/payload.ts +44 -0
  199. package/src/core/events.ts +2 -1
  200. package/src/core/model/identifyModel.ts +1 -1
  201. package/src/core/query/query.ts +3 -3
  202. package/src/core/tests/query/query/runQuery.test.ts +182 -0
  203. package/src/file/api/createVideo.ts +6 -0
  204. package/src/follow/api/queryFollowers.ts +3 -5
  205. package/src/follow/api/queryFollowings.ts +3 -5
  206. package/src/follow/api/utils.ts +7 -3
  207. package/src/follow/observers/index.ts +3 -0
  208. package/src/follow/observers/liveFollowers.ts +149 -0
  209. package/src/follow/observers/liveFollowings.ts +149 -0
  210. package/src/follow/tests/observers/liveFollowers.test.ts +240 -0
  211. package/src/follow/tests/observers/liveFollowings.test.ts +239 -0
  212. package/src/index.ts +1 -1
  213. package/src/message/api/queryMessages.ts +3 -10
  214. package/src/message/observers/index.ts +1 -0
  215. package/src/message/observers/liveMessages.ts +141 -0
  216. package/src/message/observers/observeMessages.ts +0 -1
  217. package/src/message/observers/tests/liveMessages.test.ts +313 -0
  218. package/src/post/observers/livePosts.ts +2 -1
  219. package/src/post/tests/api/getPost.test.ts +3 -3
  220. package/src/post/tests/api/queryPosts.test.ts +4 -4
  221. package/src/utils/constants.ts +1 -0
  222. package/src/utils/tests/client.ts +31 -3
  223. package/src/utils/tests/dummy/channel.ts +96 -0
  224. package/src/utils/tests/dummy/follow.ts +60 -0
  225. package/src/utils/tests/dummy/index.ts +8 -0
  226. package/src/utils/tests/dummy/message.ts +21 -0
  227. package/src/utils/tests/dummy/post.ts +31 -0
  228. package/src/utils/tests/dummy/user.ts +26 -0
  229. package/src/utils/tests/index.ts +3 -2
  230. package/src/utils/tests/utils.ts +3 -0
  231. package/dist/utils/tests/dummy.d.ts.map +0 -1
  232. package/src/utils/tests/dummy.ts +0 -7
@@ -0,0 +1,313 @@
1
+ import { ASCApiError } from '~/core/errors';
2
+ import { disableCache, enableCache } from '~/cache/api';
3
+ import { ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
+
5
+ import { liveMessages } from '../liveMessages';
6
+ import { queryMessages } from '~/message/api';
7
+
8
+ import {
9
+ client,
10
+ connectClient,
11
+ disconnectClient,
12
+ messages,
13
+ message as defaultMessage,
14
+ } from '~/utils/tests';
15
+
16
+ describe('liveMessages', () => {
17
+ beforeAll(async () => {
18
+ await connectClient();
19
+ });
20
+
21
+ afterAll(async () => {
22
+ await disconnectClient();
23
+ });
24
+
25
+ beforeEach(async () => {
26
+ enableCache();
27
+ });
28
+
29
+ afterEach(disableCache);
30
+
31
+ const { channelId } = messages;
32
+ const returnValue = [{ messageId: messages.page1[0] }];
33
+ const message = { ...defaultMessage, messageId: returnValue[0].messageId, channelId };
34
+
35
+ test('it should show message if cache not enabled', () => {
36
+ /*
37
+ * NOTE: at the time of writing this test cache is diabled by default, but
38
+ * there is a proposal to enable cache by default. So I'm disabling cache
39
+ * here to ensure cache is disabled
40
+ */
41
+ disableCache();
42
+
43
+ const callback = jest.fn();
44
+ jest.spyOn(global.console, 'log');
45
+ client.http.get = jest.fn();
46
+
47
+ // call liveMessages check if mocked console get's called with the correct
48
+ // message
49
+ client.use();
50
+ liveMessages({ channelId }, callback);
51
+
52
+ expect(console.log).toBeCalledWith(ENABLE_CACHE_MESSAGE);
53
+ });
54
+
55
+ test('it should return message collection', async () => {
56
+ // mock response of queryMessages and validate
57
+ const callback = jest.fn();
58
+ client.http.get = jest.fn().mockResolvedValue({
59
+ data: {
60
+ messages: returnValue,
61
+ paging: {},
62
+ },
63
+ });
64
+
65
+ liveMessages({ channelId }, callback);
66
+
67
+ expect(callback).toHaveBeenCalledTimes(1);
68
+
69
+ // check if cache data returned (should be empty)
70
+ expect(callback).toHaveBeenCalledWith(
71
+ expect.objectContaining({
72
+ data: [],
73
+ error: undefined,
74
+ loading: true,
75
+ }),
76
+ );
77
+
78
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
79
+
80
+ expect(callback).toHaveBeenCalledTimes(2);
81
+
82
+ expect(callback).toHaveBeenCalledWith(
83
+ expect.objectContaining({
84
+ data: returnValue,
85
+ error: undefined,
86
+ loading: false,
87
+ }),
88
+ );
89
+ });
90
+
91
+ test('it should return error on api failure', async () => {
92
+ const error = new ASCApiError(
93
+ 'server error!',
94
+ Amity.ServerError.ITEM_NOT_FOUND,
95
+ Amity.ErrorLevel.ERROR,
96
+ );
97
+
98
+ // mock response of queryMessages and validate
99
+ const callback = jest.fn();
100
+ client.http.get = jest.fn().mockRejectedValue(error);
101
+
102
+ liveMessages({ channelId }, callback);
103
+
104
+ expect(callback).toHaveBeenCalledTimes(1);
105
+
106
+ // check if cache data returned (should be empty)
107
+ expect(callback).toHaveBeenCalledWith(
108
+ expect.objectContaining({
109
+ data: [],
110
+ error: undefined,
111
+ loading: true,
112
+ }),
113
+ );
114
+
115
+ await expect(queryMessages({ channelId })).rejects.toThrow();
116
+
117
+ expect(callback).toHaveBeenCalledTimes(2);
118
+
119
+ expect(callback).toHaveBeenLastCalledWith(
120
+ expect.objectContaining({
121
+ error,
122
+ data: [],
123
+ loading: false,
124
+ }),
125
+ );
126
+ });
127
+
128
+ test('it should filter deleted messages based on param', async () => {
129
+ // mock response of queryMessages with a deleted message
130
+ const returnValue = [
131
+ { messageId: messages.page1[0], isDeleted: true },
132
+ { messageId: messages.page1[1], isDeleted: false },
133
+ ];
134
+
135
+ // mock response of queryMessages and validate
136
+ const callback = jest.fn();
137
+ client.http.get = jest.fn().mockResolvedValue({
138
+ data: {
139
+ messages: returnValue,
140
+ paging: {},
141
+ },
142
+ });
143
+
144
+ liveMessages({ channelId, isDeleted: false }, callback);
145
+
146
+ expect(callback).toHaveBeenCalledTimes(1);
147
+
148
+ // check if cache data returned (should be empty)
149
+ expect(callback).toHaveBeenCalledWith(
150
+ expect.objectContaining({
151
+ data: [],
152
+ error: undefined,
153
+ loading: true,
154
+ }),
155
+ );
156
+
157
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
158
+
159
+ expect(callback).toHaveBeenCalledTimes(2);
160
+
161
+ expect(callback).toHaveBeenCalledWith(
162
+ expect.objectContaining({
163
+ data: [returnValue[1]],
164
+ error: undefined,
165
+ loading: false,
166
+ }),
167
+ );
168
+ });
169
+
170
+ test('it should return method to fetch next page', async () => {
171
+ const returnValue2 = [{ messageId: messages.page2[0] }];
172
+
173
+ const callback = jest.fn();
174
+ client.http.get = jest
175
+ .fn()
176
+ .mockResolvedValue({
177
+ data: {
178
+ messages: returnValue,
179
+ paging: {
180
+ previous: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
181
+ next: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
182
+ },
183
+ },
184
+ })
185
+ .mockResolvedValueOnce({
186
+ data: {
187
+ messages: returnValue2,
188
+ paging: {
189
+ previous: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
190
+ next: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
191
+ },
192
+ },
193
+ });
194
+
195
+ liveMessages({ channelId }, callback);
196
+
197
+ expect(callback).toHaveBeenCalled();
198
+
199
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
200
+
201
+ expect(callback.mock.lastCall).toHaveLength(1);
202
+
203
+ const { onNextPage, hasNextPage } = callback.mock.lastCall[0];
204
+
205
+ expect(hasNextPage).toBe(true);
206
+ expect(onNextPage).toBeTruthy();
207
+
208
+ onNextPage();
209
+
210
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
211
+
212
+ // 4 -> becuase 1 local & server call each per call (2)
213
+ expect(callback).toHaveBeenCalledTimes(4);
214
+
215
+ expect(callback).toHaveBeenNthCalledWith(
216
+ 4,
217
+ expect.objectContaining({
218
+ data: [...returnValue2, ...returnValue],
219
+ error: undefined,
220
+ loading: false,
221
+ }),
222
+ );
223
+ });
224
+
225
+ test('it should add new message to collection onCreate', async () => {
226
+ const newMessage = {
227
+ ...message,
228
+ messageId: 'new-messageId',
229
+ };
230
+
231
+ // mock response of queryMessages and validate
232
+ const callback = jest.fn();
233
+ client.http.get = jest.fn().mockResolvedValue({
234
+ data: {
235
+ messages: returnValue,
236
+ paging: {},
237
+ },
238
+ });
239
+
240
+ liveMessages({ channelId }, callback);
241
+
242
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
243
+ expect(callback).toHaveBeenCalledTimes(2);
244
+
245
+ // fire new message creation event
246
+ client.emitter.emit('v3.message.didCreate', { messages: [newMessage] });
247
+
248
+ expect(callback).toHaveBeenNthCalledWith(
249
+ 3,
250
+ expect.objectContaining({
251
+ data: [newMessage, ...returnValue],
252
+ error: undefined,
253
+ loading: false,
254
+ }),
255
+ );
256
+ });
257
+
258
+ test('it should add new message to collection onUpdate', async () => {
259
+ // mock response of queryMessages and validate
260
+ const callback = jest.fn();
261
+ client.http.get = jest.fn().mockResolvedValue({
262
+ data: {
263
+ messages: returnValue,
264
+ paging: {},
265
+ },
266
+ });
267
+
268
+ liveMessages({ channelId }, callback);
269
+
270
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
271
+ expect(callback).toHaveBeenCalledTimes(2);
272
+
273
+ // fire new message creation event
274
+ client.emitter.emit('v3.message.didUpdate', { messages: [message] });
275
+
276
+ expect(callback).toHaveBeenNthCalledWith(
277
+ 3,
278
+ expect.objectContaining({
279
+ data: [message],
280
+ error: undefined,
281
+ loading: false,
282
+ }),
283
+ );
284
+ });
285
+
286
+ test('it should add new message to collection onDelete', async () => {
287
+ // mock response of queryMessages and validate
288
+ const callback = jest.fn();
289
+ client.http.get = jest.fn().mockResolvedValue({
290
+ data: {
291
+ messages: returnValue,
292
+ paging: {},
293
+ },
294
+ });
295
+
296
+ liveMessages({ channelId }, callback);
297
+
298
+ await expect(queryMessages({ channelId })).resolves.toBeTruthy();
299
+ expect(callback).toHaveBeenCalledTimes(2);
300
+
301
+ // fire new message creation event
302
+ client.emitter.emit('v3.message.didDelete', { messages: [message] });
303
+
304
+ expect(callback).toHaveBeenNthCalledWith(
305
+ 3,
306
+ expect.objectContaining({
307
+ data: [],
308
+ error: undefined,
309
+ loading: false,
310
+ }),
311
+ );
312
+ });
313
+ });
@@ -14,6 +14,7 @@ import {
14
14
  import {
15
15
  COLLECTION_DEFAULT_CACHING_POLICY,
16
16
  COLLECTION_DEFAULT_PAGINATION_LIMIT,
17
+ ENABLE_CACHE_MESSAGE,
17
18
  } from '~/utils/constants';
18
19
 
19
20
  import {
@@ -57,7 +58,7 @@ export const livePosts = (
57
58
  const { log, cache } = getActiveClient();
58
59
 
59
60
  if (!cache) {
60
- console.log('For using Live Collection feature you need to enable Cache!');
61
+ console.log(ENABLE_CACHE_MESSAGE);
61
62
  }
62
63
 
63
64
  const timestamp = Date.now();
@@ -1,12 +1,12 @@
1
1
  import { ASCApiError } from '~/core/errors';
2
- import { client, userPosts } from '~/utils/tests';
2
+ import { client, posts } from '~/utils/tests';
3
3
  import { pushToCache, enableCache, disableCache } from '~/cache/api';
4
4
 
5
5
  import { getPost } from '~/post/api';
6
6
 
7
7
  describe('post/api', () => {
8
- const post = { postId: userPosts.page1[0] };
9
- const post2 = { postId: userPosts.page1[1] };
8
+ const post = { postId: posts.page1[0] };
9
+ const post2 = { postId: posts.page1[1] };
10
10
 
11
11
  describe('getPost.locally', () => {
12
12
  test('should return undefined if cache is turned off', () => {
@@ -1,9 +1,9 @@
1
1
  import { queryPosts } from '~/post/api';
2
- import { client, userPosts } from '~/utils/tests';
2
+ import { client, posts } from '~/utils/tests';
3
3
 
4
4
  describe('post/api', () => {
5
5
  test('should return a post query', async () => {
6
- const returnValue = [{ postId: userPosts.page1[0] }];
6
+ const returnValue = [{ postId: posts.page1[0] }];
7
7
 
8
8
  client.http.get = jest.fn().mockResolvedValue({
9
9
  data: {
@@ -13,8 +13,8 @@ describe('post/api', () => {
13
13
  });
14
14
 
15
15
  const data = await queryPosts({
16
- targetId: userPosts.targetId,
17
- targetType: userPosts.targetType,
16
+ targetId: posts.targetId,
17
+ targetType: posts.targetType,
18
18
  });
19
19
 
20
20
  expect(data).toBeDefined();
@@ -1,2 +1,3 @@
1
1
  export const COLLECTION_DEFAULT_PAGINATION_LIMIT = 5;
2
2
  export const COLLECTION_DEFAULT_CACHING_POLICY: Amity.QueryPolicy = 'cache_then_server';
3
+ export const ENABLE_CACHE_MESSAGE = 'For using Live Collection feature you need to enable Cache!';
@@ -1,5 +1,33 @@
1
- import { createClient } from '~/client/api';
1
+ import {
2
+ createClient,
3
+ connectClient as sdkConnectClient,
4
+ disconnectClient as sdkDisconnectClient,
5
+ } from '~/client/api';
2
6
 
3
- const client = createClient('key', 'sg');
7
+ import { user11 } from './dummy';
4
8
 
5
- export default client;
9
+ export const client = createClient('key', 'sg');
10
+
11
+ export const connectClient = async () => {
12
+ client.mqtt.connect = jest.fn();
13
+ client.mqtt.subscribe = jest.fn();
14
+ client.http.post = jest.fn().mockReturnValueOnce({
15
+ data: {
16
+ accessToken: 'accessToken',
17
+ refreshToken: 'refreshToken',
18
+ users: [user11],
19
+ },
20
+ });
21
+
22
+ setTimeout(() => {
23
+ client.ws.emit('connect');
24
+ }, 500);
25
+
26
+ await sdkConnectClient({ userId: 'test' });
27
+ };
28
+
29
+ export const disconnectClient = async () => {
30
+ sdkDisconnectClient().then(() => client.ws.emit('disconnect'));
31
+ };
32
+
33
+ export default {};
@@ -0,0 +1,96 @@
1
+ import { date, rolesAndPermissions, user11 } from '.';
2
+
3
+ export const channelRaw1: Amity.ChannelRaw = {
4
+ messageCount: 1,
5
+ createdAt: date,
6
+ type: 'community',
7
+ channelId: 'channelId11',
8
+ };
9
+
10
+ export const channelRaw2: Amity.ChannelRaw = {
11
+ messageCount: 3,
12
+ createdAt: date,
13
+ type: 'community',
14
+ channelId: 'channelId12',
15
+ };
16
+
17
+ export const channelRaw3: Amity.ChannelRaw = {
18
+ messageCount: 3,
19
+ createdAt: date,
20
+ type: 'community',
21
+ channelId: 'channelId21',
22
+ };
23
+
24
+ export const channel11: Amity.Channel = {
25
+ ...channelRaw1,
26
+ hasMention: false,
27
+ unreadCount: 0,
28
+ type: 'community',
29
+ isDeleted: false,
30
+ tags: ['tag2'],
31
+ };
32
+
33
+ export const channel12: Amity.Channel = {
34
+ ...channelRaw2,
35
+ hasMention: false,
36
+ unreadCount: 0,
37
+ type: 'conversation',
38
+ isDeleted: true,
39
+ tags: ['tag1'],
40
+ };
41
+
42
+ export const channel21: Amity.Channel = {
43
+ ...channelRaw3,
44
+ hasMention: false,
45
+ unreadCount: 0,
46
+ type: 'conversation',
47
+ isDeleted: true,
48
+ tags: ['tag1'],
49
+ };
50
+
51
+ export const channelUser1: Amity.Membership<'channel'> = {
52
+ isMuted: false,
53
+ createdAt: date,
54
+ muteTimeout: '',
55
+ isBanned: false,
56
+ readToSegment: 1,
57
+ lastActivity: date,
58
+ membership: 'member',
59
+ userId: user11.userId,
60
+ ...rolesAndPermissions,
61
+ lastMentionedSegment: 0,
62
+ channelId: channel11.channelId,
63
+ };
64
+
65
+ export const channelUser2: Amity.Membership<'channel'> = {
66
+ ...channelUser1,
67
+ readToSegment: 1,
68
+ lastMentionedSegment: 2,
69
+ channelId: channel12.channelId,
70
+ };
71
+
72
+ export const channelQueryResponse = {
73
+ data: {
74
+ channels: [channel11, channel12],
75
+ channelUsers: [],
76
+ files: [],
77
+ users: [],
78
+ paging: {
79
+ previous: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
80
+ next: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
81
+ },
82
+ },
83
+ };
84
+
85
+ export const channelQueryResponsePage2 = {
86
+ data: {
87
+ channels: [channel21],
88
+ channelUsers: [],
89
+ files: [],
90
+ users: [],
91
+ paging: {
92
+ previous: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
93
+ next: 'eyJiZWZvcmUiOjU1LCJsYXN0IjoxMH0=',
94
+ },
95
+ },
96
+ };
@@ -0,0 +1,60 @@
1
+ import { date, user11, user12, user13, user14 } from '.';
2
+
3
+ const timeStamps: Amity.Timestamps = {
4
+ createdAt: date,
5
+ updatedAt: date,
6
+ };
7
+
8
+ // user11 follows user12
9
+ export const follow11: Amity.FollowStatus = {
10
+ ...timeStamps,
11
+ to: user12.userId,
12
+ status: 'accepted',
13
+ from: user11.userId,
14
+ };
15
+
16
+ // user11 pineding user13
17
+ export const follow12: Amity.FollowStatus = {
18
+ ...timeStamps,
19
+ status: 'pending',
20
+ to: user13.userId,
21
+ from: user11.userId,
22
+ };
23
+
24
+ // user14 declined user11
25
+ export const follow13: Amity.FollowStatus = {
26
+ ...timeStamps,
27
+ status: 'none',
28
+ to: user14.userId,
29
+ from: user11.userId,
30
+ };
31
+
32
+ // user12 follows user11
33
+ export const follow21: Amity.FollowStatus = {
34
+ ...timeStamps,
35
+ status: 'accepted',
36
+ to: user11.userId,
37
+ from: user12.userId,
38
+ };
39
+
40
+ // user13 pineding user11
41
+ export const follow22: Amity.FollowStatus = {
42
+ ...timeStamps,
43
+ status: 'pending',
44
+ to: user11.userId,
45
+ from: user13.userId,
46
+ };
47
+
48
+ // user11 declined user14
49
+ export const follow23: Amity.FollowStatus = {
50
+ ...timeStamps,
51
+ status: 'none',
52
+ to: user11.userId,
53
+ from: user14.userId,
54
+ };
55
+
56
+ export const follows = {
57
+ userId: user11.userId,
58
+ page1: [follow11, follow12, follow13],
59
+ page2: [follow21, follow22, follow23],
60
+ };
@@ -0,0 +1,8 @@
1
+ export const date = new Date().toISOString();
2
+ export const rolesAndPermissions = { roles: [], permissions: [] };
3
+
4
+ export * from './user';
5
+ export * from './post';
6
+ export * from './follow';
7
+ export * from './channel';
8
+ export * from './message';
@@ -0,0 +1,21 @@
1
+ import { date, channel11, user11 } from '.';
2
+
3
+ export const message: Amity.Message = {
4
+ reactions: {},
5
+ editedAt: date,
6
+ createdAt: date,
7
+ reactionsCount: 0,
8
+ channelSegment: 1,
9
+ childrenNumber: 0,
10
+ userId: user11.userId,
11
+ messageId: 'message11',
12
+ channelId: channel11.channelId,
13
+ data: { text: 'message1' },
14
+ };
15
+
16
+ export const messages = {
17
+ channelId: 'channelId',
18
+ page1: ['messageId11', 'messageId12', 'messageId13'],
19
+ page2: ['messageId21', 'messageId22', 'messageId23'],
20
+ page3: ['messageId31', 'messageId32', 'messageId33'],
21
+ };
@@ -0,0 +1,31 @@
1
+ import { user11, date } from '.';
2
+
3
+ export const post1: Amity.Post = {
4
+ path: '',
5
+ feedId: '',
6
+ children: [],
7
+ comments: [],
8
+ flagCount: 0,
9
+ reactions: {},
10
+ hashFlag: null,
11
+ editedAt: date,
12
+ createdAt: date,
13
+ commentsCount: 0,
14
+ reactionsCount: 0,
15
+ postId: 'postId11',
16
+ targetType: 'user',
17
+ parentId: 'postId12',
18
+ data: { text: 'post1' },
19
+ targetId: user11.userId,
20
+ hasFlaggedComment: false,
21
+ hasFlaggedChildren: false,
22
+ postedUserId: user11.userId,
23
+ };
24
+
25
+ export const posts = {
26
+ targetId: user11.userId,
27
+ targetType: 'user' as Amity.PostTargetType,
28
+ page1: [post1.postId, 'postId12', 'postId13'],
29
+ page2: ['postId21', 'postId22', 'postId23'],
30
+ page3: ['postId31', 'postId32', 'postId33'],
31
+ };
@@ -0,0 +1,26 @@
1
+ import { date, rolesAndPermissions } from '.';
2
+
3
+ export const user11: Amity.User = {
4
+ _id: '',
5
+ path: '',
6
+ flagCount: 0,
7
+ userId: 'test',
8
+ hashFlag: null,
9
+ createdAt: date,
10
+ ...rolesAndPermissions,
11
+ };
12
+
13
+ export const user12: Amity.User = {
14
+ ...user11,
15
+ userId: 'user2',
16
+ };
17
+
18
+ export const user13: Amity.User = {
19
+ ...user11,
20
+ userId: 'user3',
21
+ };
22
+
23
+ export const user14: Amity.User = {
24
+ ...user11,
25
+ userId: 'user4',
26
+ };
@@ -1,2 +1,3 @@
1
- export { userPosts } from './dummy';
2
- export { default as client } from './client';
1
+ export * from './utils';
2
+ export * from './dummy';
3
+ export * from './client';
@@ -0,0 +1,3 @@
1
+ import { encode } from 'js-base64';
2
+
3
+ export const encodeJson = (value: Record<string, any>) => encode(JSON.stringify(value));
@@ -1 +0,0 @@
1
- {"version":3,"file":"dummy.d.ts","sourceRoot":"","sources":["../../../src/utils/tests/dummy.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;;;;;;CAMrB,CAAC"}
@@ -1,7 +0,0 @@
1
- export const userPosts = {
2
- targetId: 'userId',
3
- targetType: 'user' as Amity.PostTargetType,
4
- page1: ['postId11', 'postId12', 'postId13'],
5
- page2: ['postId21', 'postId22', 'postId23'],
6
- page3: ['postId31', 'postId32', 'postId33'],
7
- };