Skip to content

Commit 97fcbf3

Browse files
Merge pull request #4228 from RedisInsight/fe/feature/RI-6330-disable-the-overview-refresh
Fe/feature/ri-6330 Disable the overview refresh
2 parents b496f8f + 466c82f commit 97fcbf3

File tree

24 files changed

+406
-140
lines changed

24 files changed

+406
-140
lines changed

redisinsight/ui/src/components/auto-refresh/AutoRefresh.spec.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
33
import { fireEvent, screen, render, act } from 'uiSrc/utils/test-utils'
4+
import { localStorageService } from 'uiSrc/services'
45
import AutoRefresh, { Props } from './AutoRefresh'
56
import { DEFAULT_REFRESH_RATE } from './utils'
67

@@ -9,6 +10,11 @@ const mockedProps = mock<Props>()
910
const INLINE_ITEM_EDITOR = 'inline-item-editor'
1011

1112
describe('AutoRefresh', () => {
13+
beforeEach(() => {
14+
// Clear any stored refresh rate before each test
15+
jest.clearAllMocks()
16+
jest.spyOn(localStorageService, 'get').mockImplementation(() => null)
17+
})
1218
it('should render', () => {
1319
expect(render(<AutoRefresh {...instance(mockedProps)} />)).toBeTruthy()
1420
})
@@ -25,6 +31,18 @@ describe('AutoRefresh', () => {
2531
expect(queryByTestId('refresh-message-label')).not.toBeInTheDocument()
2632
})
2733

34+
it('prop "displayLastRefresh = true" should show refresh time message', () => {
35+
const { queryByTestId } = render(<AutoRefresh {...instance(mockedProps)} displayLastRefresh />)
36+
37+
expect(queryByTestId('refresh-message')).toBeInTheDocument()
38+
})
39+
40+
it('prop "displayLastRefresh = false" should hide refresh time message', () => {
41+
const { queryByTestId } = render(<AutoRefresh {...instance(mockedProps)} displayLastRefresh={false} />)
42+
43+
expect(queryByTestId('refresh-message')).not.toBeInTheDocument()
44+
})
45+
2846
it('should call onRefresh', () => {
2947
const onRefresh = jest.fn()
3048
render(<AutoRefresh {...instance(mockedProps)} onRefresh={onRefresh} />)
@@ -132,6 +150,67 @@ describe('AutoRefresh', () => {
132150
})
133151
expect(onRefresh).toBeCalledTimes(3)
134152
})
153+
154+
it('should respect minimumRefreshRate when setting refresh rate', async () => {
155+
const onChangeAutoRefreshRate = jest.fn()
156+
const minimumRefreshRate = 6
157+
render(
158+
<AutoRefresh
159+
{...instance(mockedProps)}
160+
minimumRefreshRate={minimumRefreshRate}
161+
onChangeAutoRefreshRate={onChangeAutoRefreshRate}
162+
/>
163+
)
164+
165+
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
166+
fireEvent.click(screen.getByTestId('refresh-rate'))
167+
fireEvent.change(screen.getByTestId(INLINE_ITEM_EDITOR), { target: { value: (minimumRefreshRate / 2).toString() } })
168+
screen.getByTestId(/apply-btn/).click()
169+
expect(onChangeAutoRefreshRate).toHaveBeenLastCalledWith(false, minimumRefreshRate.toString())
170+
})
171+
172+
it('should allow valid refresh rates above minimumRefreshRate', async () => {
173+
const onChangeAutoRefreshRate = jest.fn()
174+
const minimumRefreshRate = 6
175+
render(
176+
<AutoRefresh
177+
{...instance(mockedProps)}
178+
minimumRefreshRate={minimumRefreshRate}
179+
onChangeAutoRefreshRate={onChangeAutoRefreshRate}
180+
/>
181+
)
182+
183+
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
184+
fireEvent.click(screen.getByTestId('refresh-rate'))
185+
fireEvent.change(
186+
screen.getByTestId(INLINE_ITEM_EDITOR),
187+
{ target: { value: (minimumRefreshRate * 2).toString() } }
188+
)
189+
screen.getByTestId(/apply-btn/).click()
190+
191+
expect(onChangeAutoRefreshRate).toHaveBeenLastCalledWith(false, (minimumRefreshRate * 2).toString())
192+
})
193+
194+
it('should use defaultRefreshRate when provided', () => {
195+
const customDefaultRate = '30'
196+
render(
197+
<AutoRefresh
198+
{...instance(mockedProps)}
199+
defaultRefreshRate={customDefaultRate}
200+
/>
201+
)
202+
203+
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
204+
expect(screen.getByTestId('refresh-rate')).toHaveTextContent(`${customDefaultRate} s`)
205+
})
206+
207+
it('should use DEFAULT_REFRESH_RATE when defaultRefreshRate is not provided', () => {
208+
render(<AutoRefresh {...instance(mockedProps)} />)
209+
210+
// Open config and check default value
211+
fireEvent.click(screen.getByTestId('auto-refresh-config-btn'))
212+
expect(screen.getByTestId('refresh-rate')).toHaveTextContent(`${DEFAULT_REFRESH_RATE} s`)
213+
})
135214
})
136215

137216
it('should NOT call onRefresh with disabled state', async () => {

redisinsight/ui/src/components/auto-refresh/AutoRefresh.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export interface Props {
2626
postfix: string
2727
loading: boolean
2828
displayText?: boolean
29+
displayLastRefresh?: boolean
2930
lastRefreshTime: Nullable<number>
3031
testid?: string
3132
containerClassName?: string
@@ -34,6 +35,8 @@ export interface Props {
3435
onRefreshClicked?: () => void
3536
onEnableAutoRefresh?: (enableAutoRefresh: boolean, refreshRate: string) => void
3637
onChangeAutoRefreshRate?: (enableAutoRefresh: boolean, refreshRate: string) => void
38+
minimumRefreshRate?: number
39+
defaultRefreshRate?: string
3740
iconSize?: EuiButtonIconSizes
3841
disabled?: boolean
3942
enableAutoRefreshDefault?: boolean
@@ -45,6 +48,7 @@ const AutoRefresh = ({
4548
postfix,
4649
loading,
4750
displayText = true,
51+
displayLastRefresh = true,
4852
lastRefreshTime,
4953
containerClassName = '',
5054
testid = '',
@@ -55,14 +59,16 @@ const AutoRefresh = ({
5559
onChangeAutoRefreshRate,
5660
iconSize = 'm',
5761
disabled,
62+
minimumRefreshRate,
63+
defaultRefreshRate,
5864
enableAutoRefreshDefault = false
5965
}: Props) => {
6066
let intervalText: NodeJS.Timeout
6167
let intervalRefresh: NodeJS.Timeout
6268

6369
const [refreshMessage, setRefreshMessage] = useState(NOW)
6470
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
65-
const [refreshRate, setRefreshRate] = useState<string>('')
71+
const [refreshRate, setRefreshRate] = useState<string>(defaultRefreshRate || '')
6672
const [refreshRateMessage, setRefreshRateMessage] = useState<string>('')
6773
const [enableAutoRefresh, setEnableAutoRefresh] = useState(enableAutoRefreshDefault)
6874
const [editingRate, setEditingRate] = useState(false)
@@ -75,7 +81,7 @@ const AutoRefresh = ({
7581

7682
useEffect(() => {
7783
const refreshRateStorage = localStorageService.get(BrowserStorageItem.autoRefreshRate + postfix)
78-
|| DEFAULT_REFRESH_RATE
84+
|| defaultRefreshRate || DEFAULT_REFRESH_RATE
7985

8086
setRefreshRate(refreshRateStorage)
8187
}, [postfix])
@@ -121,14 +127,13 @@ const AutoRefresh = ({
121127
return () => clearInterval(intervalRefresh)
122128
}, [enableAutoRefresh, refreshRate, loading, disabled, lastRefreshTime])
123129

124-
const getLastRefreshDelta = (time:Nullable<number>) => (Date.now() - (time || 0)) / 1_000
130+
const getLastRefreshDelta = (time: Nullable<number>) => (Date.now() - (time || 0)) / 1_000
125131

126132
const getDataTestid = (suffix: string) => (testid ? `${testid}-${suffix}` : suffix)
127133

128134
const updateLastRefresh = () => {
129135
const delta = getLastRefreshDelta(lastRefreshTime)
130136
const text = getTextByRefreshTime(delta, lastRefreshTime ?? 0)
131-
132137
lastRefreshTime && setRefreshMessage(text)
133138
}
134139

@@ -140,7 +145,8 @@ const AutoRefresh = ({
140145
}
141146

142147
const handleApplyAutoRefreshRate = (initValue: string) => {
143-
const value = +initValue >= MIN_REFRESH_RATE ? initValue : `${MIN_REFRESH_RATE}`
148+
const minRefreshRate = minimumRefreshRate || MIN_REFRESH_RATE
149+
const value = +initValue >= minRefreshRate ? initValue : `${minRefreshRate}`
144150
setRefreshRate(value)
145151
setEditingRate(false)
146152
localStorageService.set(BrowserStorageItem.autoRefreshRate + postfix, value)
@@ -167,14 +173,16 @@ const AutoRefresh = ({
167173
}
168174

169175
return (
170-
<div className={cx(styles.container, containerClassName, { [styles.enable]: !disabled && enableAutoRefresh })}>
176+
<div className={cx(styles.container, containerClassName, { [styles.enable]: !disabled && enableAutoRefresh })} data-testid={getDataTestid('auto-refresh-container')}>
171177
<EuiTextColor className={styles.summary}>
172178
{displayText && (
173179
<span data-testid={getDataTestid('refresh-message-label')}>{enableAutoRefresh ? 'Auto refresh:' : 'Last refresh:'}</span>
174180
)}
175-
<span className={cx('refresh-message-time', styles.time, { [styles.disabled]: disabled })} data-testid={getDataTestid('refresh-message')}>
176-
{` ${enableAutoRefresh ? refreshRateMessage : refreshMessage}`}
177-
</span>
181+
{displayLastRefresh && (
182+
<span className={cx('refresh-message-time', styles.time, { [styles.disabled]: disabled })} data-testid={getDataTestid('refresh-message')}>
183+
{` ${enableAutoRefresh ? refreshRateMessage : refreshMessage}`}
184+
</span>
185+
)}
178186
</EuiTextColor>
179187

180188
<EuiToolTip

redisinsight/ui/src/components/auto-refresh/styles.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,4 @@
144144
display: inline-block !important;
145145
}
146146
}
147-
}
147+
}

redisinsight/ui/src/components/database-overview/DatabaseOverview.spec.tsx

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,53 @@ const overviewMetrics = getOverviewMetrics({
1919
})
2020

2121
describe('DatabaseOverview', () => {
22-
it('should render', () => {
23-
expect(render(
24-
<DatabaseOverview metrics={overviewMetrics} />
25-
)).toBeTruthy()
22+
const mockLoadData = jest.fn()
23+
const mockHandleEnableAutoRefresh = jest.fn()
24+
25+
beforeEach(() => {
26+
jest.clearAllMocks()
27+
})
28+
29+
it('should render with metrics', () => {
30+
const { container } = render(
31+
<DatabaseOverview
32+
metrics={overviewMetrics}
33+
loadData={mockLoadData}
34+
lastRefreshTime={null}
35+
handleEnableAutoRefresh={mockHandleEnableAutoRefresh}
36+
/>
37+
)
38+
39+
expect(container.querySelector('[data-test-subj="overview-cpu"]')).toBeInTheDocument()
40+
expect(container.querySelector('[data-test-subj="overview-commands-sec"]')).toBeInTheDocument()
41+
expect(container.querySelector('[data-test-subj="overview-total-memory"]')).toBeInTheDocument()
42+
expect(container.querySelector('[data-test-subj="overview-total-keys"]')).toBeInTheDocument()
43+
expect(container.querySelector('[data-test-subj="overview-connected-clients"]')).toBeInTheDocument()
44+
})
45+
46+
it('should render auto-refresh component', () => {
47+
const { container } = render(
48+
<DatabaseOverview
49+
metrics={overviewMetrics}
50+
loadData={mockLoadData}
51+
lastRefreshTime={Date.now()}
52+
handleEnableAutoRefresh={mockHandleEnableAutoRefresh}
53+
/>
54+
)
55+
56+
expect(container.querySelector('[data-testid="auto-refresh-overview-auto-refresh-container"]')).toBeInTheDocument()
57+
})
58+
59+
it('should not render anything when metrics are empty', () => {
60+
const { container } = render(
61+
<DatabaseOverview
62+
metrics={[]}
63+
loadData={mockLoadData}
64+
lastRefreshTime={null}
65+
handleEnableAutoRefresh={mockHandleEnableAutoRefresh}
66+
/>
67+
)
68+
69+
expect(container.firstChild?.childNodes.length).toBe(0)
2670
})
2771
})

0 commit comments

Comments
 (0)