From 7cd24bb80c7f7728a8ca4dcb61a88a0ea1acec52 Mon Sep 17 00:00:00 2001 From: Dion Date: Mon, 16 Dec 2024 21:22:34 +0100 Subject: [PATCH] #1866 eslint any --- .../archive-wrapper.spec.tsx | 2 +- .../src/hooks/___tests___/test-hook.tsx | 9 ++++-- .../src/hooks/realtime/use-sockets.spec.tsx | 10 +++---- .../hooks/realtime/ws-current-start.spec.ts | 30 +++++++++++-------- .../src/hooks/realtime/ws-current-start.ts | 29 +++++++++++------- 5 files changed, 50 insertions(+), 30 deletions(-) diff --git a/starsky/starsky/clientapp/src/contexts-wrappers/archive-wrapper.spec.tsx b/starsky/starsky/clientapp/src/contexts-wrappers/archive-wrapper.spec.tsx index 2799b0c542..24cc2c5007 100644 --- a/starsky/starsky/clientapp/src/contexts-wrappers/archive-wrapper.spec.tsx +++ b/starsky/starsky/clientapp/src/contexts-wrappers/archive-wrapper.spec.tsx @@ -212,7 +212,7 @@ describe("ArchiveContextWrapper", () => { }); it("When outside current directory it should be ignored 2", () => { - const dispatch = (e: any) => { + const dispatch = (e: { add: IFileIndexItem[]; type: string }) => { // should ignore the first one expect(e).toStrictEqual({ add: [], diff --git a/starsky/starsky/clientapp/src/hooks/___tests___/test-hook.tsx b/starsky/starsky/clientapp/src/hooks/___tests___/test-hook.tsx index fde6960b4b..68d660afc4 100644 --- a/starsky/starsky/clientapp/src/hooks/___tests___/test-hook.tsx +++ b/starsky/starsky/clientapp/src/hooks/___tests___/test-hook.tsx @@ -1,10 +1,15 @@ -import { render } from "@testing-library/react"; +import { render, RenderResult } from "@testing-library/react"; import { act } from "react"; type ModalPropTypes = { children: (hookValues: unknown) => React.ReactNode; }; +export type MountReactHookResult = { + componentMount: RenderResult; + componentHook: object; +}; + export const mountReactHook = (hook: (...args: unknown[]) => unknown, args: unknown[]) => { const Component = ({ children }: ModalPropTypes) => { return children(hook(...args)); @@ -22,5 +27,5 @@ export const mountReactHook = (hook: (...args: unknown[]) => unknown, args: unkn ); }); - return { componentMount, componentHook }; + return { componentMount, componentHook } as MountReactHookResult; }; diff --git a/starsky/starsky/clientapp/src/hooks/realtime/use-sockets.spec.tsx b/starsky/starsky/clientapp/src/hooks/realtime/use-sockets.spec.tsx index 2fd9ba16d7..875036061e 100644 --- a/starsky/starsky/clientapp/src/hooks/realtime/use-sockets.spec.tsx +++ b/starsky/starsky/clientapp/src/hooks/realtime/use-sockets.spec.tsx @@ -1,6 +1,6 @@ -import { act } from "@testing-library/react"; +import { act, RenderResult } from "@testing-library/react"; import * as DifferenceInDate from "../../shared/date"; -import { mountReactHook } from "../___tests___/test-hook"; +import { mountReactHook, MountReactHookResult } from "../___tests___/test-hook"; import * as useInterval from "../use-interval"; import { FakeWebSocketService } from "./___tests___/fake-web-socket-service"; import useSockets, { IUseSockets } from "./use-sockets"; @@ -8,12 +8,12 @@ import WebSocketService from "./websocket-service"; import * as WsCurrentStart from "./ws-current-start"; describe("useSockets", () => { - let setupComponent: any; + let setupComponent: MountReactHookResult; let hook: IUseSockets; - let component: any; + let component: RenderResult; function mountComponent() { - setupComponent = mountReactHook(useSockets, []); // Mount a Component with our hook + setupComponent = mountReactHook(useSockets, []) as unknown as MountReactHookResult; // Mount a Component with our hook hook = setupComponent.componentHook as IUseSockets; component = setupComponent.componentMount; } diff --git a/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.spec.ts b/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.spec.ts index 9e584c9b10..ca3cebfbef 100644 --- a/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.spec.ts +++ b/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.spec.ts @@ -1,5 +1,6 @@ import { IConnectionDefault } from "../../interfaces/IConnectionDefault"; import * as FetchGet from "../../shared/fetch/fetch-get"; +import { FakeWebSocketService } from "./___tests___/fake-web-socket-service"; import { useSocketsEventName } from "./use-sockets.const"; import WebSocketService from "./websocket-service"; import WsCurrentStart, { @@ -13,7 +14,6 @@ import WsCurrentStart, { parseJson, RestoreDataOnOpen } from "./ws-current-start"; -import { FakeWebSocketService } from "./___tests___/fake-web-socket-service"; describe("WsCurrentStart", () => { const onOpenEvent = new Event("t"); @@ -32,7 +32,7 @@ describe("WsCurrentStart", () => { jest.fn(), NewFakeWebSocketService, "", - jest.fn() as any + jest.fn() as React.Dispatch> ) as FakeWebSocketService; expect(setSocketConnectedSpy).toHaveBeenCalled(); @@ -111,7 +111,7 @@ describe("WsCurrentStart", () => { data: '{"type" : "Welcome", "welcome": true}' }), setKeepAliveTimeSpy, - jest.fn() as any + jest.fn() as React.Dispatch> ); expect(setKeepAliveTimeSpy).toHaveBeenCalled(); }); @@ -121,7 +121,7 @@ describe("WsCurrentStart", () => { FireOnMessage( new MessageEvent("t", { data: '{"type" : "Welcome", "time": 1}' }), setKeepAliveTimeSpy, - jest.fn() as any + jest.fn() as React.Dispatch> ); expect(setKeepAliveTimeSpy).toHaveBeenCalled(); }); @@ -131,7 +131,7 @@ describe("WsCurrentStart", () => { FireOnMessage( new MessageEvent("t", { data: undefined }), setKeepAliveTimeSpy, - jest.fn() as any + jest.fn() as React.Dispatch> ); expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0); }); @@ -141,7 +141,7 @@ describe("WsCurrentStart", () => { FireOnMessage( new MessageEvent("t", { data: "1{1\\" }), setKeepAliveTimeSpy, - jest.fn() as any + jest.fn() as React.Dispatch> ); expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0); }); @@ -151,7 +151,7 @@ describe("WsCurrentStart", () => { FireOnMessage( new MessageEvent("t", { data: '{"data": 1}' }), setKeepAliveTimeSpy, - jest.fn() as any + jest.fn() as React.Dispatch> ); expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0); }); @@ -163,14 +163,20 @@ describe("WsCurrentStart", () => { done(); }); - FireOnMessage(new MessageEvent("t", { data: '{"data": 1}' }), jest.fn(), jest.fn() as any); + FireOnMessage( + new MessageEvent("t", { data: '{"data": 1}' }), + jest.fn(), + jest.fn() as React.Dispatch> + ); }); }); describe("HandleKeepAliveMessage", () => { it("should ignore keep alive when sending real message", () => { const setKeepAliveTimeSpy = jest.fn(); - HandleKeepAliveMessage(setKeepAliveTimeSpy, { data: '{"data": 1}' }); + HandleKeepAliveMessage(setKeepAliveTimeSpy, { + data: '{"data": 1}' as unknown as { dateTime: string } + }); expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0); }); }); @@ -179,7 +185,7 @@ describe("WsCurrentStart", () => { it("should ignore keep alive when sending real message", () => { const setKeepAliveServerTimeSpy = jest.fn(); HandleKeepAliveServerMessage(setKeepAliveServerTimeSpy, { - data: '{"data": 1}' + data: '{"data": 1}' as unknown as { dateTime: string } }); expect(setKeepAliveServerTimeSpy).toHaveBeenCalledTimes(0); }); @@ -187,14 +193,14 @@ describe("WsCurrentStart", () => { const setKeepAliveServerTimeSpy = jest.fn(); HandleKeepAliveServerMessage(setKeepAliveServerTimeSpy, { type: "Welcome", - data: { dateTime: 1 } + data: { dateTime: 1 } as unknown as { dateTime: string } }); expect(setKeepAliveServerTimeSpy).toHaveBeenCalledTimes(1); }); it("should trigger when message has no welcome", () => { const setKeepAliveServerTimeSpy = jest.fn(); HandleKeepAliveServerMessage(setKeepAliveServerTimeSpy, { - data: { dateTime: 1 } + data: { dateTime: 1 } as unknown as { dateTime: string } }); // should have type expect(setKeepAliveServerTimeSpy).toHaveBeenCalledTimes(0); }); diff --git a/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.ts b/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.ts index 23e8fcc983..8257b2125b 100644 --- a/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.ts +++ b/starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.ts @@ -6,14 +6,19 @@ import { UrlQuery } from "../../shared/url/url-query"; import { useSocketsEventName } from "./use-sockets.const"; import WebSocketService from "./websocket-service"; -export function isKeepAliveMessage(item: any) { +export interface KeepAliveMessage { + type?: string; + data?: IApiNotificationResponseModel | { dateTime: string }; +} + +export function isKeepAliveMessage(item: KeepAliveMessage) { if (!item?.type) return false; return item.type === "Welcome" || item.type === "Heartbeat"; } export function HandleKeepAliveMessage( setKeepAliveTime: Dispatch>, - item: any + item: KeepAliveMessage ) { if (!isKeepAliveMessage(item)) return; setKeepAliveTime(new Date()); @@ -21,9 +26,9 @@ export function HandleKeepAliveMessage( export function HandleKeepAliveServerMessage( setKeepAliveServerTime: Dispatch>, - item: any + item: KeepAliveMessage ) { - if (!isKeepAliveMessage(item) || !item.data?.dateTime) return; + if (!isKeepAliveMessage(item) || !item.data || !("dateTime" in item.data)) return; setKeepAliveServerTime(item.data.dateTime); } @@ -36,7 +41,7 @@ export const NewWebSocketService = (): WebSocketService => { * @param data * @returns */ -export function parseJson(data: string): any { +export function parseJson(data: string): KeepAliveMessage | null { try { return JSON.parse(data); } catch (error) { @@ -109,7 +114,9 @@ export async function RestoreDataOnOpen( continue; } const item = parseJson(dataItem.content); - PushMessage(item); + if (item) { + PushMessage(item as unknown as IApiNotificationResponseModel); + } anyResults = true; } console.log(result.data); @@ -118,18 +125,20 @@ export async function RestoreDataOnOpen( } export function FireOnMessage( - e: Event, + e: MessageEvent, setKeepAliveTime: Dispatch>, setKeepAliveServerTime: Dispatch> ) { - const item = parseJson((e as any).data); + const item = parseJson(e.data); - if (isKeepAliveMessage(item)) { + if (item && isKeepAliveMessage(item)) { HandleKeepAliveMessage(setKeepAliveTime, item); HandleKeepAliveServerMessage(setKeepAliveServerTime, item); return; } - PushMessage(item); + if (item) { + PushMessage(item as unknown as IApiNotificationResponseModel); + } } export default function WsCurrentStart(