-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad8be9d
commit ebf9200
Showing
2 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { act } from 'react'; | ||
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
import { usePopupMonitor } from './usePopupMonitor'; | ||
|
||
describe('usePopupMonitor', () => { | ||
let popupWindow: { closed: boolean }; | ||
let onCloseMock: () => void; | ||
|
||
beforeEach(() => { | ||
popupWindow = { | ||
closed: false, | ||
}; | ||
onCloseMock = vi.fn(); | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllTimers(); | ||
vi.resetAllMocks(); | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it('calls onClose when the popup is closed', () => { | ||
const { result } = renderHook(() => usePopupMonitor(onCloseMock)); | ||
|
||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
act(() => { | ||
popupWindow.closed = true; | ||
vi.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(onCloseMock).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('does not call onClose when the popup remains open', () => { | ||
const { result } = renderHook(() => usePopupMonitor(onCloseMock)); | ||
|
||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1500); | ||
}); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('cleans up interval when the component is unmounted', () => { | ||
const { result, unmount } = renderHook(() => usePopupMonitor(onCloseMock)); | ||
|
||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
act(() => { | ||
unmount(); | ||
}); | ||
|
||
expect(() => vi.runOnlyPendingTimers()).not.toThrow(); | ||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('stops monitoring the popup when stopPopupMonitor is called', () => { | ||
const { result } = renderHook(() => usePopupMonitor(onCloseMock)); | ||
|
||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
act(() => { | ||
result.current.stopPopupMonitor(); | ||
}); | ||
|
||
act(() => { | ||
popupWindow.closed = true; | ||
vi.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(onCloseMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('clears the previous interval before starting a new one', () => { | ||
const { result } = renderHook(() => usePopupMonitor(onCloseMock)); | ||
|
||
// Start monitoring the first popup | ||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
// Mock and spy on clearInterval | ||
const clearIntervalSpy = vi.spyOn(global, 'clearInterval'); | ||
|
||
// Start monitoring a second popup | ||
act(() => { | ||
result.current.startPopupMonitor(popupWindow as unknown as Window); | ||
}); | ||
|
||
expect(clearIntervalSpy).toHaveBeenCalledTimes(1); // clearInterval should be called once | ||
|
||
clearIntervalSpy.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters