Skip to content

Commit fd74c18

Browse files
committed
chore: type mockClient with StreamChat and use vi.spyOn for public methods
- Type connectUser and mockClient params as StreamChat instead of any - Use bracket notation for internal properties (_user, connectionId, userToken) that are on the StreamChat type but not public API - Replace direct method assignments (client.muteUser = mock) with vi.spyOn(client, 'muteUser').mockImplementation() in Message.test.tsx (12 occurrences) — proper spy pattern for public methods - Type ThreadStart client variable as StreamChat
1 parent bbbdbd4 commit fd74c18

3 files changed

Lines changed: 26 additions & 27 deletions

File tree

src/components/Message/__tests__/Message.test.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ describe('<Message /> component', () => {
384384
const muteUser = vi.fn(() => Promise.resolve());
385385
const userMutedNotification = 'User muted!';
386386
const getMuteUserSuccessNotification = vi.fn(() => userMutedNotification);
387-
client.muteUser = muteUser as any;
387+
vi.spyOn(client, 'muteUser').mockImplementation(muteUser as any);
388388
let context;
389389

390390
await renderComponent({
@@ -412,7 +412,7 @@ describe('<Message /> component', () => {
412412
const client = await getTestClientWithUser(alice);
413413
const addSuccessSpy = vi.spyOn(client.notifications, 'addSuccess');
414414
const muteUser = vi.fn(() => Promise.resolve());
415-
client.muteUser = muteUser as any;
415+
vi.spyOn(client, 'muteUser').mockImplementation(muteUser as any);
416416
let context;
417417

418418
await renderComponent({
@@ -441,7 +441,7 @@ describe('<Message /> component', () => {
441441
const muteUser = vi.fn(() => Promise.reject());
442442
const userMutedFailNotification = 'User mute failed!';
443443
const getMuteUserErrorNotification = vi.fn(() => userMutedFailNotification);
444-
client.muteUser = muteUser as any;
444+
vi.spyOn(client, 'muteUser').mockImplementation(muteUser as any);
445445
let context;
446446

447447
await renderComponent({
@@ -470,7 +470,7 @@ describe('<Message /> component', () => {
470470
const addErrorSpy = vi.spyOn(client.notifications, 'addError');
471471
const muteUser = vi.fn(() => Promise.reject());
472472
const defaultFailNotification = 'Error muting a user ...';
473-
client.muteUser = muteUser as any;
473+
vi.spyOn(client, 'muteUser').mockImplementation(muteUser as any);
474474
let context;
475475

476476
await renderComponent({
@@ -499,7 +499,7 @@ describe('<Message /> component', () => {
499499
const unmuteUser = vi.fn(() => Promise.resolve());
500500
const userUnmutedNotification = 'User unmuted!';
501501
const getMuteUserSuccessNotification = vi.fn(() => userUnmutedNotification);
502-
client.unmuteUser = unmuteUser as any;
502+
vi.spyOn(client, 'unmuteUser').mockImplementation(unmuteUser as any);
503503
let context;
504504

505505
await renderComponent({
@@ -528,7 +528,7 @@ describe('<Message /> component', () => {
528528
const addSuccessSpy = vi.spyOn(client.notifications, 'addSuccess');
529529
const unmuteUser = vi.fn(() => Promise.resolve());
530530
const defaultSuccessNotification = '{{ user }} has been unmuted';
531-
client.unmuteUser = unmuteUser as any;
531+
vi.spyOn(client, 'unmuteUser').mockImplementation(unmuteUser as any);
532532
let context;
533533

534534
await renderComponent({
@@ -557,7 +557,7 @@ describe('<Message /> component', () => {
557557
const unmuteUser = vi.fn(() => Promise.reject());
558558
const userMutedFailNotification = 'User muted failed!';
559559
const getMuteUserErrorNotification = vi.fn(() => userMutedFailNotification);
560-
client.unmuteUser = unmuteUser as any;
560+
vi.spyOn(client, 'unmuteUser').mockImplementation(unmuteUser as any);
561561
let context;
562562

563563
await renderComponent({
@@ -586,7 +586,7 @@ describe('<Message /> component', () => {
586586
const addErrorSpy = vi.spyOn(client.notifications, 'addError');
587587
const unmuteUser = vi.fn(() => Promise.reject());
588588
const defaultFailNotification = 'Error unmuting a user ...';
589-
client.unmuteUser = unmuteUser as any;
589+
vi.spyOn(client, 'unmuteUser').mockImplementation(unmuteUser as any);
590590
let context;
591591

592592
await renderComponent({
@@ -778,7 +778,7 @@ describe('<Message /> component', () => {
778778
const client = await getTestClientWithUser(alice);
779779
const addSuccessSpy = vi.spyOn(client.notifications, 'addSuccess');
780780
const flagMessage = vi.fn(() => Promise.resolve());
781-
client.flagMessage = flagMessage as any;
781+
vi.spyOn(client, 'flagMessage').mockImplementation(flagMessage as any);
782782
const messageFlaggedNotification = 'Message flagged!';
783783
const getFlagMessageSuccessNotification = vi.fn(() => messageFlaggedNotification);
784784
let context;
@@ -807,7 +807,7 @@ describe('<Message /> component', () => {
807807
const client = await getTestClientWithUser(alice);
808808
const addSuccessSpy = vi.spyOn(client.notifications, 'addSuccess');
809809
const flagMessage = vi.fn(() => Promise.resolve());
810-
client.flagMessage = flagMessage as any;
810+
vi.spyOn(client, 'flagMessage').mockImplementation(flagMessage as any);
811811
const defaultSuccessNotification = 'Message has been successfully flagged';
812812
let context;
813813

@@ -834,7 +834,7 @@ describe('<Message /> component', () => {
834834
const client = await getTestClientWithUser(alice);
835835
const addErrorSpy = vi.spyOn(client.notifications, 'addError');
836836
const flagMessage = vi.fn(() => Promise.reject());
837-
client.flagMessage = flagMessage as any;
837+
vi.spyOn(client, 'flagMessage').mockImplementation(flagMessage as any);
838838
const messageFlagFailedNotification = 'Message flagged failed!';
839839
const getFlagMessageErrorNotification = vi.fn(() => messageFlagFailedNotification);
840840
let context;
@@ -863,7 +863,7 @@ describe('<Message /> component', () => {
863863
const client = await getTestClientWithUser(alice);
864864
const addErrorSpy = vi.spyOn(client.notifications, 'addError');
865865
const flagMessage = vi.fn(() => Promise.reject());
866-
client.flagMessage = flagMessage as any;
866+
vi.spyOn(client, 'flagMessage').mockImplementation(flagMessage as any);
867867
const defaultFlagMessageFailedNotification = 'Error adding flag';
868868
let context;
869869

src/components/Thread/__tests__/ThreadStart.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TranslationProvider,
1010
} from '../../../context';
1111

12+
import type { StreamChat } from 'stream-chat';
1213
import {
1314
generateMessage,
1415
getTestClientWithUser,
@@ -17,7 +18,7 @@ import {
1718
mockTranslationContextValue,
1819
} from '../../../mock-builders';
1920

20-
let client: any;
21+
let client: StreamChat;
2122

2223
const mockedChannel = {
2324
off: vi.fn(),

src/mock-builders/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
/* eslint-disable no-underscore-dangle */
21
import { StreamChat } from 'stream-chat';
32
import type { UserResponse } from 'stream-chat';
43
import { nanoid } from 'nanoid';
54

65
const apiKey = 'API_KEY';
76
const token = 'dummy_token';
87

9-
const connectUser = (client: any, user: Partial<UserResponse>) =>
8+
const connectUser = (client: StreamChat, user: Partial<UserResponse>) =>
109
new Promise<void>((resolve) => {
11-
client.connectionId = 'dumm_connection_id';
12-
client.user = user;
13-
client.user.mutes = [];
14-
client._user = { ...user };
10+
client['connectionId'] = 'dumm_connection_id';
11+
client.user = { ...user, mutes: [] } as UserResponse;
12+
client['_user'] = { ...user } as UserResponse;
1513
client.userID = user.id;
16-
client.userToken = token;
17-
client.wsPromise = Promise.resolve(true);
14+
client['userToken'] = token;
15+
client.wsPromise = Promise.resolve(true) as any;
1816
resolve();
1917
});
2018

2119
const noop = () => {};
2220

23-
function mockClient(client: any, mocks: Record<string, any> = {}) {
24-
vi.spyOn(client, '_setToken').mockImplementation(noop);
25-
vi.spyOn(client, '_setupConnection').mockImplementation(noop);
21+
function mockClient(client: StreamChat, mocks: Record<string, any> = {}) {
22+
vi.spyOn(client, '_setToken').mockImplementation(noop as any);
23+
vi.spyOn(client, '_setupConnection').mockImplementation(noop as any);
2624
vi.spyOn(client, 'getAppSettings').mockImplementation(mocks.getAppSettings ?? noop);
2725
vi.spyOn(client, 'queryReactions').mockImplementation(mocks.queryReactions ?? noop);
2826
client.tokenManager = {
2927
getToken: vi.fn(() => token),
3028
tokenReady: vi.fn(() => true),
31-
};
32-
client.connectUser = connectUser.bind(null, client);
33-
return client as StreamChat;
29+
} as any;
30+
client['connectUser'] = connectUser.bind(null, client) as any;
31+
return client;
3432
}
3533

3634
export const getTestClient = (mocks?: Record<string, unknown>) =>

0 commit comments

Comments
 (0)