Skip to content

Commit f08b541

Browse files
committed
chore: add type annotations to untyped let declarations in tests
Annotate ~30 let variables across 14 test files with types inferred from their assignments: - client/chatClient β†’ StreamChat (from getTestClientWithUser/getTestClient) - channel β†’ Channel (from client.channel()) - testChannel1/2/3 β†’ ChannelAPIResponse (from generateChannel()) - users β†’ UserResponse[] (from createClientWithChannel) - containerIsThread β†’ boolean Also fixed two bugs surfaced by typing: - TypingIndicator: ch.id β†’ ch.channel.id (ChannelAPIResponse has .channel.id) - ChannelHeader: testChannel1.id β†’ testChannel1.channel.id
1 parent cab3ffd commit f08b541

File tree

14 files changed

+51
-37
lines changed

14 files changed

+51
-37
lines changed

β€Žsrc/components/ChannelHeader/__tests__/ChannelHeader.test.tsxβ€Ž

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@ import {
2222
} from '../../../mock-builders';
2323
import { axe } from '../../../../axe-helper';
2424
import { ChannelAvatar } from '../../Avatar';
25-
import type { ChannelResponse, Channel as ChannelType, StreamChat } from 'stream-chat';
25+
import type {
26+
ChannelAPIResponse,
27+
ChannelResponse,
28+
Channel as ChannelType,
29+
StreamChat,
30+
} from 'stream-chat';
2631
import type { ChannelHeaderProps } from '../ChannelHeader';
2732
import type { GenerateChannelOptions } from '../../../mock-builders/generator/channel';
2833

2934
const AVATAR_IMG_TEST_ID = 'avatar-img';
3035

3136
const user1 = generateUser();
3237
const user2 = generateUser({ image: null });
33-
let testChannel1;
34-
let client;
38+
let testChannel1: ChannelAPIResponse;
39+
let client: StreamChat;
3540

3641
const CustomMenuIcon = () => <div id='custom-icon'>Custom Menu Icon</div>;
3742
const defaultChannelState = {
@@ -72,7 +77,7 @@ async function renderComponent({
7277
testChannel1 = generateChannel({ ...defaultChannelState, channel: channelData });
7378
/* eslint-disable-next-line react-hooks/rules-of-hooks */
7479
useMockedApis(client, [getOrCreateChannelApi(testChannel1)]);
75-
const channel = client.channel(channelType, testChannel1.id, channelData);
80+
const channel = client.channel(channelType, testChannel1.channel.id, channelData);
7681
await channel.query();
7782

7883
return renderComponentBase({ channel, client, props });

β€Žsrc/components/ChannelList/__tests__/ChannelList.test.tsxβ€Ž

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect } from 'react';
22
import { nanoid } from 'nanoid';
33
import { SearchController } from 'stream-chat';
4-
import type { StreamChat } from 'stream-chat';
4+
import type { ChannelAPIResponse, StreamChat } from 'stream-chat';
55
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
66
import { axe } from '../../../../axe-helper';
77

@@ -84,10 +84,10 @@ const ChannelListComponent = (props) => {
8484
const ROLE_LIST_ITEM_SELECTOR = '[role="listitem"]';
8585
const SEARCH_RESULT_LIST_SELECTOR = '.str-chat__search-results';
8686
describe('ChannelList', () => {
87-
let chatClient;
88-
let testChannel1;
89-
let testChannel2;
90-
let testChannel3;
87+
let chatClient: StreamChat;
88+
let testChannel1: ChannelAPIResponse;
89+
let testChannel2: ChannelAPIResponse;
90+
let testChannel3: ChannelAPIResponse;
9191

9292
beforeEach(async () => {
9393
chatClient = await getTestClientWithUser({ id: 'uthred' });
@@ -666,8 +666,8 @@ describe('ChannelList', () => {
666666
});
667667

668668
it('should fall back to the first channel when `customActiveChannel` is not found', async () => {
669-
chatClient.axiosInstance.post.mockReset();
670-
chatClient.axiosInstance.post
669+
vi.mocked(chatClient.axiosInstance.post).mockReset();
670+
vi.mocked(chatClient.axiosInstance.post)
671671
.mockResolvedValueOnce(queryChannelsApi([testChannel1, testChannel2]).response)
672672
.mockResolvedValueOnce(queryChannelsApi([]).response);
673673

@@ -1941,7 +1941,7 @@ describe('ChannelList', () => {
19411941

19421942
await waitFor(() => {
19431943
expect(chatClient.queryChannels).toHaveBeenCalledTimes(2);
1944-
expect(chatClient.queryChannels.mock.calls[1][2]).toStrictEqual(
1944+
expect(vi.mocked(chatClient.queryChannels).mock.calls[1][2]).toStrictEqual(
19451945
expect.objectContaining({ offset: 0 }),
19461946
);
19471947
});
@@ -1964,7 +1964,7 @@ describe('ChannelList', () => {
19641964

19651965
await waitFor(() => {
19661966
expect(chatClient.queryChannels).toHaveBeenCalledTimes(2);
1967-
expect(chatClient.queryChannels.mock.calls[1][2]).toStrictEqual(
1967+
expect(vi.mocked(chatClient.queryChannels).mock.calls[1][2]).toStrictEqual(
19681968
expect.objectContaining({ offset: 0 }),
19691969
);
19701970
});
@@ -1977,7 +1977,7 @@ describe('ChannelList', () => {
19771977

19781978
await waitFor(() => {
19791979
expect(chatClient.queryChannels).toHaveBeenCalledTimes(3);
1980-
expect(chatClient.queryChannels.mock.calls[2][2]).toStrictEqual(
1980+
expect(vi.mocked(chatClient.queryChannels).mock.calls[2][2]).toStrictEqual(
19811981
expect.objectContaining({ offset: 0 }),
19821982
);
19831983
});
@@ -2014,7 +2014,7 @@ describe('ChannelList', () => {
20142014

20152015
await waitFor(() => {
20162016
expect(chatClient.queryChannels).toHaveBeenCalledTimes(3);
2017-
expect(chatClient.queryChannels.mock.calls[2][2]).toStrictEqual(
2017+
expect(vi.mocked(chatClient.queryChannels).mock.calls[2][2]).toStrictEqual(
20182018
expect.objectContaining({ offset: 0 }),
20192019
);
20202020
});

β€Žsrc/components/ChannelListItem/__tests__/ChannelListItemUI.test.tsxβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useMockedApis,
1111
} from 'mock-builders';
1212

13+
import type { Channel, StreamChat } from 'stream-chat';
1314
import { ChannelListItemUI } from '../ChannelListItemUI';
1415
import { ChatProvider, ComponentProvider, DialogManagerProvider } from '../../../context';
1516

@@ -23,8 +24,8 @@ NoopActionButtons.displayName = 'ChannelListItemActionButtons';
2324
describe('ChannelPreviewMessenger', () => {
2425
const clientUser = generateUser();
2526

26-
let chatClient;
27-
let channel;
27+
let chatClient: StreamChat;
28+
let channel: Channel;
2829
const renderComponent = (props?: any, componentOverrides = {}) => (
2930
<ChatProvider value={mockChatContext({ client: chatClient })}>
3031
<DialogManagerProvider>

β€Žsrc/components/ChannelListItem/__tests__/utils.test.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
useMockedApis,
1414
} from 'mock-builders';
1515

16+
import type { StreamChat } from 'stream-chat';
1617
import {
1718
getChannelDisplayImage,
1819
getGroupChannelDisplayInfo,
@@ -23,7 +24,7 @@ import { render } from '@testing-library/react';
2324

2425
describe('ChannelPreview utils', () => {
2526
const clientUser = generateUser();
26-
let chatClient;
27+
let chatClient: StreamChat;
2728
const getQueriedChannelInstance = async (c) => {
2829
// eslint-disable-next-line react-hooks/rules-of-hooks
2930
useMockedApis(chatClient, [getOrCreateChannelApi(c)]);

β€Žsrc/components/Message/__tests__/MessageUI.test.tsxβ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from '../../../mock-builders';
3737
import { MessageBouncePrompt } from '../../MessageBounce';
3838
import { generateReminderResponse } from '../../../mock-builders/generator/reminder';
39-
import type { ChannelConfigWithInfo } from 'stream-chat';
39+
import type { Channel, ChannelConfigWithInfo, StreamChat } from 'stream-chat';
4040
import type { ComponentContextValue, MessageContextValue } from '../../../context';
4141

4242
vi.mock('../../ChatView', async (importOriginal) => {
@@ -94,8 +94,8 @@ function generateBobMessage(
9494
}
9595

9696
describe('<MessageSimple />', () => {
97-
let channel;
98-
let client;
97+
let channel: Channel;
98+
let client: StreamChat;
9999

100100
async function renderMessageSimple({
101101
channelCapabilities = { 'send-reaction': true, 'send-reply': true },

β€Žsrc/components/Message/__tests__/utils.test.tsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { generateMessage, generateReaction, generateUser } from 'mock-builders';
22
import { fromPartial } from '@total-typescript/shoehorn';
33
import type { ChannelConfigWithInfo, Mute } from 'stream-chat';
4+
import type { StreamChat } from 'stream-chat';
45
import {
56
countReactions,
67
getTestClientWithUser,
@@ -442,7 +443,7 @@ describe('Message utils', () => {
442443
describe('getReadByTooltipText', () => {
443444
const tooltipUserNameMapper = mapToUserNameOrId;
444445

445-
let client;
446+
let client: StreamChat;
446447

447448
beforeAll(async () => {
448449
client = await getTestClientWithUser(alice);

β€Žsrc/components/Message/hooks/__tests__/useDeleteHandler.test.tsxβ€Ž

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { renderHook } from '@testing-library/react';
3+
import type { Channel as ChannelType, StreamChat } from 'stream-chat';
34

45
import { useDeleteHandler } from '../useDeleteHandler';
56
import {
@@ -18,8 +19,8 @@ import { Channel } from '../../../Channel';
1819
import { Chat } from '../../../Chat';
1920
import { act } from '@testing-library/react';
2021

21-
let channel;
22-
let client;
22+
let channel: ChannelType;
23+
let client: StreamChat;
2324
const testMessage = generateMessage();
2425
const deleteMessage = vi.fn(() => Promise.resolve(testMessage)) as any;
2526
const removeMessage = vi.fn();

β€Žsrc/components/MessageList/__tests__/ConnectionStatus.test.tsxβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import React from 'react';
22
import { act, cleanup, render, screen, waitFor } from '@testing-library/react';
3+
import type { StreamChat } from 'stream-chat';
34
import { dispatchConnectionChangedEvent, getTestClient } from 'mock-builders';
45

56
import { ConnectionStatus } from '../ConnectionStatus';
67
import { Chat } from '../../Chat';
78

89
const customNotificationId = 'custom-notification';
910
describe('<ChatContext /> component', () => {
10-
let chatClient;
11+
let chatClient: StreamChat;
1112
beforeEach(() => {
1213
chatClient = getTestClient();
1314
});

β€Žsrc/components/MessageList/__tests__/MessageList.test.tsxβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const createDeferred = () => {
9696
};
9797

9898
describe('MessageList', () => {
99-
let chatClient;
99+
let chatClient: StreamChat;
100100
let channel;
101101
let markReadMock;
102102

β€Žsrc/components/MessageList/__tests__/ScrollToLatestMessageButton.test.tsxβ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
3+
import type { Channel, StreamChat, UserResponse } from 'stream-chat';
34

45
import { ScrollToLatestMessageButton } from '../ScrollToLatestMessageButton';
56
import { ChannelStateProvider, ChatProvider } from '../../../context';
@@ -17,10 +18,10 @@ const NEW_MESSAGE_COUNTER_TEST_ID = 'unread-message-notification-counter';
1718
const mainList = 'the main message list';
1819
const threadList = 'a thread';
1920

20-
let client;
21-
let channel;
22-
let users;
23-
let containerIsThread;
21+
let client: StreamChat;
22+
let channel: Channel;
23+
let users: UserResponse[];
24+
let containerIsThread: boolean;
2425
let anotherUser;
2526
let channelStateContext;
2627
let parentMsg;

0 commit comments

Comments
Β (0)