Skip to content

Commit ddeb0a3

Browse files
Add handle-user-updated tests
1 parent 043598b commit ddeb0a3

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, it, beforeEach, expect } from 'vitest';
2+
3+
import { FeedsClient, handleUserUpdated } from '../..';
4+
import { generateOwnUser, generateUserResponse } from '../../../test-utils';
5+
import { EventPayload } from '../../../types-internal';
6+
7+
describe('handleUserUpdated', () => {
8+
let feedsClient: FeedsClient;
9+
10+
beforeEach(() => {
11+
feedsClient = new FeedsClient('mock-api-key');
12+
const connectedUser = generateOwnUser();
13+
14+
feedsClient.state.partialNext({ connected_user: connectedUser });
15+
});
16+
17+
it('should update the connected user in the state', () => {
18+
const stateBefore = feedsClient.state.getLatestValue();
19+
20+
const event: EventPayload<'user.updated'> = {
21+
type: 'user.updated',
22+
created_at: new Date(),
23+
custom: {},
24+
user: {
25+
...generateUserResponse(),
26+
...stateBefore.connected_user!,
27+
},
28+
};
29+
30+
handleUserUpdated.call(feedsClient, event);
31+
32+
const stateAfter = feedsClient.state.getLatestValue();
33+
34+
expect(stateAfter.connected_user).toMatchObject({ name: event.user.name });
35+
});
36+
37+
it('should not update the connected user if the incoming event contains other user', () => {
38+
const event: EventPayload<'user.updated'> = {
39+
type: 'user.updated',
40+
created_at: new Date(),
41+
custom: {},
42+
user: generateUserResponse(),
43+
};
44+
45+
const stateBefore = feedsClient.state.getLatestValue();
46+
47+
handleUserUpdated.call(feedsClient, event);
48+
49+
const stateAfter = feedsClient.state.getLatestValue();
50+
51+
expect(stateAfter.connected_user).toBe(stateBefore.connected_user);
52+
});
53+
});

packages/feeds-client/src/test-utils/response-generators.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const generateUserResponse = (
1313
overrides: Partial<UserResponse> = {},
1414
): UserResponse => ({
1515
id: `user-${getHumanId()}`,
16+
name: humanId({ separator: ' ' }),
1617
created_at: new Date(),
1718
updated_at: new Date(),
1819
banned: false,
@@ -58,28 +59,26 @@ export const generateFeedResponse = (
5859
const id = overrides.id || `feed-${getHumanId()}`;
5960
const groupId = overrides.group_id || 'user';
6061
const fid = `${groupId}:${id}`;
61-
const createdBy = generateUserResponse(overrides.created_by);
62-
const description = humanId({
63-
addAdverb: true,
64-
adjectiveCount: 4,
65-
});
66-
const name = humanId();
6762

6863
return {
6964
id,
7065
group_id: groupId,
7166
created_at: new Date(),
7267
updated_at: new Date(),
73-
description,
74-
fid,
68+
description: humanId({
69+
addAdverb: true,
70+
adjectiveCount: 4,
71+
separator: ' ',
72+
}),
7573
follower_count: 0,
7674
following_count: 0,
7775
member_count: 0,
78-
name,
76+
name: humanId({ separator: ' ' }),
7977
pin_count: 0,
8078
custom: {},
8179
...overrides,
82-
created_by: createdBy,
80+
fid,
81+
created_by: generateUserResponse(overrides.created_by),
8382
};
8483
};
8584

0 commit comments

Comments
 (0)