@amityco/ts-sdk 0.0.1-beta-34 → 0.0.1-beta-35
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.
- package/dist/cache/utils/tests/encodeKey.test.d.ts +2 -0
- package/dist/cache/utils/tests/encodeKey.test.d.ts.map +1 -0
- package/dist/cache/utils/tests/partialMatch.test.d.ts +2 -0
- package/dist/cache/utils/tests/partialMatch.test.d.ts.map +1 -0
- package/dist/client/api/tests/activeClient.test.d.ts +2 -0
- package/dist/client/api/tests/activeClient.test.d.ts.map +1 -0
- package/dist/client/api/tests/activeUser.test.d.ts +2 -0
- package/dist/client/api/tests/activeUser.test.d.ts.map +1 -0
- package/dist/index.cjs.js +9 -0
- package/dist/index.esm.js +9 -0
- package/dist/index.umd.js +1 -1
- package/dist/post/events/tests/onPostApproved.test.d.ts +2 -0
- package/dist/post/events/tests/onPostApproved.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostCreated.test.d.ts +2 -0
- package/dist/post/events/tests/onPostCreated.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostDeclined.test.d.ts +2 -0
- package/dist/post/events/tests/onPostDeclined.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostDeleted.test.d.ts +2 -0
- package/dist/post/events/tests/onPostDeleted.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostFlagged.test.d.ts +2 -0
- package/dist/post/events/tests/onPostFlagged.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostReactionAdded.test.d.ts +2 -0
- package/dist/post/events/tests/onPostReactionAdded.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostReactionRemoved.test.d.ts +2 -0
- package/dist/post/events/tests/onPostReactionRemoved.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostUnflagged.test.d.ts +2 -0
- package/dist/post/events/tests/onPostUnflagged.test.d.ts.map +1 -0
- package/dist/post/events/tests/onPostUpdated.test.d.ts +2 -0
- package/dist/post/events/tests/onPostUpdated.test.d.ts.map +1 -0
- package/dist/post/observers/livePosts.d.ts.map +1 -1
- package/dist/post/observers/tests/livePosts.test.d.ts +2 -0
- package/dist/post/observers/tests/livePosts.test.d.ts.map +1 -0
- package/dist/utils/tests/dummy/post.d.ts +15 -0
- package/dist/utils/tests/dummy/post.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/cache/utils/tests/encodeKey.test.ts +19 -0
- package/src/cache/utils/tests/partialMatch.test.ts +13 -0
- package/src/client/api/tests/activeClient.test.ts +20 -0
- package/src/client/api/tests/activeUser.test.ts +20 -0
- package/src/post/events/tests/onPostApproved.test.ts +33 -0
- package/src/post/events/tests/onPostCreated.test.ts +33 -0
- package/src/post/events/tests/onPostDeclined.test.ts +33 -0
- package/src/post/events/tests/onPostDeleted.test.ts +33 -0
- package/src/post/events/tests/onPostFlagged.test.ts +33 -0
- package/src/post/events/tests/onPostReactionAdded.test.ts +40 -0
- package/src/post/events/tests/onPostReactionRemoved.test.ts +40 -0
- package/src/post/events/tests/onPostUnflagged.test.ts +33 -0
- package/src/post/events/tests/onPostUpdated.test.ts +33 -0
- package/src/post/observers/livePosts.ts +10 -0
- package/src/post/observers/tests/livePosts.test.ts +273 -0
- package/src/utils/tests/dummy/post.ts +34 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { disableCache, enableCache } from '~/cache/api';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
client,
|
|
5
|
+
pause,
|
|
6
|
+
connectClient,
|
|
7
|
+
disconnectClient,
|
|
8
|
+
post11,
|
|
9
|
+
postQueryResponse,
|
|
10
|
+
postQueryResponsePage2,
|
|
11
|
+
} from '~/utils/tests';
|
|
12
|
+
|
|
13
|
+
import { livePosts } from '../livePosts';
|
|
14
|
+
|
|
15
|
+
const getSnapshot = () => {
|
|
16
|
+
return {
|
|
17
|
+
data: [] as Amity.Post[],
|
|
18
|
+
loading: true,
|
|
19
|
+
error: undefined as any,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const { targetId, targetType } = post11;
|
|
24
|
+
const params = { targetId, targetType };
|
|
25
|
+
|
|
26
|
+
describe('livePosts', () => {
|
|
27
|
+
beforeAll(async () => {
|
|
28
|
+
await connectClient();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterAll(async () => {
|
|
32
|
+
await disconnectClient();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
beforeEach(enableCache);
|
|
36
|
+
|
|
37
|
+
afterEach(disableCache);
|
|
38
|
+
|
|
39
|
+
test('it should return post collection', async () => {
|
|
40
|
+
const callback = jest.fn();
|
|
41
|
+
client.http.get = jest.fn().mockResolvedValue(postQueryResponse);
|
|
42
|
+
|
|
43
|
+
livePosts(params, callback);
|
|
44
|
+
|
|
45
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
46
|
+
|
|
47
|
+
const snapshot = getSnapshot();
|
|
48
|
+
|
|
49
|
+
// check if cache data returned (should be empty)
|
|
50
|
+
expect(callback).toHaveBeenCalledWith(expect.objectContaining(snapshot));
|
|
51
|
+
|
|
52
|
+
await pause();
|
|
53
|
+
|
|
54
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
55
|
+
|
|
56
|
+
// cause we apply prepareChannelPayload on post raw data from server
|
|
57
|
+
const payload = postQueryResponse.data;
|
|
58
|
+
|
|
59
|
+
snapshot.data = payload.posts;
|
|
60
|
+
snapshot.loading = false;
|
|
61
|
+
|
|
62
|
+
expect(callback).toHaveBeenCalledWith(expect.objectContaining(snapshot));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const filters: [string, Amity.PostLiveCollection, number?][] = [
|
|
66
|
+
['not deleted', { ...params, isDeleted: false }],
|
|
67
|
+
['deleted', { ...params, isDeleted: true }, 1],
|
|
68
|
+
['tags', { ...params, tags: ['test_tag'] }, 1],
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
test.each(filters)(
|
|
72
|
+
'it should filter by %s posts',
|
|
73
|
+
async (filter, params, expectedPostIdx = 0) => {
|
|
74
|
+
const callback = jest.fn();
|
|
75
|
+
client.http.get = jest.fn().mockResolvedValue(postQueryResponsePage2);
|
|
76
|
+
|
|
77
|
+
const payload = postQueryResponsePage2.data;
|
|
78
|
+
const snapshotData = [payload.posts[expectedPostIdx]];
|
|
79
|
+
|
|
80
|
+
livePosts(params, callback);
|
|
81
|
+
|
|
82
|
+
expect(callback).toHaveBeenCalledTimes(1);
|
|
83
|
+
|
|
84
|
+
const snapshot = getSnapshot();
|
|
85
|
+
// check if cache data returned (should be empty)
|
|
86
|
+
expect(callback).toHaveBeenCalledWith(expect.objectContaining(snapshot));
|
|
87
|
+
|
|
88
|
+
await pause();
|
|
89
|
+
|
|
90
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
91
|
+
|
|
92
|
+
snapshot.loading = false;
|
|
93
|
+
snapshot.data = snapshotData;
|
|
94
|
+
|
|
95
|
+
expect(callback).toHaveBeenCalledWith(expect.objectContaining(snapshot));
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
test('it should return method to fetch next page', async () => {
|
|
100
|
+
const callback = jest.fn();
|
|
101
|
+
client.http.get = jest
|
|
102
|
+
.fn()
|
|
103
|
+
.mockResolvedValue(postQueryResponse)
|
|
104
|
+
.mockResolvedValueOnce(postQueryResponsePage2);
|
|
105
|
+
|
|
106
|
+
livePosts(params, callback);
|
|
107
|
+
|
|
108
|
+
expect(callback).toHaveBeenCalled();
|
|
109
|
+
|
|
110
|
+
await pause();
|
|
111
|
+
|
|
112
|
+
expect(callback.mock.lastCall).toHaveLength(1);
|
|
113
|
+
|
|
114
|
+
const { onNextPage, hasNextPage } = callback.mock.lastCall[0];
|
|
115
|
+
|
|
116
|
+
expect(hasNextPage).toBe(true);
|
|
117
|
+
expect(onNextPage).toBeTruthy();
|
|
118
|
+
|
|
119
|
+
onNextPage();
|
|
120
|
+
|
|
121
|
+
await pause();
|
|
122
|
+
|
|
123
|
+
// 4 -> becuase 1 local & server call each per call (2)
|
|
124
|
+
expect(callback).toHaveBeenCalledTimes(4);
|
|
125
|
+
|
|
126
|
+
const payload = postQueryResponse.data;
|
|
127
|
+
const payload2 = postQueryResponsePage2.data;
|
|
128
|
+
|
|
129
|
+
const snapshot = getSnapshot();
|
|
130
|
+
snapshot.loading = false;
|
|
131
|
+
snapshot.data = [...payload2.posts, ...payload.posts];
|
|
132
|
+
|
|
133
|
+
expect(callback).toHaveBeenNthCalledWith(4, expect.objectContaining(snapshot));
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const events: [string, keyof Amity.Events, Amity.Post][] = [
|
|
137
|
+
[
|
|
138
|
+
'it should add new post to collection onCreate',
|
|
139
|
+
'post.created',
|
|
140
|
+
{ ...post11, postId: 'new-post-id' },
|
|
141
|
+
],
|
|
142
|
+
['it should edit post in collection onUpdate', 'post.updated', post11],
|
|
143
|
+
['it should update post in collection onApproved', 'post.approved', post11],
|
|
144
|
+
['it should remove post in collection onDelete', 'post.deleted', post11],
|
|
145
|
+
['it should remove post in collection onDeclined', 'post.declined', post11],
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
test.each(events)('%s', async (test, event, post) => {
|
|
149
|
+
const snapshot = getSnapshot();
|
|
150
|
+
|
|
151
|
+
const callback = jest.fn();
|
|
152
|
+
client.http.get = jest.fn().mockResolvedValue(postQueryResponse);
|
|
153
|
+
|
|
154
|
+
livePosts(params, callback);
|
|
155
|
+
|
|
156
|
+
await pause();
|
|
157
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
158
|
+
|
|
159
|
+
client.emitter.emit(event, {
|
|
160
|
+
posts: [post],
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const payload = postQueryResponse.data;
|
|
164
|
+
const deletePayload = payload.posts.filter(post => post.postId !== post11.postId);
|
|
165
|
+
|
|
166
|
+
snapshot.loading = false;
|
|
167
|
+
// get data based on event
|
|
168
|
+
switch (event) {
|
|
169
|
+
case 'post.created':
|
|
170
|
+
snapshot.data = [post, ...payload.posts];
|
|
171
|
+
break;
|
|
172
|
+
|
|
173
|
+
case 'post.deleted':
|
|
174
|
+
case 'post.declined':
|
|
175
|
+
snapshot.data = deletePayload;
|
|
176
|
+
break;
|
|
177
|
+
|
|
178
|
+
case 'post.addReaction':
|
|
179
|
+
payload.posts[0] = post;
|
|
180
|
+
snapshot.data = payload.posts;
|
|
181
|
+
break;
|
|
182
|
+
|
|
183
|
+
default:
|
|
184
|
+
snapshot.data = payload.posts;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
expect(callback).toHaveBeenNthCalledWith(3, expect.objectContaining(snapshot));
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
const reactionEvents: [
|
|
191
|
+
string,
|
|
192
|
+
keyof Amity.Events,
|
|
193
|
+
Amity.PostPayload & { reactor: Amity.Reaction },
|
|
194
|
+
][] = [
|
|
195
|
+
[
|
|
196
|
+
'it should update post in collection onReactionAdded',
|
|
197
|
+
'post.addReaction',
|
|
198
|
+
{
|
|
199
|
+
posts: [{ ...post11, reactionsCount: 1 }],
|
|
200
|
+
reactor: {
|
|
201
|
+
referenceId: post11.postId,
|
|
202
|
+
referenceType: 'post',
|
|
203
|
+
reactionId: 'test-reaction-id',
|
|
204
|
+
reactionName: 'like',
|
|
205
|
+
userId: 'test',
|
|
206
|
+
createdAt: '',
|
|
207
|
+
},
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
[
|
|
211
|
+
'it should update post in collection onReactionRemoved',
|
|
212
|
+
'post.removeReaction',
|
|
213
|
+
{
|
|
214
|
+
posts: [{ ...post11, reactionsCount: 0 }],
|
|
215
|
+
reactor: {
|
|
216
|
+
referenceId: post11.postId,
|
|
217
|
+
referenceType: 'post',
|
|
218
|
+
reactionId: 'test-reaction-id',
|
|
219
|
+
reactionName: 'like',
|
|
220
|
+
userId: 'test',
|
|
221
|
+
createdAt: '',
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
test.each(reactionEvents)('%s', async (test, event, reactionPayload) => {
|
|
228
|
+
const expectedReactionCount = reactionPayload.posts[0].reactionsCount;
|
|
229
|
+
|
|
230
|
+
const callback = jest.fn();
|
|
231
|
+
client.http.get = jest.fn().mockResolvedValue(postQueryResponse);
|
|
232
|
+
|
|
233
|
+
livePosts(params, callback);
|
|
234
|
+
|
|
235
|
+
await pause();
|
|
236
|
+
expect(callback).toHaveBeenCalledTimes(2);
|
|
237
|
+
|
|
238
|
+
client.emitter.emit(event, reactionPayload);
|
|
239
|
+
|
|
240
|
+
const { reactionsCount } = callback.mock.lastCall[0].data[0];
|
|
241
|
+
expect(reactionsCount).toBe(expectedReactionCount);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
/*
|
|
245
|
+
* This test was added as when isDeleted posts were queried, the livePosts api
|
|
246
|
+
* would try to delete the post when 'post.deleted' event occured, where instead
|
|
247
|
+
* it should be adding it to the collection. As livePosts is called with a
|
|
248
|
+
* param to reutrun deleted posts only
|
|
249
|
+
*/
|
|
250
|
+
test('it should update posts to include deleted post', async () => {
|
|
251
|
+
const deletedPost = { ...post11, isDeleted: true };
|
|
252
|
+
const callback = jest.fn();
|
|
253
|
+
client.http.get = jest.fn().mockResolvedValue(postQueryResponsePage2);
|
|
254
|
+
|
|
255
|
+
livePosts({ ...params, isDeleted: true }, callback);
|
|
256
|
+
|
|
257
|
+
await pause();
|
|
258
|
+
const payload = postQueryResponsePage2.data.posts.filter(p => p.isDeleted);
|
|
259
|
+
const snapshot = getSnapshot();
|
|
260
|
+
|
|
261
|
+
snapshot.data = payload;
|
|
262
|
+
snapshot.loading = false;
|
|
263
|
+
|
|
264
|
+
expect(callback).toHaveBeenNthCalledWith(2, expect.objectContaining(snapshot));
|
|
265
|
+
|
|
266
|
+
client.emitter.emit('post.deleted', {
|
|
267
|
+
posts: [deletedPost],
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
snapshot.data = [deletedPost, ...payload];
|
|
271
|
+
expect(callback).toHaveBeenNthCalledWith(3, expect.objectContaining(snapshot));
|
|
272
|
+
});
|
|
273
|
+
});
|
|
@@ -58,6 +58,29 @@ export const post13: Amity.Post = {
|
|
|
58
58
|
postId: 'post13',
|
|
59
59
|
};
|
|
60
60
|
|
|
61
|
+
export const post14: Amity.Post = {
|
|
62
|
+
...post11,
|
|
63
|
+
postId: 'post14',
|
|
64
|
+
targetType: 'community',
|
|
65
|
+
isDeleted: false,
|
|
66
|
+
children: [],
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const post15: Amity.Post = {
|
|
70
|
+
...post11,
|
|
71
|
+
postId: 'post15',
|
|
72
|
+
isDeleted: true,
|
|
73
|
+
children: [post11.postId],
|
|
74
|
+
targetType: 'community',
|
|
75
|
+
tags: ['test_tag'],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const post16: Amity.Post = {
|
|
79
|
+
...post11,
|
|
80
|
+
postId: 'post16',
|
|
81
|
+
targetType: 'user',
|
|
82
|
+
};
|
|
83
|
+
|
|
61
84
|
export const posts = {
|
|
62
85
|
targetId: user11.userId,
|
|
63
86
|
targetType: 'user' as Amity.PostTargetType,
|
|
@@ -85,6 +108,17 @@ export const postQueryResponse = {
|
|
|
85
108
|
data: {
|
|
86
109
|
posts: [post11, post12, post13],
|
|
87
110
|
files: [],
|
|
111
|
+
reactor: { userId: 'test' },
|
|
112
|
+
paging: {
|
|
113
|
+
next: nextPage,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const postQueryResponsePage2 = {
|
|
119
|
+
data: {
|
|
120
|
+
posts: [post14, post15, post16],
|
|
121
|
+
files: [],
|
|
88
122
|
paging: {
|
|
89
123
|
next: nextPage,
|
|
90
124
|
},
|