@amityco/ts-sdk 0.0.1-beta.37 → 0.0.1-beta.39

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 (251) hide show
  1. package/.eslintrc.json +3 -5
  2. package/dist/@types/core/events.d.ts +1 -0
  3. package/dist/@types/core/events.d.ts.map +1 -1
  4. package/dist/@types/core/model.d.ts +2 -0
  5. package/dist/@types/core/model.d.ts.map +1 -1
  6. package/dist/@types/domains/category.d.ts +7 -0
  7. package/dist/@types/domains/category.d.ts.map +1 -1
  8. package/dist/@types/domains/channel.d.ts +2 -2
  9. package/dist/@types/domains/channel.d.ts.map +1 -1
  10. package/dist/@types/domains/community.d.ts +12 -0
  11. package/dist/@types/domains/community.d.ts.map +1 -1
  12. package/dist/@types/domains/feed.d.ts +1 -2
  13. package/dist/@types/domains/feed.d.ts.map +1 -1
  14. package/dist/@types/domains/message.d.ts +1 -1
  15. package/dist/@types/domains/message.d.ts.map +1 -1
  16. package/dist/@types/domains/post.d.ts +1 -1
  17. package/dist/@types/domains/post.d.ts.map +1 -1
  18. package/dist/@types/domains/user.d.ts +8 -0
  19. package/dist/@types/domains/user.d.ts.map +1 -1
  20. package/dist/@types/global.d.ts +5 -0
  21. package/dist/@types/global.d.ts.map +1 -1
  22. package/dist/category/api/queryCategories.d.ts +1 -5
  23. package/dist/category/api/queryCategories.d.ts.map +1 -1
  24. package/dist/category/observers/index.d.ts +2 -0
  25. package/dist/category/observers/index.d.ts.map +1 -0
  26. package/dist/category/observers/liveCategories.d.ts +18 -0
  27. package/dist/category/observers/liveCategories.d.ts.map +1 -0
  28. package/dist/category/observers/tests/liveCategories.test.d.ts +2 -0
  29. package/dist/category/observers/tests/liveCategories.test.d.ts.map +1 -0
  30. package/dist/channel/api/tests/getChannels.test.d.ts +2 -0
  31. package/dist/channel/api/tests/getChannels.test.d.ts.map +1 -0
  32. package/dist/channel/api/tests/leaveChannel.test.d.ts +2 -0
  33. package/dist/channel/api/tests/leaveChannel.test.d.ts.map +1 -0
  34. package/dist/channel/observers/liveChannel.d.ts.map +1 -1
  35. package/dist/channel/observers/liveChannels.d.ts.map +1 -1
  36. package/dist/client/api/accessTokenExpiryWatcher.d.ts +4 -1
  37. package/dist/client/api/accessTokenExpiryWatcher.d.ts.map +1 -1
  38. package/dist/client/api/connectClient.d.ts.map +1 -1
  39. package/dist/client/api/getToken.d.ts +3 -1
  40. package/dist/client/api/getToken.d.ts.map +1 -1
  41. package/dist/client/utils/setClientToken.d.ts +3 -3
  42. package/dist/comment/api/deleteComment.d.ts.map +1 -1
  43. package/dist/comment/api/getComment.d.ts.map +1 -1
  44. package/dist/comment/observers/index.d.ts +1 -0
  45. package/dist/comment/observers/index.d.ts.map +1 -1
  46. package/dist/comment/observers/liveComment.d.ts +21 -0
  47. package/dist/comment/observers/liveComment.d.ts.map +1 -0
  48. package/dist/comment/observers/tests/liveComment.test.d.ts +2 -0
  49. package/dist/comment/observers/tests/liveComment.test.d.ts.map +1 -0
  50. package/dist/community/api/queryCommunities.d.ts +1 -10
  51. package/dist/community/api/queryCommunities.d.ts.map +1 -1
  52. package/dist/community/events/index.d.ts +1 -0
  53. package/dist/community/events/index.d.ts.map +1 -1
  54. package/dist/community/events/onCommunityUserChanged.d.ts +17 -0
  55. package/dist/community/events/onCommunityUserChanged.d.ts.map +1 -0
  56. package/dist/community/events/utils.d.ts +1 -0
  57. package/dist/community/events/utils.d.ts.map +1 -1
  58. package/dist/community/observers/index.d.ts +1 -0
  59. package/dist/community/observers/index.d.ts.map +1 -1
  60. package/dist/community/observers/liveCommunities.d.ts +20 -0
  61. package/dist/community/observers/liveCommunities.d.ts.map +1 -0
  62. package/dist/community/observers/tests/liveCommunities.test.d.ts +2 -0
  63. package/dist/community/observers/tests/liveCommunities.test.d.ts.map +1 -0
  64. package/dist/core/events.d.ts +3 -3
  65. package/dist/core/events.d.ts.map +1 -1
  66. package/dist/core/model/idResolvers.d.ts.map +1 -1
  67. package/dist/core/model/identifyModel.d.ts.map +1 -1
  68. package/dist/core/model/index.d.ts.map +1 -1
  69. package/dist/core/query/filtering.d.ts +10 -0
  70. package/dist/core/query/filtering.d.ts.map +1 -1
  71. package/dist/core/query/paging.d.ts +2 -2
  72. package/dist/core/query/paging.d.ts.map +1 -1
  73. package/dist/core/query/sorting.d.ts +6 -0
  74. package/dist/core/query/sorting.d.ts.map +1 -1
  75. package/dist/core/query/tests/paging.test.d.ts +2 -0
  76. package/dist/core/query/tests/paging.test.d.ts.map +1 -0
  77. package/dist/core/transports/http.d.ts +0 -5
  78. package/dist/core/transports/http.d.ts.map +1 -1
  79. package/dist/file/api/test/createImage.test.d.ts +2 -0
  80. package/dist/file/api/test/createImage.test.d.ts.map +1 -0
  81. package/dist/file/api/test/createVideo.test.d.ts +2 -0
  82. package/dist/file/api/test/createVideo.test.d.ts.map +1 -0
  83. package/dist/index.cjs.js +607 -252
  84. package/dist/index.d.ts +1 -0
  85. package/dist/index.d.ts.map +1 -1
  86. package/dist/index.esm.js +599 -251
  87. package/dist/index.umd.js +4 -4
  88. package/dist/message/api/queryMessages.d.ts +2 -2
  89. package/dist/message/api/queryMessages.d.ts.map +1 -1
  90. package/dist/message/observers/liveMessage.d.ts.map +1 -1
  91. package/dist/post/observers/index.d.ts +1 -0
  92. package/dist/post/observers/index.d.ts.map +1 -1
  93. package/dist/post/observers/livePost.d.ts +21 -0
  94. package/dist/post/observers/livePost.d.ts.map +1 -0
  95. package/dist/post/observers/livePosts.d.ts +2 -2
  96. package/dist/post/observers/livePosts.d.ts.map +1 -1
  97. package/dist/post/observers/tests/livePost.test.d.ts +2 -0
  98. package/dist/post/observers/tests/livePost.test.d.ts.map +1 -0
  99. package/dist/report/api/createReport.d.ts.map +1 -1
  100. package/dist/report/api/deleteReport.d.ts.map +1 -1
  101. package/dist/report/api/tests/createReport.test.d.ts +2 -0
  102. package/dist/report/api/tests/createReport.test.d.ts.map +1 -0
  103. package/dist/report/api/tests/deleteReport.test.d.ts +2 -0
  104. package/dist/report/api/tests/deleteReport.test.d.ts.map +1 -0
  105. package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts +2 -0
  106. package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts.map +1 -0
  107. package/dist/subChannel/api/tests/getSubChannel.test.d.ts +2 -0
  108. package/dist/subChannel/api/tests/getSubChannel.test.d.ts.map +1 -0
  109. package/dist/subChannel/api/tests/getSubChannels.test.d.ts +2 -0
  110. package/dist/subChannel/api/tests/getSubChannels.test.d.ts.map +1 -0
  111. package/dist/subChannel/api/tests/updateSubChannel.test.d.ts +2 -0
  112. package/dist/subChannel/api/tests/updateSubChannel.test.d.ts.map +1 -0
  113. package/dist/subChannel/observers/liveSubChannel.d.ts.map +1 -1
  114. package/dist/user/api/queryUsers.d.ts +1 -6
  115. package/dist/user/api/queryUsers.d.ts.map +1 -1
  116. package/dist/user/api/tests/updateUser.test.d.ts +2 -0
  117. package/dist/user/api/tests/updateUser.test.d.ts.map +1 -0
  118. package/dist/user/observers/index.d.ts +1 -0
  119. package/dist/user/observers/index.d.ts.map +1 -1
  120. package/dist/user/observers/liveUser.d.ts.map +1 -1
  121. package/dist/user/observers/liveUsers.d.ts +18 -0
  122. package/dist/user/observers/liveUsers.d.ts.map +1 -0
  123. package/dist/user/observers/tests/liveUsers.test.d.ts +2 -0
  124. package/dist/user/observers/tests/liveUsers.test.d.ts.map +1 -0
  125. package/dist/utils/liveObject.d.ts +2 -0
  126. package/dist/utils/liveObject.d.ts.map +1 -0
  127. package/dist/utils/tests/dummy/category.d.ts +64 -0
  128. package/dist/utils/tests/dummy/category.d.ts.map +1 -0
  129. package/dist/utils/tests/dummy/channel.d.ts +50 -0
  130. package/dist/utils/tests/dummy/channel.d.ts.map +1 -1
  131. package/dist/utils/tests/dummy/community.d.ts +132 -0
  132. package/dist/utils/tests/dummy/community.d.ts.map +1 -1
  133. package/dist/utils/tests/dummy/feed.d.ts +21 -0
  134. package/dist/utils/tests/dummy/feed.d.ts.map +1 -0
  135. package/dist/utils/tests/dummy/file.d.ts +3 -0
  136. package/dist/utils/tests/dummy/file.d.ts.map +1 -1
  137. package/dist/utils/tests/dummy/index.d.ts +2 -0
  138. package/dist/utils/tests/dummy/index.d.ts.map +1 -1
  139. package/dist/utils/tests/dummy/post.d.ts +11 -0
  140. package/dist/utils/tests/dummy/post.d.ts.map +1 -1
  141. package/dist/utils/tests/dummy/subChannel.d.ts +0 -20
  142. package/dist/utils/tests/dummy/subChannel.d.ts.map +1 -1
  143. package/dist/utils/tests/dummy/user.d.ts +81 -0
  144. package/dist/utils/tests/dummy/user.d.ts.map +1 -1
  145. package/dist/utils/tests/liveObject.test.d.ts +2 -0
  146. package/dist/utils/tests/liveObject.test.d.ts.map +1 -0
  147. package/package.json +4 -4
  148. package/src/@types/core/events.ts +1 -0
  149. package/src/@types/core/model.ts +4 -0
  150. package/src/@types/domains/category.ts +13 -0
  151. package/src/@types/domains/channel.ts +2 -2
  152. package/src/@types/domains/community.ts +25 -0
  153. package/src/@types/domains/feed.ts +1 -3
  154. package/src/@types/domains/message.ts +4 -1
  155. package/src/@types/domains/post.ts +1 -1
  156. package/src/@types/domains/user.ts +14 -0
  157. package/src/@types/global.ts +5 -0
  158. package/src/cache/api/tests/ingestInCache.test.ts +2 -0
  159. package/src/category/api/queryCategories.ts +3 -5
  160. package/src/category/observers/index.ts +1 -0
  161. package/src/category/observers/liveCategories.ts +141 -0
  162. package/src/category/observers/tests/liveCategories.test.ts +147 -0
  163. package/src/channel/api/tests/createChannel.test.ts +30 -6
  164. package/src/channel/api/tests/getChannel.test.ts +3 -1
  165. package/src/channel/api/tests/getChannels.test.ts +97 -0
  166. package/src/channel/api/tests/joinChannel.test.ts +62 -3
  167. package/src/channel/api/tests/leaveChannel.test.ts +90 -0
  168. package/src/channel/api/tests/queryChannels.test.ts +38 -1
  169. package/src/channel/observers/liveChannel.ts +8 -80
  170. package/src/channel/observers/liveChannels.ts +29 -24
  171. package/src/client/api/accessTokenExpiryWatcher.ts +7 -5
  172. package/src/client/api/connectClient.ts +0 -2
  173. package/src/client/api/tests/accessTokenExpiryWatcher.test.ts +8 -5
  174. package/src/comment/api/deleteComment.ts +5 -5
  175. package/src/comment/api/getComment.ts +8 -3
  176. package/src/comment/observers/index.ts +1 -0
  177. package/src/comment/observers/liveComment.ts +43 -0
  178. package/src/comment/observers/tests/liveComment.test.ts +95 -0
  179. package/src/community/api/queryCommunities.ts +3 -10
  180. package/src/community/api/test/addCommunityMember.test.ts +14 -2
  181. package/src/community/api/test/joinCommunity.test.ts +14 -2
  182. package/src/community/api/test/leaveCommunity.test.ts +11 -1
  183. package/src/community/api/test/removeCommunityMembers.test.ts +11 -1
  184. package/src/community/events/index.ts +1 -0
  185. package/src/community/events/onCommunityUserChanged.ts +20 -0
  186. package/src/community/events/test/onCommunityJoined.test.ts +1 -1
  187. package/src/community/events/test/onCommunityUserAdded.test.ts +1 -1
  188. package/src/community/events/test/onCommunityUserBanned.test.ts +1 -1
  189. package/src/community/events/test/onCommunityUserUnbaned.test.ts +1 -1
  190. package/src/community/events/test/utils.test.ts +52 -3
  191. package/src/community/events/utils.ts +49 -0
  192. package/src/community/observers/index.ts +1 -0
  193. package/src/community/observers/liveCommunities.ts +188 -0
  194. package/src/community/observers/tests/liveCommunities.test.ts +284 -0
  195. package/src/core/model/idResolvers.ts +2 -0
  196. package/src/core/model/identifyModel.ts +1 -0
  197. package/src/core/model/index.ts +2 -0
  198. package/src/core/query/filtering.ts +33 -3
  199. package/src/core/query/paging.ts +5 -4
  200. package/src/core/query/sorting.ts +8 -0
  201. package/src/core/query/tests/paging.test.ts +66 -0
  202. package/src/core/transports/http.ts +17 -13
  203. package/src/core/transports/test/http.test.ts +49 -60
  204. package/src/file/api/createFile.ts +1 -1
  205. package/src/file/api/createImage.ts +1 -1
  206. package/src/file/api/createVideo.ts +1 -1
  207. package/src/file/api/test/createImage.test.ts +69 -0
  208. package/src/file/api/test/createVideo.test.ts +81 -0
  209. package/src/index.ts +1 -0
  210. package/src/message/api/queryMessages.ts +7 -7
  211. package/src/message/api/tests/queryMessages.test.ts +6 -6
  212. package/src/message/observers/liveMessage.ts +11 -83
  213. package/src/message/tests/observers/liveMessage.test.ts +1 -143
  214. package/src/message/tests/observers/liveMessages.test.ts +4 -4
  215. package/src/post/observers/index.ts +1 -0
  216. package/src/post/observers/livePost.ts +47 -0
  217. package/src/post/observers/livePosts.ts +14 -22
  218. package/src/post/observers/tests/livePost.test.ts +97 -0
  219. package/src/post/observers/tests/livePosts.test.ts +65 -8
  220. package/src/report/api/createReport.ts +1 -6
  221. package/src/report/api/deleteReport.ts +2 -6
  222. package/src/report/api/tests/createReport.test.ts +93 -0
  223. package/src/report/api/tests/deleteReport.test.ts +93 -0
  224. package/src/stream/api/tests/createStream.test.ts +3 -11
  225. package/src/stream/api/tests/deleteStream.test.ts +5 -3
  226. package/src/subChannel/api/tests/createSubChannel.test.ts +21 -14
  227. package/src/subChannel/api/tests/deleteSubChannel.test.ts +112 -0
  228. package/src/subChannel/api/tests/getSubChannel.test.ts +97 -0
  229. package/src/subChannel/api/tests/getSubChannels.test.ts +97 -0
  230. package/src/subChannel/api/tests/updateSubChannel.test.ts +86 -0
  231. package/src/subChannel/observers/liveSubChannel.ts +6 -56
  232. package/src/subChannel/observers/tests/liveSubChannel.test.ts +0 -82
  233. package/src/user/api/queryUsers.ts +1 -6
  234. package/src/user/api/tests/updateUser.test.ts +60 -0
  235. package/src/user/observers/index.ts +1 -0
  236. package/src/user/observers/liveUser.ts +9 -61
  237. package/src/user/observers/liveUsers.ts +162 -0
  238. package/src/user/observers/tests/liveUsers.test.ts +202 -0
  239. package/src/user/tests/observers/liveUser.test.ts +1 -154
  240. package/src/utils/liveObject.ts +86 -0
  241. package/src/utils/tests/dummy/category.ts +57 -0
  242. package/src/utils/tests/dummy/channel.ts +52 -1
  243. package/src/utils/tests/dummy/community.ts +94 -0
  244. package/src/utils/tests/dummy/feed.ts +13 -0
  245. package/src/utils/tests/dummy/file.ts +32 -0
  246. package/src/utils/tests/dummy/index.ts +3 -0
  247. package/src/utils/tests/dummy/post.ts +62 -47
  248. package/src/utils/tests/dummy/stream.ts +73 -73
  249. package/src/utils/tests/dummy/subChannel.ts +0 -22
  250. package/src/utils/tests/dummy/user.ts +57 -0
  251. package/src/utils/tests/liveObject.test.ts +172 -0
@@ -0,0 +1,93 @@
1
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
2
+ import {
3
+ client,
4
+ connectClient,
5
+ disconnectClient,
6
+ flaggedUser as unflaggedUser,
7
+ flaggedUserQueryResponse as unflaggedUserQueryResponse,
8
+ } from '~/utils/tests';
9
+ import { ASCApiError } from '~/core/errors';
10
+
11
+ import { onUserUnflagged } from '~/user/events';
12
+
13
+ import { deleteReport } from '..';
14
+ import REFERENCE_TYPES from '../../constants/referenceTypes';
15
+
16
+ describe('deleteReport', () => {
17
+ beforeAll(connectClient);
18
+ afterAll(disconnectClient);
19
+
20
+ // integration_test_id: fddf1d63-98e2-4296-ab3a-4e2b55044f89
21
+ test('it should unflag user with userId', async () => {
22
+ const expected = true;
23
+ client.http.delete = jest.fn().mockResolvedValue(unflaggedUserQueryResponse);
24
+
25
+ const received = await deleteReport('user', unflaggedUser.userId);
26
+
27
+ expect(received).toBe(expected);
28
+ });
29
+
30
+ // integration_test_id: a95a0784-56ce-4b17-ac1b-0f1c50a361ef
31
+ test('it should throw error with 400400 when sending invalid userId', async () => {
32
+ client.http.delete = jest
33
+ .fn()
34
+ .mockRejectedValueOnce(
35
+ new ASCApiError('unauthorized', Amity.ServerError.ITEM_NOT_FOUND, Amity.ErrorLevel.ERROR),
36
+ );
37
+
38
+ await expect(deleteReport('user', unflaggedUser.userId)).rejects.toThrow('400400');
39
+ });
40
+
41
+ test('it should fire event for user', async () => {
42
+ let dispose;
43
+ const expected = unflaggedUser;
44
+
45
+ client.http.delete = jest.fn().mockResolvedValue(unflaggedUserQueryResponse);
46
+
47
+ const callbackPromise = new Promise(resolve => {
48
+ dispose = onUserUnflagged(resolve);
49
+ }).finally(dispose);
50
+
51
+ const recieved = await deleteReport('user', unflaggedUser.userId);
52
+
53
+ expect(recieved).toBe(true);
54
+ await expect(callbackPromise).resolves.toEqual(expect.objectContaining(expected));
55
+ });
56
+
57
+ test.todo('it should fire event for unflagging message');
58
+ test.todo('it should fire event for unflagging delete');
59
+
60
+ const tests: [keyof typeof REFERENCE_TYPES, string, string][] = [
61
+ ['user', 'userId', `/api/v4/me/flags/${encodeURIComponent('userId')}`],
62
+ ['post', 'postId', `/api/v3/posts/${encodeURIComponent('postId')}/unflag`],
63
+ ['message', 'messageId', `/api/v5/messages/${encodeURIComponent('messageId')}/flags`],
64
+ ];
65
+
66
+ test.each(tests)(
67
+ 'it should call the appropriate api for %s',
68
+ async (referenceType, referenceId, expected) => {
69
+ const apimock = jest.fn();
70
+
71
+ client.http.delete = apimock.mockResolvedValue({ data: {} });
72
+
73
+ await deleteReport(referenceType, referenceId);
74
+
75
+ const received = apimock.mock.lastCall[0];
76
+
77
+ expect(received).toBe(expected);
78
+ },
79
+ );
80
+
81
+ test('it should add data to cache', async () => {
82
+ enableCache();
83
+ const expected = unflaggedUser;
84
+ client.http.delete = jest.fn().mockResolvedValue(unflaggedUserQueryResponse);
85
+
86
+ await deleteReport('user', unflaggedUser.userId);
87
+ const recieved = pullFromCache(['user', 'get', unflaggedUser.userId]);
88
+
89
+ expect(recieved?.data).toBe(expected);
90
+
91
+ disableCache();
92
+ });
93
+ });
@@ -1,15 +1,10 @@
1
1
  import { getActiveClient } from '~/client/api';
2
- import {
3
- client,
4
- connectClient,
5
- disconnectClient,
6
- streamResponse,
7
- } from '~/utils/tests';
2
+ import { client, connectClient, disconnectClient, streamResponse } from '~/utils/tests';
8
3
  import { createStream } from '../createStream';
9
4
 
10
5
  const bundle: Parameters<typeof createStream>[0] = {
11
6
  title: 'my stream',
12
- 'thumbnailFileId': streamResponse.data.videoStreamings[0].thumbnailFileId,
7
+ thumbnailFileId: streamResponse.data.videoStreamings[0].thumbnailFileId,
13
8
  };
14
9
 
15
10
  describe('createStream', () => {
@@ -21,10 +16,7 @@ describe('createStream', () => {
21
16
 
22
17
  const created = await createStream(bundle);
23
18
 
24
- expect(getActiveClient().http.post).toHaveBeenCalledWith(
25
- `/api/v3/video-streaming`,
26
- bundle
27
- );
19
+ expect(getActiveClient().http.post).toHaveBeenCalledWith(`/api/v3/video-streaming`, bundle);
28
20
  expect(created.data).toEqual(streamResponse.data.videoStreamings[0]);
29
21
  });
30
22
  });
@@ -39,9 +39,11 @@ describe('deleteStream', () => {
39
39
  await deleteStream(STREAM_ID);
40
40
  const recieved = pullFromCache(['stream', 'get', STREAM_ID])?.data;
41
41
 
42
- expect(recieved).toEqual(expect.objectContaining({
43
- isDeleted: true
44
- }));
42
+ expect(recieved).toEqual(
43
+ expect.objectContaining({
44
+ isDeleted: true,
45
+ }),
46
+ );
45
47
 
46
48
  disableCache();
47
49
  });
@@ -1,14 +1,10 @@
1
1
  import { createSubChannel } from '../createSubChannel';
2
- import {
3
- client,
4
- generateRawSubChannel,
5
- createSubChannelError400301,
6
- createSubChannelError400400,
7
- } from '~/utils/tests';
2
+ import { client, generateRawSubChannel } from '~/utils/tests';
8
3
 
9
4
  import { onSubChannelCreated } from '~/subChannel/events';
10
5
 
11
- import { disableCache, enableCache, pullFromCache, pushToCache } from '~/cache/api';
6
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
7
+ import { ASCApiError } from '~/core/errors';
12
8
 
13
9
  const subChannelId = 'channelId11--sub-channel-id';
14
10
 
@@ -38,37 +34,48 @@ describe('createSubChannel', () => {
38
34
 
39
35
  // integration_test_id: f5b376b7-32b6-47f3-a5a1-553221340d17
40
36
  test('it should return error 400400 when try to create subChannel from wrong parent channel Id', async () => {
37
+ const apiError = new ASCApiError(
38
+ 'Channel not found.',
39
+ Amity.ServerError.ITEM_NOT_FOUND,
40
+ Amity.ErrorLevel.ERROR,
41
+ );
42
+
41
43
  client.http.post = jest.fn().mockRejectedValue({
42
44
  status: 404,
43
- data: createSubChannelError400400,
45
+ data: apiError,
44
46
  });
45
47
 
46
48
  try {
47
49
  await createSubChannel({
48
- channelId: createSubChannelError400400.data.detail.channelId,
50
+ channelId: 'invalid_channel_id',
49
51
  displayName: 'sub channel name',
50
52
  });
51
53
  } catch ({ status = null, data = null }) {
52
54
  expect(status).toEqual(404);
53
- expect(data).toEqual(createSubChannelError400400);
55
+ expect(data).toEqual(apiError);
54
56
  }
55
57
  });
56
58
 
57
59
  // integration_test_id: 73b59e57-2330-4ed3-95bd-241c9e7a2c43
58
- test('it should return error 400301 when try to create subChannel from channel Id', async () => {
60
+ test('it should return error 400301 when try to create subChannel from channel Id which current user is not member', async () => {
61
+ const apiError = new ASCApiError(
62
+ 'You are not a member of this channel.',
63
+ Amity.ServerError.PERMISSION_DENIED,
64
+ Amity.ErrorLevel.ERROR,
65
+ );
59
66
  client.http.post = jest.fn().mockRejectedValue({
60
67
  status: 403,
61
- data: createSubChannelError400301,
68
+ data: apiError,
62
69
  });
63
70
 
64
71
  try {
65
72
  await createSubChannel({
66
- channelId: createSubChannelError400301.data.detail.channelId,
73
+ channelId: 'invalid_channel_id',
67
74
  displayName: 'sub channel name',
68
75
  });
69
76
  } catch ({ status = null, data = null }) {
70
77
  expect(status).toEqual(403);
71
- expect(data).toEqual(createSubChannelError400301);
78
+ expect(data).toEqual(apiError);
72
79
  }
73
80
  });
74
81
 
@@ -0,0 +1,112 @@
1
+ import { deleteSubChannel } from '../deleteSubChannel';
2
+ import { onSubChannelDeleted } from '../../events';
3
+
4
+ import { client, generateRawSubChannel } from '~/utils/tests';
5
+
6
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
7
+ import { ASCApiError } from '~/core/errors';
8
+
9
+ const subChannelId = 'channelId11--sub-channel-id';
10
+
11
+ describe('createSubChannel', () => {
12
+ beforeEach(enableCache);
13
+
14
+ afterEach(disableCache);
15
+
16
+ // integration_test_id: 43ba1273-4560-4cea-93ce-cf5728b2a475
17
+ test('it should delete sub channel', async () => {
18
+ const rawSubChannel = generateRawSubChannel();
19
+ const rawDeleteSubChannel = generateRawSubChannel({ isDeleted: true });
20
+
21
+ // Mock for getSubChannel
22
+ client.http.get = jest.fn().mockResolvedValue({
23
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
24
+ });
25
+
26
+ client.http.delete = jest.fn().mockResolvedValue({
27
+ data: {
28
+ messageFeeds: [rawDeleteSubChannel],
29
+ messages: [],
30
+ users: [],
31
+ files: [],
32
+ },
33
+ });
34
+
35
+ const result = await deleteSubChannel(subChannelId);
36
+ expect(result.isDeleted).toEqual(true);
37
+ });
38
+
39
+ // integration_test_id: 57ba785f-484b-4f19-aa38-18e0bd4ec433
40
+ test('it should return error 400400 when try to delete subChannel from invalid subChannelId', async () => {
41
+ const apiError = new ASCApiError(
42
+ 'Feed not found',
43
+ Amity.ServerError.ITEM_NOT_FOUND,
44
+ Amity.ErrorLevel.ERROR,
45
+ );
46
+
47
+ client.http.get = jest.fn().mockRejectedValue({
48
+ status: 404,
49
+ data: apiError,
50
+ });
51
+
52
+ try {
53
+ await deleteSubChannel(subChannelId);
54
+ } catch ({ status = null, data = null }) {
55
+ expect(status).toEqual(404);
56
+ expect(data).toEqual(apiError);
57
+ }
58
+ });
59
+
60
+ test('it should return a valid sub channel from cache', async () => {
61
+ const rawSubChannel = generateRawSubChannel();
62
+ const rawDeleteSubChannel = generateRawSubChannel({ isDeleted: true });
63
+
64
+ // Mock for getSubChannel
65
+ client.http.get = jest.fn().mockResolvedValue({
66
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
67
+ });
68
+
69
+ client.http.delete = jest.fn().mockResolvedValue({
70
+ data: {
71
+ messageFeeds: [rawDeleteSubChannel],
72
+ messages: [],
73
+ users: [],
74
+ files: [],
75
+ },
76
+ });
77
+
78
+ await deleteSubChannel(subChannelId);
79
+
80
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
81
+
82
+ expect(cache?.data).toEqual(expect.objectContaining({ isDeleted: true }));
83
+ });
84
+
85
+ test('should fire event `onChannelDeleted`', async () => {
86
+ let dispose;
87
+ const rawSubChannel = generateRawSubChannel();
88
+ const rawDeleteSubChannel = generateRawSubChannel({ isDeleted: true });
89
+
90
+ // Mock for getSubChannel
91
+ client.http.get = jest.fn().mockResolvedValue({
92
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
93
+ });
94
+
95
+ client.http.delete = jest.fn().mockResolvedValue({
96
+ data: {
97
+ messageFeeds: [rawDeleteSubChannel],
98
+ messages: [],
99
+ users: [],
100
+ files: [],
101
+ },
102
+ });
103
+
104
+ const callbackPromise = new Promise(resolve => {
105
+ dispose = onSubChannelDeleted(resolve);
106
+ }).finally(dispose);
107
+
108
+ await deleteSubChannel(subChannelId);
109
+
110
+ await expect(callbackPromise).resolves.toEqual(expect.objectContaining({ isDeleted: true }));
111
+ });
112
+ });
@@ -0,0 +1,97 @@
1
+ import { getSubChannel } from '../getSubChannel';
2
+ import { client, generateRawSubChannel } from '~/utils/tests';
3
+
4
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
5
+ import { ASCApiError } from '~/core/errors';
6
+ import { onSubChannelFetched } from '../../events/onSubChannelFetched';
7
+
8
+ const subChannelId = 'channelId11--sub-channel-id';
9
+
10
+ const subChannelPayload = {
11
+ channelId: 'channelId11',
12
+ displayName: 'sub channel name',
13
+ };
14
+
15
+ describe('createSubChannel', () => {
16
+ beforeEach(enableCache);
17
+
18
+ afterEach(disableCache);
19
+
20
+ // integration_test_id: 6f019056-921e-43e2-a426-dd5a96f2fdcf
21
+ test('it should return a valid sub channel', async () => {
22
+ const rawSubChannel = generateRawSubChannel();
23
+ client.http.get = jest.fn().mockResolvedValue({
24
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
25
+ });
26
+
27
+ const { data } = await getSubChannel(subChannelId);
28
+
29
+ expect(data.channelId).toEqual(subChannelPayload.channelId);
30
+ expect(data.displayName).toEqual(subChannelPayload.displayName);
31
+ expect(data.subChannelId).toEqual(subChannelId);
32
+ });
33
+
34
+ // integration_test_id: 0dc440b6-b2bb-41fb-bbe8-1b2a4c71bffc
35
+ test('it should return error 400400 when try to get subChannel from invalid subChannelId', async () => {
36
+ const apiError = new ASCApiError(
37
+ 'Feed not found',
38
+ Amity.ServerError.ITEM_NOT_FOUND,
39
+ Amity.ErrorLevel.ERROR,
40
+ );
41
+
42
+ client.http.put = jest.fn().mockRejectedValue({
43
+ status: 404,
44
+ data: apiError,
45
+ });
46
+
47
+ try {
48
+ await getSubChannel(subChannelId);
49
+ } catch ({ status = null, data = null }) {
50
+ expect(status).toEqual(404);
51
+ expect(data).toEqual(apiError);
52
+ }
53
+ });
54
+
55
+ test('it should return a valid sub channel from cache', async () => {
56
+ const rawSubChannel = generateRawSubChannel();
57
+ client.http.put = jest.fn().mockResolvedValue({
58
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
59
+ });
60
+
61
+ const { data } = await getSubChannel(subChannelId);
62
+
63
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
64
+
65
+ expect(cache?.data).toEqual(data);
66
+ });
67
+
68
+ test('it should return a valid sub channel from locally cache', async () => {
69
+ const rawSubChannel = generateRawSubChannel();
70
+ client.http.put = jest.fn().mockResolvedValue({
71
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
72
+ });
73
+
74
+ await getSubChannel(subChannelId);
75
+
76
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
77
+ const locallyData = await getSubChannel.locally(subChannelId);
78
+
79
+ expect(cache?.data).toEqual(locallyData?.data);
80
+ });
81
+
82
+ test('should fire event `onSubChannelFetched`', async () => {
83
+ let dispose;
84
+ const rawSubChannel = generateRawSubChannel();
85
+ client.http.put = jest.fn().mockResolvedValue({
86
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
87
+ });
88
+
89
+ const callbackPromise = new Promise(resolve => {
90
+ dispose = onSubChannelFetched(resolve);
91
+ }).finally(dispose);
92
+
93
+ const { data } = await getSubChannel(subChannelId);
94
+
95
+ await expect(callbackPromise).resolves.toMatchObject(data);
96
+ });
97
+ });
@@ -0,0 +1,97 @@
1
+ import { getSubChannels } from '../getSubChannels';
2
+ import { client, generateRawSubChannel } from '~/utils/tests';
3
+
4
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
5
+ import { ASCApiError } from '~/core/errors';
6
+ import { onSubChannelFetched } from '../../events/onSubChannelFetched';
7
+
8
+ const subChannelId = 'channelId11--sub-channel-id';
9
+
10
+ const subChannelPayload = {
11
+ channelId: 'channelId11',
12
+ displayName: 'sub channel name',
13
+ };
14
+
15
+ describe('createSubChannel', () => {
16
+ beforeEach(enableCache);
17
+
18
+ afterEach(disableCache);
19
+
20
+ // integration_test_id: dc771f42-14b9-420e-999d-858151e8ce41
21
+ test('it should return a valid sub channel', async () => {
22
+ const rawSubChannel = generateRawSubChannel();
23
+ client.http.get = jest.fn().mockResolvedValue({
24
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
25
+ });
26
+
27
+ const { data } = await getSubChannels([subChannelId]);
28
+
29
+ expect(data[0].channelId).toEqual(subChannelPayload.channelId);
30
+ expect(data[0].displayName).toEqual(subChannelPayload.displayName);
31
+ expect(data[0].subChannelId).toEqual(subChannelId);
32
+ });
33
+
34
+ // integration_test_id: 28710df0-e5b6-4e46-b1aa-440d5ea01995
35
+ test('it should return error 400400 when try to get subChannel from invalid subChannelId', async () => {
36
+ const apiError = new ASCApiError(
37
+ 'Feed not found',
38
+ Amity.ServerError.ITEM_NOT_FOUND,
39
+ Amity.ErrorLevel.ERROR,
40
+ );
41
+
42
+ client.http.get = jest.fn().mockRejectedValue({
43
+ status: 404,
44
+ data: apiError,
45
+ });
46
+
47
+ try {
48
+ await getSubChannels([subChannelId]);
49
+ } catch ({ status = null, data = null }) {
50
+ expect(status).toEqual(404);
51
+ expect(data).toEqual(apiError);
52
+ }
53
+ });
54
+
55
+ test('it should return a valid sub channel from cache', async () => {
56
+ const rawSubChannel = generateRawSubChannel();
57
+ client.http.get = jest.fn().mockResolvedValue({
58
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
59
+ });
60
+
61
+ const { data } = await getSubChannels([subChannelId]);
62
+
63
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
64
+
65
+ expect(cache?.data).toEqual(data[0]);
66
+ });
67
+
68
+ test('it should return a valid sub channel from locally cache', async () => {
69
+ const rawSubChannel = generateRawSubChannel();
70
+ client.http.get = jest.fn().mockResolvedValue({
71
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
72
+ });
73
+
74
+ await getSubChannels([subChannelId]);
75
+
76
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
77
+ const locallyData = await getSubChannels.locally([subChannelId]);
78
+
79
+ expect(cache?.data).toEqual(locallyData?.data[0]);
80
+ });
81
+
82
+ test('should fire event `onSubChannelFetched`', async () => {
83
+ let dispose;
84
+ const rawSubChannel = generateRawSubChannel();
85
+ client.http.get = jest.fn().mockResolvedValue({
86
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
87
+ });
88
+
89
+ const callbackPromise = new Promise(resolve => {
90
+ dispose = onSubChannelFetched(resolve);
91
+ }).finally(dispose);
92
+
93
+ const { data } = await getSubChannels([subChannelId]);
94
+
95
+ await expect(callbackPromise).resolves.toMatchObject(data[0]);
96
+ });
97
+ });
@@ -0,0 +1,86 @@
1
+ import { updateSubChannel } from '../updateSubChannel';
2
+ import { client, generateRawSubChannel } from '~/utils/tests';
3
+
4
+ import { onSubChannelUpdated } from '~/subChannel/events';
5
+
6
+ import { disableCache, enableCache, pullFromCache } from '~/cache/api';
7
+ import { ASCApiError } from '~/core/errors';
8
+
9
+ const subChannelId = 'channelId11--sub-channel-id';
10
+
11
+ const subChannelPayload = {
12
+ channelId: 'channelId11',
13
+ displayName: 'sub channel name',
14
+ };
15
+
16
+ describe('createSubChannel', () => {
17
+ beforeEach(enableCache);
18
+
19
+ afterEach(disableCache);
20
+
21
+ // integration_test_id: 9f50fa02-c26c-4666-8e8b-2d9076a3edd3
22
+ test('it should return a valid sub channel', async () => {
23
+ const rawSubChannel = generateRawSubChannel();
24
+ client.http.put = jest.fn().mockResolvedValue({
25
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
26
+ });
27
+
28
+ const { data } = await updateSubChannel(subChannelId, subChannelPayload);
29
+
30
+ expect(data.channelId).toEqual(subChannelPayload.channelId);
31
+ expect(data.displayName).toEqual(subChannelPayload.displayName);
32
+ expect(data.subChannelId).toEqual(subChannelId);
33
+ });
34
+
35
+ // integration_test_id: 118f410c-5476-4c4e-9199-ac166da30840
36
+ test('it should return error 400400 when try to update subChannel from wrong sub channel Id', async () => {
37
+ const apiError = new ASCApiError(
38
+ 'Feed not found',
39
+ Amity.ServerError.ITEM_NOT_FOUND,
40
+ Amity.ErrorLevel.ERROR,
41
+ );
42
+
43
+ client.http.put = jest.fn().mockRejectedValue({
44
+ status: 404,
45
+ data: apiError,
46
+ });
47
+
48
+ try {
49
+ await updateSubChannel(subChannelId, {
50
+ displayName: 'sub channel name',
51
+ });
52
+ } catch ({ status = null, data = null }) {
53
+ expect(status).toEqual(404);
54
+ expect(data).toEqual(apiError);
55
+ }
56
+ });
57
+
58
+ test('it should return a valid sub channel from cache', async () => {
59
+ const rawSubChannel = generateRawSubChannel();
60
+ client.http.put = jest.fn().mockResolvedValue({
61
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
62
+ });
63
+
64
+ const { data } = await updateSubChannel(subChannelId, subChannelPayload);
65
+
66
+ const cache = pullFromCache(['subChannel', 'get', subChannelId]);
67
+
68
+ expect(cache?.data).toEqual(data);
69
+ });
70
+
71
+ test('should fire event `onSubChannelUpdated`', async () => {
72
+ let dispose;
73
+ const rawSubChannel = generateRawSubChannel();
74
+ client.http.put = jest.fn().mockResolvedValue({
75
+ data: { messageFeeds: [rawSubChannel], messages: [], users: [], files: [] },
76
+ });
77
+
78
+ const callbackPromise = new Promise(resolve => {
79
+ dispose = onSubChannelUpdated(resolve);
80
+ }).finally(dispose);
81
+
82
+ const { data } = await updateSubChannel(subChannelId, subChannelPayload);
83
+
84
+ await expect(callbackPromise).resolves.toMatchObject(data);
85
+ });
86
+ });
@@ -1,7 +1,4 @@
1
- import { getActiveClient } from '~/client/api';
2
- import { createQuery, runQuery } from '~/core/query';
3
- import { LIVE_OBJECT_ENABLE_CACHE_MESSAGE } from '~/utils/constants';
4
- import { hasUpdates } from '~/core/model';
1
+ import { liveObject } from '~/utils/liveObject';
5
2
  import { getSubChannel } from '../api';
6
3
  import { onSubChannelDeleted, onSubChannelUpdated } from '../events';
7
4
  import { onSubChannelFetched } from '../events/onSubChannelFetched';
@@ -29,56 +26,9 @@ export const liveSubChannel = (
29
26
  subChannelId: Amity.SubChannel['subChannelId'],
30
27
  callback: Amity.LiveObjectCallback<Amity.SubChannel>,
31
28
  ): Amity.Unsubscriber => {
32
- const { log, cache } = getActiveClient();
33
- const timestamp = Date.now();
34
- log(`liveSubChannel(tmpid: ${timestamp}) > listen`);
35
-
36
- if (!cache) {
37
- console.log(LIVE_OBJECT_ENABLE_CACHE_MESSAGE);
38
- }
39
-
40
- let subChannel: Amity.SubChannel;
41
- const disposers: Amity.Unsubscriber[] = [];
42
-
43
- const dispatcher = (data: Amity.LiveObject<Amity.SubChannel>) => {
44
- subChannel = data.data;
45
- callback(data);
46
- };
47
-
48
- const realtimeRouter = (eventSubChannel: Amity.SubChannel) => {
49
- if (subChannelId !== eventSubChannel.subChannelId) {
50
- return;
51
- }
52
-
53
- if (subChannel && !hasUpdates(subChannel, eventSubChannel)) {
54
- return;
55
- }
56
-
57
- dispatcher({ loading: false, data: eventSubChannel, origin: 'event' });
58
- };
59
-
60
- const onFetch = () => {
61
- const query = createQuery(getSubChannel, subChannelId);
62
-
63
- runQuery(query, ({ error, data, loading, origin }) => {
64
- dispatcher({ error, data, loading, origin });
65
-
66
- if (error) {
67
- disposers.forEach(fn => fn());
68
- }
69
- });
70
- };
71
-
72
- disposers.push(
73
- onSubChannelFetched(realtimeRouter),
74
- onSubChannelUpdated(realtimeRouter),
75
- onSubChannelDeleted(realtimeRouter),
76
- );
77
-
78
- onFetch();
79
-
80
- return () => {
81
- log(`liveSubChannel(tmpid: ${timestamp}) > dispose`);
82
- disposers.forEach(fn => fn());
83
- };
29
+ return liveObject(subChannelId, callback, 'subChannelId', getSubChannel, [
30
+ onSubChannelFetched,
31
+ onSubChannelUpdated,
32
+ onSubChannelDeleted,
33
+ ]);
84
34
  };