-
Notifications
You must be signed in to change notification settings - Fork 5
Update on tab change #2235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update on tab change #2235
Changes from 6 commits
a6fca66
2af464a
c329cc5
c8bf377
01c2104
f201afa
ca9f1a0
da11c6b
dc3ea28
4f6cbbf
4ef5d44
52dc866
fe7c467
64407ef
c2ef1da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||
| const clearSpy = jest.spyOn(global, "clearInterval"); | ||
|
Check warning on line 24 in __tests__/components/brain/left-sidebar/waves/BrainLeftSidebarWaveDropTime.test.tsx
|
||
| 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
|
||
| }); | ||
| expect(getTimeAgoShort).toHaveBeenLastCalledWith( | ||
| 10, | ||
| new Date("2026-04-07T10:02:00.000Z").getTime() | ||
| ); | ||
|
|
||
| jest.useRealTimers(); | ||
| }); | ||
| }); | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.