Skip to content

Commit fdbba61

Browse files
committed
test new refresh hook
1 parent 265c668 commit fdbba61

File tree

2 files changed

+86
-9
lines changed

2 files changed

+86
-9
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { act, renderHook } from '__tests__/_setup/testing-utils';
2+
import { describe, expect, it } from 'vitest';
3+
import { useLiveRefresh } from '../../app/hooks/useLiveRefresh';
4+
5+
describe('useLiveRefresh', () => {
6+
it('returns base properties', () => {
7+
const { result } = renderHook(() => useLiveRefresh());
8+
9+
expect(result.current.isLiveRefreshRunning).toBeFalsy();
10+
expect(result.current.liveRefreshLabel).toBe('');
11+
expect(result.current.refetchInterval).toBe(0);
12+
});
13+
14+
it.each([
15+
{ refetch: 5000, label: '5s', time: 'five' },
16+
{ refetch: 15000, label: '15s', time: 'fifteen' },
17+
{ refetch: 30000, label: '30s', time: 'thirty' },
18+
{ refetch: 60000, label: '1m', time: 'sixty' },
19+
{ refetch: 120000, label: '2m', time: 'onehundredtwenty' },
20+
{ refetch: 300000, label: '5m', time: 'threehundred' },
21+
{ refetch: 900000, label: '15m', time: 'ninehundred' },
22+
])('starts fetch interval', ({ label, refetch, time }) => {
23+
const { result } = renderHook(() => useLiveRefresh());
24+
25+
act(() => {
26+
result.current.startLiveRefresh(time);
27+
});
28+
29+
expect(result.current.isLiveRefreshRunning).toBeTruthy();
30+
expect(result.current.liveRefreshLabel).toBe(label);
31+
expect(result.current.refetchInterval).toBe(refetch);
32+
});
33+
34+
it('stops fetch interval', () => {
35+
const { result } = renderHook(() => useLiveRefresh());
36+
37+
act(() => {
38+
result.current.startLiveRefresh('five');
39+
});
40+
expect(result.current.refetchInterval).toBe(5000);
41+
42+
act(() => {
43+
result.current.stopLiveRefresh();
44+
});
45+
46+
expect(result.current.isLiveRefreshRunning).toBeFalsy();
47+
expect(result.current.refetchInterval).toBe(0);
48+
});
49+
50+
it('does not activate on invalid time', () => {
51+
const { result } = renderHook(() => useLiveRefresh());
52+
53+
act(() => {
54+
result.current.startLiveRefresh('five');
55+
});
56+
expect(result.current.refetchInterval).toBe(5000);
57+
58+
act(() => {
59+
result.current.startLiveRefresh(null);
60+
});
61+
62+
expect(result.current.isLiveRefreshRunning).toBeFalsy();
63+
expect(result.current.refetchInterval).toBe(0);
64+
});
65+
66+
it('set activation time to 0 on unexpected time', () => {
67+
const { result } = renderHook(() => useLiveRefresh());
68+
69+
act(() => {
70+
result.current.startLiveRefresh('uhm');
71+
});
72+
73+
expect(result.current.isLiveRefreshRunning).toBeFalsy();
74+
expect(result.current.refetchInterval).toBe(0);
75+
});
76+
});

src/Serilog.Ui.Web/src/app/hooks/useLiveRefresh.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ enum options {
1212
}
1313

1414
export const liveRefreshOptions = [
15-
{ label: '5s', liveLabel: '5s', value: 'five' },
16-
{ label: '15s', liveLabel: '15s', value: 'fifteen' },
17-
{ label: '30s', liveLabel: '30s', value: 'thirty' },
18-
{ label: '1m', liveLabel: '1m', value: 'sixty' },
19-
{ label: '2m', liveLabel: '2m', value: 'onehundredtwenty' },
20-
{ label: '5m', liveLabel: '5m', value: 'threehundred' },
21-
{ label: '15m', liveLabel: '15m', value: 'ninehundred' },
15+
{ label: '5s', value: 'five' },
16+
{ label: '15s', value: 'fifteen' },
17+
{ label: '30s', value: 'thirty' },
18+
{ label: '1m', value: 'sixty' },
19+
{ label: '2m', value: 'onehundredtwenty' },
20+
{ label: '5m', value: 'threehundred' },
21+
{ label: '15m', value: 'ninehundred' },
2222
];
2323

2424
export const useLiveRefresh = () => {
@@ -28,15 +28,16 @@ export const useLiveRefresh = () => {
2828
const liveRefreshLabel = !isLiveRefreshRunning
2929
? ''
3030
: liveRefreshOptions.find((lr) => lr.value === options[refetchInterval / 1000])
31-
?.liveLabel;
31+
?.label;
3232

3333
const startLiveRefresh = (v: string | null) => {
3434
if (v === null) {
3535
return setRefetchInterval(0);
3636
}
3737

3838
const eachSecond = options[v];
39-
setRefetchInterval(eachSecond * 1000);
39+
const isNan = Number.isNaN(Number.parseInt(eachSecond, 10));
40+
setRefetchInterval(isNan ? 0 : eachSecond * 1000);
4041
};
4142

4243
const stopLiveRefresh = () => {

0 commit comments

Comments
 (0)