Skip to content

Commit

Permalink
#1866 eslint any
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Dec 16, 2024
1 parent eb15661 commit 7cd24bb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
9 changes: 7 additions & 2 deletions starsky/starsky/clientapp/src/hooks/___tests___/test-hook.tsx
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -22,5 +27,5 @@ export const mountReactHook = (hook: (...args: unknown[]) => unknown, args: unkn
</Component>
);
});
return { componentMount, componentHook };
return { componentMount, componentHook } as MountReactHookResult;
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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";
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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, {
Expand All @@ -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");
Expand All @@ -32,7 +32,7 @@ describe("WsCurrentStart", () => {
jest.fn(),
NewFakeWebSocketService,
"",
jest.fn() as any
jest.fn() as React.Dispatch<React.SetStateAction<string>>
) as FakeWebSocketService;

expect(setSocketConnectedSpy).toHaveBeenCalled();
Expand Down Expand Up @@ -111,7 +111,7 @@ describe("WsCurrentStart", () => {
data: '{"type" : "Welcome", "welcome": true}'
}),
setKeepAliveTimeSpy,
jest.fn() as any
jest.fn() as React.Dispatch<React.SetStateAction<string>>
);
expect(setKeepAliveTimeSpy).toHaveBeenCalled();
});
Expand All @@ -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<React.SetStateAction<string>>
);
expect(setKeepAliveTimeSpy).toHaveBeenCalled();
});
Expand All @@ -131,7 +131,7 @@ describe("WsCurrentStart", () => {
FireOnMessage(
new MessageEvent("t", { data: undefined }),
setKeepAliveTimeSpy,
jest.fn() as any
jest.fn() as React.Dispatch<React.SetStateAction<string>>
);
expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0);
});
Expand All @@ -141,7 +141,7 @@ describe("WsCurrentStart", () => {
FireOnMessage(
new MessageEvent("t", { data: "1{1\\" }),
setKeepAliveTimeSpy,
jest.fn() as any
jest.fn() as React.Dispatch<React.SetStateAction<string>>
);
expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0);
});
Expand All @@ -151,7 +151,7 @@ describe("WsCurrentStart", () => {
FireOnMessage(
new MessageEvent("t", { data: '{"data": 1}' }),
setKeepAliveTimeSpy,
jest.fn() as any
jest.fn() as React.Dispatch<React.SetStateAction<string>>
);
expect(setKeepAliveTimeSpy).toHaveBeenCalledTimes(0);
});
Expand All @@ -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<React.SetStateAction<string>>
);
});
});

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);
});
});
Expand All @@ -179,22 +185,22 @@ 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);
});
it("should trigger when message is valid", () => {
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);
});
Expand Down
29 changes: 19 additions & 10 deletions starsky/starsky/clientapp/src/hooks/realtime/ws-current-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@ 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<IFileIndexItem[]> | { dateTime: string };
}

export function isKeepAliveMessage(item: KeepAliveMessage) {
if (!item?.type) return false;
return item.type === "Welcome" || item.type === "Heartbeat";
}

export function HandleKeepAliveMessage(
setKeepAliveTime: Dispatch<SetStateAction<Date>>,
item: any
item: KeepAliveMessage
) {
if (!isKeepAliveMessage(item)) return;
setKeepAliveTime(new Date());
}

export function HandleKeepAliveServerMessage(
setKeepAliveServerTime: Dispatch<SetStateAction<string>>,
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);
}

Expand All @@ -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) {
Expand Down Expand Up @@ -109,7 +114,9 @@ export async function RestoreDataOnOpen(
continue;
}
const item = parseJson(dataItem.content);
PushMessage(item);
if (item) {
PushMessage(item as unknown as IApiNotificationResponseModel<IFileIndexItem[]>);
}
anyResults = true;
}
console.log(result.data);
Expand All @@ -118,18 +125,20 @@ export async function RestoreDataOnOpen(
}

export function FireOnMessage(
e: Event,
e: MessageEvent,
setKeepAliveTime: Dispatch<SetStateAction<Date>>,
setKeepAliveServerTime: Dispatch<SetStateAction<string>>
) {
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<IFileIndexItem[]>);
}
}

export default function WsCurrentStart(
Expand Down

0 comments on commit 7cd24bb

Please sign in to comment.