Skip to content

Commit 8c0883b

Browse files
committed
feat(FR-1530): add useIntervalValue test code and fix broken test code snapshot
1 parent 9c43d1d commit 8c0883b

File tree

2 files changed

+208
-1
lines changed

2 files changed

+208
-1
lines changed

react/src/components/__snapshots__/DomainSelector.test.tsx.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`DomainSelect default render 1`] = `
44
<DocumentFragment>
55
<div
6-
class="ant-select ant-select-outlined css-dev-only-do-not-override-1odpy5d ant-select-focused ant-select-single ant-select-show-arrow ant-select-open"
6+
class="ant-select ant-select-outlined css-dev-only-do-not-override-1m63z2v ant-select-focused ant-select-single ant-select-show-arrow ant-select-open"
77
title="hello"
88
>
99
<div
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import { useInterval, useIntervalValue } from './useIntervalValue';
2+
import { render, screen, act } from '@testing-library/react';
3+
import React from 'react';
4+
5+
// Test component for useInterval
6+
const TestIntervalComponent: React.FC<{
7+
delay: number | null;
8+
pauseWhenHidden?: boolean;
9+
}> = ({ delay, pauseWhenHidden = true }) => {
10+
const [count, setCount] = React.useState(0);
11+
12+
useInterval(
13+
() => {
14+
setCount((prev) => prev + 1);
15+
},
16+
delay,
17+
pauseWhenHidden,
18+
);
19+
20+
return <div data-testid="interval-count">{count}</div>;
21+
};
22+
23+
// Test component for useIntervalValue
24+
const TestIntervalValueComponent: React.FC<{
25+
delay: number | null;
26+
pauseWhenHidden?: boolean;
27+
}> = ({ delay, pauseWhenHidden = true }) => {
28+
const value = useIntervalValue(
29+
() => Date.now(),
30+
delay,
31+
undefined,
32+
pauseWhenHidden,
33+
);
34+
35+
return <div data-testid="interval-value">{value}</div>;
36+
};
37+
38+
describe('useInterval and useIntervalValue hooks', () => {
39+
beforeEach(() => {
40+
jest.useFakeTimers();
41+
// Mock document.hidden as false initially (page is visible)
42+
Object.defineProperty(document, 'hidden', {
43+
writable: true,
44+
value: false,
45+
});
46+
});
47+
48+
afterEach(() => {
49+
jest.useRealTimers();
50+
});
51+
52+
describe('useInterval', () => {
53+
it('should increment count at specified interval', () => {
54+
render(<TestIntervalComponent delay={1000} />);
55+
56+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
57+
58+
act(() => {
59+
jest.advanceTimersByTime(1000);
60+
});
61+
62+
expect(screen.getByTestId('interval-count')).toHaveTextContent('1');
63+
64+
act(() => {
65+
jest.advanceTimersByTime(2000);
66+
});
67+
68+
expect(screen.getByTestId('interval-count')).toHaveTextContent('3');
69+
});
70+
71+
it('should pause when pauseWhenHidden is true and page becomes hidden', () => {
72+
render(<TestIntervalComponent delay={1000} pauseWhenHidden={true} />);
73+
74+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
75+
76+
// Page becomes hidden
77+
act(() => {
78+
Object.defineProperty(document, 'hidden', { value: true });
79+
document.dispatchEvent(new Event('visibilitychange'));
80+
});
81+
82+
// Timer should not increment
83+
act(() => {
84+
jest.advanceTimersByTime(2000);
85+
});
86+
87+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
88+
89+
// Page becomes visible again
90+
act(() => {
91+
Object.defineProperty(document, 'hidden', { value: false });
92+
document.dispatchEvent(new Event('visibilitychange'));
93+
});
94+
95+
// Should execute immediately when visible again
96+
expect(screen.getByTestId('interval-count')).toHaveTextContent('1');
97+
98+
// Continue normal interval
99+
act(() => {
100+
jest.advanceTimersByTime(1000);
101+
});
102+
103+
expect(screen.getByTestId('interval-count')).toHaveTextContent('2');
104+
});
105+
106+
it('should continue running when pauseWhenHidden is false and page becomes hidden', () => {
107+
render(<TestIntervalComponent delay={1000} pauseWhenHidden={false} />);
108+
109+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
110+
111+
// Page becomes hidden
112+
act(() => {
113+
Object.defineProperty(document, 'hidden', { value: true });
114+
document.dispatchEvent(new Event('visibilitychange'));
115+
});
116+
117+
// Timer should continue to increment even when hidden
118+
act(() => {
119+
jest.advanceTimersByTime(2000);
120+
});
121+
122+
expect(screen.getByTestId('interval-count')).toHaveTextContent('2');
123+
});
124+
125+
it('should not run when delay is null', () => {
126+
render(<TestIntervalComponent delay={null} />);
127+
128+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
129+
130+
act(() => {
131+
jest.advanceTimersByTime(5000);
132+
});
133+
134+
expect(screen.getByTestId('interval-count')).toHaveTextContent('0');
135+
});
136+
});
137+
138+
describe('useIntervalValue', () => {
139+
it('should update value at specified interval', () => {
140+
render(<TestIntervalValueComponent delay={1000} />);
141+
142+
const initialValue = screen.getByTestId('interval-value').textContent;
143+
144+
act(() => {
145+
jest.advanceTimersByTime(1000);
146+
});
147+
148+
const updatedValue = screen.getByTestId('interval-value').textContent;
149+
expect(updatedValue).not.toBe(initialValue);
150+
});
151+
152+
it('should pause value updates when page becomes hidden and pauseWhenHidden is true', () => {
153+
render(
154+
<TestIntervalValueComponent delay={1000} pauseWhenHidden={true} />,
155+
);
156+
157+
const initialValue = screen.getByTestId('interval-value').textContent;
158+
159+
// Page becomes hidden
160+
act(() => {
161+
Object.defineProperty(document, 'hidden', { value: true });
162+
document.dispatchEvent(new Event('visibilitychange'));
163+
});
164+
165+
act(() => {
166+
jest.advanceTimersByTime(2000);
167+
});
168+
169+
// Value should not change when hidden
170+
expect(screen.getByTestId('interval-value')).toHaveTextContent(
171+
initialValue!,
172+
);
173+
174+
// Page becomes visible again
175+
act(() => {
176+
Object.defineProperty(document, 'hidden', { value: false });
177+
document.dispatchEvent(new Event('visibilitychange'));
178+
});
179+
180+
// Value should update immediately when visible again
181+
const newValue = screen.getByTestId('interval-value').textContent;
182+
expect(newValue).not.toBe(initialValue);
183+
});
184+
185+
it('should continue updating when pauseWhenHidden is false and page becomes hidden', () => {
186+
render(
187+
<TestIntervalValueComponent delay={1000} pauseWhenHidden={false} />,
188+
);
189+
190+
const initialValue = screen.getByTestId('interval-value').textContent;
191+
192+
// Page becomes hidden
193+
act(() => {
194+
Object.defineProperty(document, 'hidden', { value: true });
195+
document.dispatchEvent(new Event('visibilitychange'));
196+
});
197+
198+
act(() => {
199+
jest.advanceTimersByTime(1000);
200+
});
201+
202+
// Value should continue to update even when hidden
203+
const updatedValue = screen.getByTestId('interval-value').textContent;
204+
expect(updatedValue).not.toBe(initialValue);
205+
});
206+
});
207+
});

0 commit comments

Comments
 (0)