Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,68 @@
import { render } from '@testing-library/react';
import React from 'react';
import BrainLeftSidebarWaveDropTime from '@/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime';
import { getTimeAgoShort } from '@/helpers/Helpers';

jest.mock('@/helpers/Helpers');

describe('BrainLeftSidebarWaveDropTime', () => {
it('renders time using helper and sets interval', () => {
(getTimeAgoShort as jest.Mock).mockReturnValue('1m');
const setSpy = jest.spyOn(global, 'setInterval');
const clearSpy = jest.spyOn(global, 'clearInterval');
const { unmount, getByText } = render(<BrainLeftSidebarWaveDropTime time={10} />);
expect(getByText('1m')).toBeInTheDocument();
import { act, render } from "@testing-library/react";
import React from "react";
import BrainLeftSidebarWaveDropTime from "@/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime";
import { getTimeAgoShort } from "@/helpers/Helpers";

jest.mock("@/helpers/Helpers");

describe("BrainLeftSidebarWaveDropTime", () => {
const setDocumentVisibilityState = (state: DocumentVisibilityState) => {
Object.defineProperty(document, "visibilityState", {
configurable: true,
value: state,
});
};

beforeEach(() => {
jest.clearAllMocks();
setDocumentVisibilityState("visible");
});

it("renders time using helper and sets interval", () => {
(getTimeAgoShort as jest.Mock).mockReturnValue("1m");
const setSpy = jest.spyOn(global, "setInterval");

Check warning on line 23 in __tests__/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeW_9depOQ5y5TO3&open=AZ1tIeW_9depOQ5y5TO3&pullRequest=2235
const clearSpy = jest.spyOn(global, "clearInterval");

Check warning on line 24 in __tests__/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeW_9depOQ5y5TO4&open=AZ1tIeW_9depOQ5y5TO4&pullRequest=2235
const { unmount, getByText } = render(
<BrainLeftSidebarWaveDropTime time={10} />
);
expect(getByText("1m")).toBeInTheDocument();
expect(setSpy).toHaveBeenCalledWith(expect.any(Function), 60000);
unmount();
expect(clearSpy).toHaveBeenCalled();
});

it("refreshes immediately when the tab becomes visible again or gains focus", () => {
jest.useFakeTimers();
jest.setSystemTime(new Date("2026-04-07T10:00:00.000Z"));
(getTimeAgoShort as jest.Mock).mockImplementation(
(_time: number, now?: number) => `${now}`
);

render(<BrainLeftSidebarWaveDropTime time={10} />);
expect(getTimeAgoShort).toHaveBeenLastCalledWith(
10,
new Date("2026-04-07T10:00:00.000Z").getTime()
);

jest.setSystemTime(new Date("2026-04-07T10:01:00.000Z"));
act(() => {
setDocumentVisibilityState("visible");
document.dispatchEvent(new Event("visibilitychange"));
});
expect(getTimeAgoShort).toHaveBeenLastCalledWith(
10,
new Date("2026-04-07T10:01:00.000Z").getTime()
);

jest.setSystemTime(new Date("2026-04-07T10:02:00.000Z"));
act(() => {
window.dispatchEvent(new Event("focus"));

Check warning on line 59 in __tests__/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `window`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeW_9depOQ5y5TO5&open=AZ1tIeW_9depOQ5y5TO5&pullRequest=2235
});
expect(getTimeAgoShort).toHaveBeenLastCalledWith(
10,
new Date("2026-04-07T10:02:00.000Z").getTime()
);

jest.useRealTimers();
});
Comment thread
simo6529 marked this conversation as resolved.
});
153 changes: 0 additions & 153 deletions __tests__/contexts/wave/MyStreamContext.test.tsx

This file was deleted.

62 changes: 61 additions & 1 deletion __tests__/services/websocket/WebSocketProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@
consoleSpy.mockRestore();
});

it("closes existing connection before creating new one", () => {
it("closes existing connection before creating new one and ignores stale close events", () => {
jest.useFakeTimers();

const wrapper = createWrapper({ url: "ws://test" });
const { result } = renderHook(() => React.useContext(WebSocketContext)!, {
wrapper,
Expand All @@ -702,11 +704,69 @@
result.current.connect("token2");
});

const ws2 = (global.WebSocket as jest.MockedFunction<typeof WebSocket>)

Check warning on line 707 in __tests__/services/websocket/WebSocketProvider.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeb39depOQ5y5TPB&open=AZ1tIeb39depOQ5y5TPB&pullRequest=2235
.mock.results[1]?.value as MockWebSocket;

expect(ws1.close).toHaveBeenCalled();
expect(global.WebSocket).toHaveBeenCalledTimes(2);
expect(global.WebSocket).toHaveBeenLastCalledWith(
"ws://test?token=token2"
);

expect(result.current.status).toBe(WebSocketStatus.CONNECTING);

act(() => {
ws1.triggerClose(1006, "Stale socket close");
});

expect(result.current.status).toBe(WebSocketStatus.CONNECTING);

act(() => {
jest.advanceTimersByTime(5000);
});

expect(global.WebSocket).toHaveBeenCalledTimes(2);

Check warning on line 728 in __tests__/services/websocket/WebSocketProvider.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeb39depOQ5y5TPC&open=AZ1tIeb39depOQ5y5TPC&pullRequest=2235

act(() => {
ws2.triggerOpen();
});

expect(result.current.status).toBe(WebSocketStatus.CONNECTED);
});

it("ignores open events from a replaced socket", () => {
const wrapper = createWrapper({ url: "ws://test" });
const { result } = renderHook(() => React.useContext(WebSocketContext)!, {
wrapper,
});

act(() => {
result.current.connect("token1");
});

const ws1 = (global.WebSocket as jest.MockedFunction<typeof WebSocket>)

Check warning on line 747 in __tests__/services/websocket/WebSocketProvider.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeb39depOQ5y5TPD&open=AZ1tIeb39depOQ5y5TPD&pullRequest=2235
.mock.results[0]?.value as MockWebSocket;

act(() => {
result.current.connect("token2");
});

const ws2 = (global.WebSocket as jest.MockedFunction<typeof WebSocket>)

Check warning on line 754 in __tests__/services/websocket/WebSocketProvider.test.tsx

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefer `globalThis` over `global`.

See more on https://sonarcloud.io/project/issues?id=6529-Collections_6529seize-frontend&issues=AZ1tIeb39depOQ5y5TPE&open=AZ1tIeb39depOQ5y5TPE&pullRequest=2235
.mock.results[1]?.value as MockWebSocket;

expect(result.current.status).toBe(WebSocketStatus.CONNECTING);

act(() => {
ws1.triggerOpen();
});

expect(result.current.status).toBe(WebSocketStatus.CONNECTING);

act(() => {
ws2.triggerOpen();
});

expect(result.current.status).toBe(WebSocketStatus.CONNECTED);
});
});

Expand Down
Loading
Loading