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

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 (179) 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/domains/category.d.ts +7 -0
  5. package/dist/@types/domains/category.d.ts.map +1 -1
  6. package/dist/@types/domains/community.d.ts +12 -0
  7. package/dist/@types/domains/community.d.ts.map +1 -1
  8. package/dist/@types/global.d.ts +5 -0
  9. package/dist/@types/global.d.ts.map +1 -1
  10. package/dist/category/api/queryCategories.d.ts +1 -5
  11. package/dist/category/api/queryCategories.d.ts.map +1 -1
  12. package/dist/category/observers/index.d.ts +2 -0
  13. package/dist/category/observers/index.d.ts.map +1 -0
  14. package/dist/category/observers/liveCategories.d.ts +18 -0
  15. package/dist/category/observers/liveCategories.d.ts.map +1 -0
  16. package/dist/category/observers/tests/liveCategories.test.d.ts +2 -0
  17. package/dist/category/observers/tests/liveCategories.test.d.ts.map +1 -0
  18. package/dist/channel/api/tests/getChannels.test.d.ts +2 -0
  19. package/dist/channel/api/tests/getChannels.test.d.ts.map +1 -0
  20. package/dist/channel/api/tests/leaveChannel.test.d.ts +2 -0
  21. package/dist/channel/api/tests/leaveChannel.test.d.ts.map +1 -0
  22. package/dist/channel/observers/liveChannel.d.ts.map +1 -1
  23. package/dist/client/api/accessTokenExpiryWatcher.d.ts +4 -1
  24. package/dist/client/api/accessTokenExpiryWatcher.d.ts.map +1 -1
  25. package/dist/client/api/getToken.d.ts +3 -1
  26. package/dist/client/api/getToken.d.ts.map +1 -1
  27. package/dist/client/utils/setClientToken.d.ts +3 -3
  28. package/dist/comment/api/deleteComment.d.ts.map +1 -1
  29. package/dist/comment/api/getComment.d.ts.map +1 -1
  30. package/dist/comment/observers/index.d.ts +1 -0
  31. package/dist/comment/observers/index.d.ts.map +1 -1
  32. package/dist/comment/observers/liveComment.d.ts +21 -0
  33. package/dist/comment/observers/liveComment.d.ts.map +1 -0
  34. package/dist/comment/observers/tests/liveComment.test.d.ts +2 -0
  35. package/dist/comment/observers/tests/liveComment.test.d.ts.map +1 -0
  36. package/dist/community/api/queryCommunities.d.ts +1 -10
  37. package/dist/community/api/queryCommunities.d.ts.map +1 -1
  38. package/dist/community/events/index.d.ts +1 -0
  39. package/dist/community/events/index.d.ts.map +1 -1
  40. package/dist/community/events/onCommunityUserChanged.d.ts +17 -0
  41. package/dist/community/events/onCommunityUserChanged.d.ts.map +1 -0
  42. package/dist/community/events/utils.d.ts +1 -0
  43. package/dist/community/events/utils.d.ts.map +1 -1
  44. package/dist/community/observers/index.d.ts +1 -0
  45. package/dist/community/observers/index.d.ts.map +1 -1
  46. package/dist/community/observers/liveCommunities.d.ts +20 -0
  47. package/dist/community/observers/liveCommunities.d.ts.map +1 -0
  48. package/dist/community/observers/tests/liveCommunities.test.d.ts +2 -0
  49. package/dist/community/observers/tests/liveCommunities.test.d.ts.map +1 -0
  50. package/dist/core/events.d.ts +3 -3
  51. package/dist/core/events.d.ts.map +1 -1
  52. package/dist/core/query/sorting.d.ts +6 -0
  53. package/dist/core/query/sorting.d.ts.map +1 -1
  54. package/dist/core/transports/http.d.ts +0 -5
  55. package/dist/core/transports/http.d.ts.map +1 -1
  56. package/dist/file/api/test/createImage.test.d.ts +2 -0
  57. package/dist/file/api/test/createImage.test.d.ts.map +1 -0
  58. package/dist/file/api/test/createVideo.test.d.ts +2 -0
  59. package/dist/file/api/test/createVideo.test.d.ts.map +1 -0
  60. package/dist/index.cjs.js +441 -195
  61. package/dist/index.d.ts +1 -0
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.esm.js +436 -195
  64. package/dist/index.umd.js +4 -4
  65. package/dist/message/observers/liveMessage.d.ts.map +1 -1
  66. package/dist/post/observers/index.d.ts +1 -0
  67. package/dist/post/observers/index.d.ts.map +1 -1
  68. package/dist/post/observers/livePost.d.ts +21 -0
  69. package/dist/post/observers/livePost.d.ts.map +1 -0
  70. package/dist/post/observers/tests/livePost.test.d.ts +2 -0
  71. package/dist/post/observers/tests/livePost.test.d.ts.map +1 -0
  72. package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts +2 -0
  73. package/dist/subChannel/api/tests/deleteSubChannel.test.d.ts.map +1 -0
  74. package/dist/subChannel/api/tests/getSubChannel.test.d.ts +2 -0
  75. package/dist/subChannel/api/tests/getSubChannel.test.d.ts.map +1 -0
  76. package/dist/subChannel/api/tests/getSubChannels.test.d.ts +2 -0
  77. package/dist/subChannel/api/tests/getSubChannels.test.d.ts.map +1 -0
  78. package/dist/subChannel/api/tests/updateSubChannel.test.d.ts +2 -0
  79. package/dist/subChannel/api/tests/updateSubChannel.test.d.ts.map +1 -0
  80. package/dist/subChannel/observers/liveSubChannel.d.ts.map +1 -1
  81. package/dist/user/api/tests/updateUser.test.d.ts +2 -0
  82. package/dist/user/api/tests/updateUser.test.d.ts.map +1 -0
  83. package/dist/user/observers/liveUser.d.ts.map +1 -1
  84. package/dist/utils/liveObject.d.ts +2 -0
  85. package/dist/utils/liveObject.d.ts.map +1 -0
  86. package/dist/utils/tests/dummy/category.d.ts +64 -0
  87. package/dist/utils/tests/dummy/category.d.ts.map +1 -0
  88. package/dist/utils/tests/dummy/channel.d.ts +50 -0
  89. package/dist/utils/tests/dummy/channel.d.ts.map +1 -1
  90. package/dist/utils/tests/dummy/community.d.ts +132 -0
  91. package/dist/utils/tests/dummy/community.d.ts.map +1 -1
  92. package/dist/utils/tests/dummy/file.d.ts +3 -0
  93. package/dist/utils/tests/dummy/file.d.ts.map +1 -1
  94. package/dist/utils/tests/dummy/index.d.ts +1 -0
  95. package/dist/utils/tests/dummy/index.d.ts.map +1 -1
  96. package/dist/utils/tests/dummy/post.d.ts +2 -0
  97. package/dist/utils/tests/dummy/post.d.ts.map +1 -1
  98. package/dist/utils/tests/dummy/subChannel.d.ts +0 -20
  99. package/dist/utils/tests/dummy/subChannel.d.ts.map +1 -1
  100. package/dist/utils/tests/dummy/user.d.ts +18 -0
  101. package/dist/utils/tests/dummy/user.d.ts.map +1 -1
  102. package/dist/utils/tests/liveObject.test.d.ts +2 -0
  103. package/dist/utils/tests/liveObject.test.d.ts.map +1 -0
  104. package/package.json +4 -4
  105. package/src/@types/core/events.ts +1 -0
  106. package/src/@types/domains/category.ts +13 -0
  107. package/src/@types/domains/community.ts +25 -0
  108. package/src/@types/global.ts +5 -0
  109. package/src/category/api/queryCategories.ts +3 -5
  110. package/src/category/observers/index.ts +1 -0
  111. package/src/category/observers/liveCategories.ts +141 -0
  112. package/src/category/observers/tests/liveCategories.test.ts +147 -0
  113. package/src/channel/api/tests/createChannel.test.ts +30 -6
  114. package/src/channel/api/tests/getChannel.test.ts +3 -1
  115. package/src/channel/api/tests/getChannels.test.ts +97 -0
  116. package/src/channel/api/tests/joinChannel.test.ts +62 -3
  117. package/src/channel/api/tests/leaveChannel.test.ts +90 -0
  118. package/src/channel/api/tests/queryChannels.test.ts +38 -1
  119. package/src/channel/observers/liveChannel.ts +8 -80
  120. package/src/client/api/accessTokenExpiryWatcher.ts +7 -5
  121. package/src/client/api/tests/accessTokenExpiryWatcher.test.ts +8 -5
  122. package/src/comment/api/deleteComment.ts +5 -5
  123. package/src/comment/api/getComment.ts +8 -3
  124. package/src/comment/observers/index.ts +1 -0
  125. package/src/comment/observers/liveComment.ts +43 -0
  126. package/src/comment/observers/tests/liveComment.test.ts +95 -0
  127. package/src/community/api/queryCommunities.ts +3 -10
  128. package/src/community/api/test/addCommunityMember.test.ts +14 -2
  129. package/src/community/api/test/joinCommunity.test.ts +14 -2
  130. package/src/community/api/test/leaveCommunity.test.ts +11 -1
  131. package/src/community/api/test/removeCommunityMembers.test.ts +11 -1
  132. package/src/community/events/index.ts +1 -0
  133. package/src/community/events/onCommunityUserChanged.ts +20 -0
  134. package/src/community/events/test/onCommunityJoined.test.ts +1 -1
  135. package/src/community/events/test/onCommunityUserAdded.test.ts +1 -1
  136. package/src/community/events/test/onCommunityUserBanned.test.ts +1 -1
  137. package/src/community/events/test/onCommunityUserUnbaned.test.ts +1 -1
  138. package/src/community/events/test/utils.test.ts +52 -3
  139. package/src/community/events/utils.ts +49 -0
  140. package/src/community/observers/index.ts +1 -0
  141. package/src/community/observers/liveCommunities.ts +188 -0
  142. package/src/community/observers/tests/liveCommunities.test.ts +284 -0
  143. package/src/core/query/sorting.ts +8 -0
  144. package/src/core/transports/http.ts +17 -13
  145. package/src/core/transports/test/http.test.ts +49 -60
  146. package/src/file/api/createFile.ts +1 -1
  147. package/src/file/api/createImage.ts +1 -1
  148. package/src/file/api/createVideo.ts +1 -1
  149. package/src/file/api/test/createImage.test.ts +69 -0
  150. package/src/file/api/test/createVideo.test.ts +81 -0
  151. package/src/index.ts +1 -0
  152. package/src/message/observers/liveMessage.ts +11 -83
  153. package/src/message/tests/observers/liveMessage.test.ts +1 -143
  154. package/src/post/observers/index.ts +1 -0
  155. package/src/post/observers/livePost.ts +47 -0
  156. package/src/post/observers/tests/livePost.test.ts +97 -0
  157. package/src/stream/api/tests/createStream.test.ts +3 -11
  158. package/src/stream/api/tests/deleteStream.test.ts +5 -3
  159. package/src/subChannel/api/tests/createSubChannel.test.ts +21 -14
  160. package/src/subChannel/api/tests/deleteSubChannel.test.ts +112 -0
  161. package/src/subChannel/api/tests/getSubChannel.test.ts +97 -0
  162. package/src/subChannel/api/tests/getSubChannels.test.ts +97 -0
  163. package/src/subChannel/api/tests/updateSubChannel.test.ts +86 -0
  164. package/src/subChannel/observers/liveSubChannel.ts +6 -56
  165. package/src/subChannel/observers/tests/liveSubChannel.test.ts +0 -82
  166. package/src/user/api/tests/updateUser.test.ts +60 -0
  167. package/src/user/observers/liveUser.ts +9 -61
  168. package/src/user/tests/observers/liveUser.test.ts +1 -154
  169. package/src/utils/liveObject.ts +86 -0
  170. package/src/utils/tests/dummy/category.ts +57 -0
  171. package/src/utils/tests/dummy/channel.ts +51 -1
  172. package/src/utils/tests/dummy/community.ts +94 -0
  173. package/src/utils/tests/dummy/file.ts +32 -0
  174. package/src/utils/tests/dummy/index.ts +1 -0
  175. package/src/utils/tests/dummy/post.ts +56 -45
  176. package/src/utils/tests/dummy/stream.ts +73 -73
  177. package/src/utils/tests/dummy/subChannel.ts +0 -22
  178. package/src/utils/tests/dummy/user.ts +21 -0
  179. package/src/utils/tests/liveObject.test.ts +172 -0
@@ -0,0 +1,97 @@
1
+ import { disableCache, enableCache } from '~/cache/api';
2
+ import {
3
+ pause,
4
+ client,
5
+ connectClient,
6
+ disconnectClient,
7
+ generatePost,
8
+ user12,
9
+ } from '~/utils/tests';
10
+ import { getFutureDate } from '~/core/model';
11
+ import { livePost } from '../livePost';
12
+
13
+ describe('livePost', () => {
14
+ const post = generatePost();
15
+ const response = { data: { posts: [post] } };
16
+
17
+ beforeAll(async () => {
18
+ await connectClient();
19
+ });
20
+
21
+ afterAll(async () => {
22
+ await disconnectClient();
23
+ });
24
+
25
+ beforeEach(enableCache);
26
+
27
+ afterEach(disableCache);
28
+
29
+ const events: [string, keyof Amity.MqttPostEvents][] = [
30
+ ['should get update on post.updated event', 'post.updated'],
31
+ ['should get update on post.deleted event', 'post.deleted'],
32
+ ['should get update on post.approved event', 'post.approved'],
33
+ ['should get update on post.declined event', 'post.declined'],
34
+ ['should get update on post.flagged event', 'post.flagged'],
35
+ ['should get update on post.unflagged event', 'post.unflagged'],
36
+ ];
37
+
38
+ test.each(events)('%s', async (test, event) => {
39
+ const update = {
40
+ ...post,
41
+ data: { text: 'new-text' },
42
+ updatedAt: getFutureDate(post.updatedAt),
43
+ };
44
+
45
+ const callback = jest.fn();
46
+ client.http.get = jest.fn().mockResolvedValue(response);
47
+
48
+ livePost(post.postId, callback);
49
+ await pause();
50
+ client.emitter.emit(event, { posts: [update] });
51
+ await pause();
52
+
53
+ expect(callback).toHaveBeenCalledTimes(3);
54
+ expect(callback).toHaveBeenNthCalledWith(
55
+ 3,
56
+ expect.objectContaining({
57
+ data: update,
58
+ loading: false,
59
+ }),
60
+ );
61
+ });
62
+
63
+ test.each([
64
+ ['should get update on post.addReaction event', 'post.addReaction'],
65
+ ['should get update on post.removeReaction event', 'post.removeReaction'],
66
+ ] as [string, keyof Amity.MqttPostEvents][])('%s', async (test, event) => {
67
+ const update = {
68
+ ...post,
69
+ data: { text: 'new-text' },
70
+ updatedAt: getFutureDate(post.updatedAt),
71
+ };
72
+ const reactor: Amity.Reaction = {
73
+ createdAt: getFutureDate(post.updatedAt),
74
+ reactionId: 'like',
75
+ reactionName: 'like',
76
+ referenceId: post.postId,
77
+ referenceType: 'post',
78
+ userId: user12.userId,
79
+ };
80
+ const callback = jest.fn();
81
+ client.http.get = jest.fn().mockResolvedValue(response);
82
+
83
+ livePost(post.postId, callback);
84
+ await pause();
85
+ client.emitter.emit(event, { posts: [update], reactor });
86
+ await pause();
87
+
88
+ expect(callback).toHaveBeenCalledTimes(3);
89
+ expect(callback).toHaveBeenNthCalledWith(
90
+ 3,
91
+ expect.objectContaining({
92
+ data: update,
93
+ loading: false,
94
+ }),
95
+ );
96
+ });
97
+ });
@@ -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
  };