Skip to content

Commit b3fa7a3

Browse files
committed
refactor(tests): simplify over-typed casts
Focused pass to reduce redundant casts across the test suite. Cuts \`as unknown as\` occurrences from 359 → 283 (-76, ~21%) without changing runtime behavior. **Highlights:** - \`channelMocks.tsx\`: introduce local \`mockMessage\` / \`mockUser\` helpers so 17 per-literal \`as unknown as MessageResponse\` / \`as unknown as UserResponse\` casts collapse into one internal narrow cast inside each helper. - \`useMessageListPagination.test.tsx\` / \`Channel.test.tsx\` / \`MessageList.test.tsx\`: drop many \`as unknown as typeof channel.state.messages\` casts on \`generateMessage()\` outputs — the generator now returns \`LocalMessage\`, which already matches. - \`Channel.test.tsx\`: tighten \`ChannelContext as unknown as React.Context<unknown>\` → \`as React.Context<unknown>\` (React's \`Context<T>\` is invariant, so a full cast isn't needed, just the single \`as\`). - Dropped \`{} as unknown as X\` → \`{} as X\` for empty provider values where the double-cast was overkill. - Dropped the \`client={chatClient}\` dead prop still lingering on one of the two \`<Channel>\` renders in \`Thread.test.tsx\` (missed in the earlier dead-prop sweep). - Simplified \`as unknown as X\` to \`as X\` at ~10 miscellaneous sites (e.g. \`Parameters<T>\`, \`jest.Mocked<T>\`, context-prop indexers). **Intentionally kept:** - \`_fiber\` internal-access casts in offline-support helpers. - Private-member access casts (\`syncManager\`, \`_sendMessage\`, \`_setToken\`, etc.). - \`Streami18n\` \`ConstructorParameters\` double-casts where test fixtures violate the stricter option types. - \`channel.state = {...} as unknown as typeof channel.state\` where a partial spread genuinely doesn't satisfy the full ChannelState class instance shape. test:typecheck: 0 errors. Full test suite: same pre-existing SQLite-isolation flake; no regressions.
1 parent 7642d24 commit b3fa7a3

13 files changed

Lines changed: 101 additions & 111 deletions

File tree

package/src/__tests__/offline-support/offline-feature.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export const Generic = () => {
254254
const filters = {
255255
foo: 'bar',
256256
type: 'messaging',
257-
} as unknown as ChannelFilters;
257+
} as ChannelFilters;
258258
const sort: ChannelSort = { last_updated: 1 };
259259

260260
const renderComponent = () =>

package/src/components/Channel/__tests__/Channel.test.tsx

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ type RenderComponentProps = Partial<Omit<ComponentProps<typeof Channel>, 'channe
8181
const renderComponent = (
8282
props: RenderComponentProps = {},
8383
callback: (ctx: unknown) => void = () => {},
84-
context: React.Context<unknown> = ChannelContext as unknown as React.Context<unknown>,
84+
context: React.Context<unknown> = ChannelContext as React.Context<unknown>,
8585
) =>
8686
render(
8787
<ChannelsStateProvider>
@@ -194,7 +194,7 @@ describe('Channel', () => {
194194
hasThread(thread.id);
195195
}
196196
},
197-
ThreadContext as unknown as React.Context<unknown>,
197+
ThreadContext as React.Context<unknown>,
198198
);
199199

200200
rerender(
@@ -213,7 +213,7 @@ describe('Channel', () => {
213213
hasThread(thread.id);
214214
}
215215
}}
216-
context={ThreadContext as unknown as React.Context<unknown>}
216+
context={ThreadContext as React.Context<unknown>}
217217
/>
218218
</Channel>
219219
</Chat>
@@ -230,7 +230,7 @@ describe('Channel', () => {
230230
config: channel.getConfig(),
231231
id: channel.id,
232232
type: channel.type,
233-
} as unknown as NonNullable<Parameters<typeof generateChannelResponse>[0]>['channel'],
233+
},
234234
messages: newMessages,
235235
}),
236236
);
@@ -245,7 +245,7 @@ describe('Channel', () => {
245245
() => {
246246
useMockedApis(chatClient, [queryChannelWithNewMessages(newMessages)]);
247247
},
248-
MessagesContext as unknown as React.Context<unknown>,
248+
MessagesContext as React.Context<unknown>,
249249
);
250250

251251
await waitFor(() => expect(channelQuerySpy).toHaveBeenCalled());
@@ -254,7 +254,7 @@ describe('Channel', () => {
254254
describe('ChannelContext', () => {
255255
it('renders children without crashing', async () => {
256256
const { getByTestId } = render(
257-
<ChannelProvider value={{} as unknown as ChannelContextValue}>
257+
<ChannelProvider value={{} as ChannelContextValue}>
258258
<View testID='children' />
259259
</ChannelProvider>,
260260
);
@@ -275,7 +275,7 @@ describe('Channel', () => {
275275
render(
276276
<ChannelProvider value={mockContext as unknown as ChannelContextValue}>
277277
<ContextConsumer
278-
context={ChannelContext as unknown as React.Context<unknown>}
278+
context={ChannelContext as React.Context<unknown>}
279279
fn={(ctx) => {
280280
context = ctx as ChannelContextValue;
281281
}}
@@ -297,7 +297,7 @@ describe('Channel', () => {
297297
describe('MessagesContext', () => {
298298
it('renders children without crashing', async () => {
299299
const { getByTestId } = render(
300-
<MessagesProvider value={{} as unknown as MessagesContextValue}>
300+
<MessagesProvider value={{} as MessagesContextValue}>
301301
<View testID='children' />
302302
</MessagesProvider>,
303303
);
@@ -318,7 +318,7 @@ describe('Channel', () => {
318318
render(
319319
<MessagesProvider value={mockContext as unknown as MessagesContextValue}>
320320
<ContextConsumer
321-
context={MessagesContext as unknown as React.Context<unknown>}
321+
context={MessagesContext as React.Context<unknown>}
322322
fn={(ctx) => {
323323
context = ctx as MessagesContextValue;
324324
}}
@@ -340,7 +340,7 @@ describe('Channel', () => {
340340
describe('ThreadContext', () => {
341341
it('renders children without crashing', async () => {
342342
const { getByTestId } = render(
343-
<ThreadProvider value={{} as unknown as ThreadContextValue}>
343+
<ThreadProvider value={{} as ThreadContextValue}>
344344
<View testID='children' />
345345
</ThreadProvider>,
346346
);
@@ -361,7 +361,7 @@ describe('Channel', () => {
361361
render(
362362
<ThreadProvider value={mockContext as unknown as ThreadContextValue}>
363363
<ContextConsumer
364-
context={ThreadContext as unknown as React.Context<unknown>}
364+
context={ThreadContext as React.Context<unknown>}
365365
fn={(ctx) => {
366366
context = ctx as ThreadContextValue;
367367
}}
@@ -436,12 +436,7 @@ describe('Channel initial load useEffect', () => {
436436
channel.state = {
437437
...channelInitialState,
438438
members: Object.fromEntries(
439-
Array.from({ length: 10 }, (_, i) => [
440-
i,
441-
generateMember({ user_id: String(i) } as unknown as Partial<
442-
Parameters<typeof generateMember>[0]
443-
>),
444-
]),
439+
Array.from({ length: 10 }, (_, i) => [i, generateMember({ user_id: String(i) })]),
445440
),
446441
messagePagination: {
447442
hasPrev: true,
@@ -488,12 +483,8 @@ describe('Channel initial load useEffect', () => {
488483
await channel.watch();
489484

490485
const loadMessageIntoState = jest.fn(() => {
491-
const newMessages = getElementsAround(
492-
messages as unknown as typeof channel.state.messages,
493-
'id',
494-
messageToSearch.id,
495-
);
496-
channel.state.messages = newMessages as unknown as typeof channel.state.messages;
486+
const newMessages = getElementsAround(messages, 'id', messageToSearch.id);
487+
channel.state.messages = newMessages;
497488
});
498489

499490
channel.state = {

package/src/components/Channel/__tests__/useMessageListPagination.test.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe('useMessageListPagination', () => {
6363
const loadMessageIntoState = jest.fn(() => {
6464
channel.state.messages = Array.from({ length: 20 }, (_, i) =>
6565
generateMessage({ text: `message-${i}` }),
66-
) as unknown as typeof channel.state.messages;
66+
);
6767
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
6868
});
6969
channel.state = {
@@ -104,7 +104,7 @@ describe('useMessageListPagination', () => {
104104
hasPrev: false,
105105
},
106106
} as unknown as typeof channel.state;
107-
channel.query = queryFn as unknown as typeof channel.query;
107+
channel.query = queryFn as typeof channel.query;
108108
const { result } = renderHook(() => useMessageListPagination({ channel }));
109109

110110
await act(async () => {
@@ -125,7 +125,7 @@ describe('useMessageListPagination', () => {
125125
hasPrev: true,
126126
},
127127
} as unknown as typeof channel.state;
128-
channel.query = queryFn as unknown as typeof channel.query;
128+
channel.query = queryFn as typeof channel.query;
129129

130130
mockedHook({ loadingMore: true, loadingMoreRecent: true });
131131

@@ -147,7 +147,7 @@ describe('useMessageListPagination', () => {
147147
const queryFn = jest.fn(() => {
148148
channel.state.messages = Array.from({ length: 40 }, (_, i) =>
149149
generateMessage({ text: `message-${i}` }),
150-
) as unknown as typeof channel.state.messages;
150+
);
151151
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
152152
});
153153
channel.state = {
@@ -197,7 +197,7 @@ describe('useMessageListPagination', () => {
197197
hasPrev: true,
198198
},
199199
} as unknown as typeof channel.state;
200-
channel.query = queryFn as unknown as typeof channel.query;
200+
channel.query = queryFn as typeof channel.query;
201201
const { result } = renderHook(() => useMessageListPagination({ channel }));
202202

203203
await act(async () => {
@@ -218,7 +218,7 @@ describe('useMessageListPagination', () => {
218218
hasPrev: true,
219219
},
220220
} as unknown as typeof channel.state;
221-
channel.query = queryFn as unknown as typeof channel.query;
221+
channel.query = queryFn as typeof channel.query;
222222

223223
mockedHook({ loadingMore: true, loadingMoreRecent: true });
224224

@@ -240,7 +240,7 @@ describe('useMessageListPagination', () => {
240240
const queryFn = jest.fn(() => {
241241
channel.state.messages = Array.from({ length: 40 }, (_, i) =>
242242
generateMessage({ text: `message-${i}` }),
243-
) as unknown as typeof channel.state.messages;
243+
);
244244
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
245245
});
246246
channel.state = {
@@ -283,7 +283,7 @@ describe('useMessageListPagination', () => {
283283
const loadMessageIntoState = jest.fn(() => {
284284
channel.state.messages = Array.from({ length: 20 }, (_, i) =>
285285
generateMessage({ text: `message-${i}` }),
286-
) as unknown as typeof channel.state.messages;
286+
);
287287
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
288288
});
289289
channel.state = {
@@ -309,7 +309,7 @@ describe('useMessageListPagination', () => {
309309
const loadMessageIntoState = jest.fn(() => {
310310
channel.state.messages = Array.from({ length: 20 }, (_, i) =>
311311
generateMessage({ text: `message-${i}` }),
312-
) as unknown as typeof channel.state.messages;
312+
);
313313
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
314314
});
315315
channel.state = {
@@ -350,7 +350,7 @@ describe('useMessageListPagination', () => {
350350
generateMessage({ text: `message-${i}` }),
351351
);
352352
const loadMessageIntoState = jest.fn(() => {
353-
channel.state.messages = messages as unknown as typeof channel.state.messages;
353+
channel.state.messages = messages;
354354
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
355355
});
356356
channel.state = {
@@ -446,7 +446,7 @@ describe('useMessageListPagination', () => {
446446
const newMessages = Array.from({ length: 20 }, (_, i) =>
447447
generateMessage({ id: String(i + 21), text: `message-${i + 21}` }),
448448
);
449-
channel.state.messages = newMessages as unknown as typeof channel.state.messages;
449+
channel.state.messages = newMessages;
450450
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
451451
});
452452
(channel.state as unknown as { loadMessageIntoState: jest.Mock }).loadMessageIntoState =
@@ -489,7 +489,7 @@ describe('useMessageListPagination', () => {
489489
const newMessages = Array.from({ length: 20 }, (_, i) =>
490490
generateMessage({ id: String(i + 21), text: `message-${i + 21}` }),
491491
);
492-
channel.state.messages = newMessages as unknown as typeof channel.state.messages;
492+
channel.state.messages = newMessages;
493493
(channel.state.messagePagination as { hasPrev: boolean }).hasPrev = true;
494494
});
495495
(channel.state as unknown as { loadMessageIntoState: jest.Mock }).loadMessageIntoState =
@@ -581,9 +581,9 @@ describe('useMessageListPagination', () => {
581581
const user = generateUser();
582582

583583
it.each`
584-
scenario | last_read | expectedQueryCalls | expectedJumpToMessageFinishedCalls | expectedSetChannelUnreadStateCalls | expectedSetTargetedMessageCalls | expectedTargetedMessageId
585-
${'when last_read matches a message'} | ${new Date(messages[10].created_at as unknown as Date)} | ${0} | ${1} | ${1} | ${1} | ${'10'}
586-
${'when last_read does not match any message'} | ${new Date('2021-09-02T00:00:00.000Z')} | ${1} | ${0} | ${0} | ${0} | ${undefined}
584+
scenario | last_read | expectedQueryCalls | expectedJumpToMessageFinishedCalls | expectedSetChannelUnreadStateCalls | expectedSetTargetedMessageCalls | expectedTargetedMessageId
585+
${'when last_read matches a message'} | ${new Date(messages[10].created_at)} | ${0} | ${1} | ${1} | ${1} | ${'10'}
586+
${'when last_read does not match any message'} | ${new Date('2021-09-02T00:00:00.000Z')} | ${1} | ${0} | ${0} | ${0} | ${undefined}
587587
`(
588588
'$scenario',
589589
async ({

package/src/components/ChannelList/__tests__/ChannelList.test.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const mockChannelSwipableWrapper = jest.fn(({ children }: { children: React.Reac
4343
));
4444

4545
jest.mock('../../ChannelPreview/ChannelSwipableWrapper', () => ({
46-
ChannelSwipableWrapper: (...args: unknown[]) =>
47-
(mockChannelSwipableWrapper as unknown as (...a: unknown[]) => React.ReactElement)(...args),
46+
ChannelSwipableWrapper: (...args: Parameters<typeof mockChannelSwipableWrapper>) =>
47+
mockChannelSwipableWrapper(...args),
4848
}));
4949

5050
/**
@@ -173,11 +173,7 @@ describe('ChannelList', () => {
173173
<WithComponents overrides={{ ChannelPreview: ChannelPreviewComponent }}>
174174
<ChannelList
175175
{...props}
176-
filters={
177-
{ dummyFilter: true } as unknown as React.ComponentProps<
178-
typeof ChannelList
179-
>['filters']
180-
}
176+
filters={{ dummyFilter: true } as React.ComponentProps<typeof ChannelList>['filters']}
181177
/>
182178
</WithComponents>
183179
</Chat>,
@@ -220,14 +216,14 @@ describe('ChannelList', () => {
220216
return deferredCallForFreshFilter.promise;
221217
}
222218
return deferredCallForStaleFilter.promise;
223-
}) as unknown as typeof chatClient.queryChannels);
219+
}) as typeof chatClient.queryChannels);
224220

225221
const { rerender, queryByTestId } = render(
226222
<Chat client={chatClient}>
227223
<WithComponents overrides={{ ChannelPreview: ChannelPreviewComponent }}>
228224
<ChannelList
229225
{...props}
230-
filters={staleFilter as unknown as React.ComponentProps<typeof ChannelList>['filters']}
226+
filters={staleFilter as React.ComponentProps<typeof ChannelList>['filters']}
231227
/>
232228
</WithComponents>
233229
</Chat>,
@@ -250,7 +246,7 @@ describe('ChannelList', () => {
250246
<WithComponents overrides={{ ChannelPreview: ChannelPreviewComponent }}>
251247
<ChannelList
252248
{...props}
253-
filters={freshFilter as unknown as React.ComponentProps<typeof ChannelList>['filters']}
249+
filters={freshFilter as React.ComponentProps<typeof ChannelList>['filters']}
254250
/>
255251
</WithComponents>
256252
</Chat>,
@@ -922,7 +918,7 @@ describe('ChannelList', () => {
922918

923919
chatClient.queryChannels = jest.fn(
924920
() => deferredPromise.promise,
925-
) as unknown as typeof chatClient.queryChannels;
921+
) as typeof chatClient.queryChannels;
926922

927923
act(() => dispatchConnectionChangedEvent(chatClient, false));
928924
act(() => dispatchConnectionChangedEvent(chatClient, true));

package/src/components/ChannelPreview/__tests__/ChannelPreview.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const initChannelFromData = async (
6868
channel.initialized = true;
6969
channel.lastMessage = jest.fn().mockReturnValue(generateMessage());
7070
channel.muteStatus = jest.fn().mockReturnValue({ muted: false });
71-
channel.state.messages = [generateMessage()] as unknown as typeof channel.state.messages;
71+
channel.state.messages = [generateMessage()];
7272

7373
return channel;
7474
};

package/src/components/Chat/__tests__/Chat.test.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ describe('TranslationContext', () => {
168168
const i18nInstance = new Streami18n();
169169
const { t, tDateTimeParser } = await i18nInstance.getTranslators();
170170

171-
i18nInstance.t = (() => 't') as unknown as typeof i18nInstance.t;
172-
i18nInstance.tDateTimeParser = (() =>
173-
'tDateTimeParser') as unknown as typeof i18nInstance.tDateTimeParser;
171+
i18nInstance.t = (() => 't') as typeof i18nInstance.t;
172+
i18nInstance.tDateTimeParser = (() => 'tDateTimeParser') as typeof i18nInstance.tDateTimeParser;
174173

175174
render(
176175
<Chat client={chatClient} i18nInstance={i18nInstance}>
@@ -194,9 +193,8 @@ describe('TranslationContext', () => {
194193
let context: TranslationContextValue = {} as TranslationContextValue;
195194
const i18nInstance = new Streami18n();
196195

197-
i18nInstance.t = (() => 't') as unknown as typeof i18nInstance.t;
198-
i18nInstance.tDateTimeParser = (() =>
199-
'tDateTimeParser') as unknown as typeof i18nInstance.tDateTimeParser;
196+
i18nInstance.t = (() => 't') as typeof i18nInstance.t;
197+
i18nInstance.tDateTimeParser = (() => 'tDateTimeParser') as typeof i18nInstance.tDateTimeParser;
200198

201199
const { rerender } = render(
202200
<Chat client={chatClient} i18nInstance={i18nInstance}>
@@ -215,9 +213,9 @@ describe('TranslationContext', () => {
215213

216214
const newI18nInstance = new Streami18n();
217215

218-
newI18nInstance.t = (() => 'newT') as unknown as typeof newI18nInstance.t;
216+
newI18nInstance.t = (() => 'newT') as typeof newI18nInstance.t;
219217
newI18nInstance.tDateTimeParser = (() =>
220-
'newtDateTimeParser') as unknown as typeof newI18nInstance.tDateTimeParser;
218+
'newtDateTimeParser') as typeof newI18nInstance.tDateTimeParser;
221219

222220
rerender(
223221
<Chat client={chatClient} i18nInstance={newI18nInstance}>

package/src/components/MessageList/__tests__/MessageList.test.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,12 +590,7 @@ describe('MessageList pagination', () => {
590590
...channelInitialState,
591591
latestMessages: [],
592592
members: Object.fromEntries(
593-
Array.from({ length: 10 }, (_, i) => [
594-
i,
595-
generateMember({ user_id: String(i) } as unknown as Partial<
596-
Parameters<typeof generateMember>[0]
597-
>),
598-
]),
593+
Array.from({ length: 10 }, (_, i) => [i, generateMember({ user_id: String(i) })]),
599594
),
600595
messages: Array.from({ length: 10 }, (_, i) => generateMessage({ id: String(i) })),
601596
messageSets: [{ isCurrent: true, isLatest: true }],

0 commit comments

Comments
 (0)