Skip to content
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

Fix periodic scanner form #483

Merged
merged 1 commit into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 67 additions & 0 deletions ui/src/Pages/SettingsPage/PeriodicScanner.test.tsx
@@ -0,0 +1,67 @@
import { MockedProvider } from '@apollo/client/testing'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import PeriodicScanner, {
SCAN_INTERVAL_MUTATION,
SCAN_INTERVAL_QUERY,
} from './PeriodicScanner'

test('Enable periodic scanner', async () => {
const graphqlMocks = [
{
request: {
query: SCAN_INTERVAL_QUERY,
},
result: {
data: {
siteInfo: { periodicScanInterval: 7380, __typename: 'SiteInfo' },
},
},
},
{
request: {
query: SCAN_INTERVAL_MUTATION,
variables: { interval: 0 },
},
result: { data: { setPeriodicScanInterval: 0 } },
},
{
request: {
query: SCAN_INTERVAL_MUTATION,
variables: { interval: 123 * 60 },
},
result: { data: { setPeriodicScanInterval: 123 * 60 } },
},
]

render(
<MockedProvider mocks={graphqlMocks} addTypename={true}>
<PeriodicScanner />
</MockedProvider>
)

const enableCheckbox = screen.getByLabelText('Enable periodic scanner')
const inputField = screen.getByLabelText('Interval value')
const unitDropdown = screen.getByLabelText('Interval unit')

expect(inputField).toBeDisabled()
expect(unitDropdown).toBeDisabled()

fireEvent.click(enableCheckbox)

expect(inputField).toBeEnabled()
expect(unitDropdown).toBeEnabled()

fireEvent.change(unitDropdown, { target: { value: 'minute' } })
fireEvent.change(inputField, { target: { value: '123' } })

await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument()
})

fireEvent.keyUp(inputField, { key: 'Enter' })

await waitFor(() => {
expect(screen.queryByText('Loading...')).not.toBeInTheDocument()
})
})
28 changes: 20 additions & 8 deletions ui/src/Pages/SettingsPage/PeriodicScanner.tsx
@@ -1,7 +1,7 @@
import { gql } from '@apollo/client'
import React, { useRef, useState } from 'react'
import { useMutation, useQuery } from '@apollo/client'
import { InputLabelDescription } from './SettingsPage'
import { InputLabelDescription, InputLabelTitle } from './SettingsPage'
import { useTranslation } from 'react-i18next'
import { scanIntervalQuery } from './__generated__/scanIntervalQuery'
import {
Expand All @@ -13,15 +13,15 @@ import { TextField } from '../../primitives/form/Input'
import Dropdown, { DropdownItem } from '../../primitives/form/Dropdown'
import Loader from '../../primitives/Loader'

const SCAN_INTERVAL_QUERY = gql`
export const SCAN_INTERVAL_QUERY = gql`
query scanIntervalQuery {
siteInfo {
periodicScanInterval
}
}
`

const SCAN_INTERVAL_MUTATION = gql`
export const SCAN_INTERVAL_MUTATION = gql`
mutation changeScanIntervalMutation($interval: Int!) {
setPeriodicScanInterval(interval: $interval)
}
Expand Down Expand Up @@ -100,8 +100,8 @@ const PeriodicScanner = () => {

const [enablePeriodicScanner, setEnablePeriodicScanner] = useState(false)
const [scanInterval, setScanInterval] = useState<TimeValue>({
value: 4,
unit: TimeUnit.Minute,
value: 0,
unit: TimeUnit.Second,
})

const scanIntervalServerValue = useRef<number | null>(null)
Expand Down Expand Up @@ -148,7 +148,7 @@ const PeriodicScanner = () => {
if (scanIntervalServerValue.current != seconds) {
setScanIntervalMutation({
variables: {
interval: convertToSeconds(scanInterval),
interval: seconds,
},
})
scanIntervalServerValue.current = seconds
Expand Down Expand Up @@ -198,12 +198,12 @@ const PeriodicScanner = () => {

<div className="mt-4">
<label htmlFor="periodic_scan_field">
<h4 className="font-semibold">
<InputLabelTitle>
{t(
'settings.periodic_scanner.field.label',
'Periodic scan interval'
)}
</h4>
</InputLabelTitle>
<InputLabelDescription>
{t(
'settings.periodic_scanner.field.description',
Expand All @@ -214,9 +214,21 @@ const PeriodicScanner = () => {
<div className="flex gap-2">
<TextField
id="periodic_scan_field"
aria-label="Interval value"
disabled={!enablePeriodicScanner}
value={scanInterval.value}
onChange={e => {
setScanInterval(x => ({
value: Number(e.target.value),
unit: x.unit,
}))
}}
action={() => {
onScanIntervalUpdate(scanInterval)
}}
/>
<Dropdown
aria-label="Interval unit"
disabled={!enablePeriodicScanner}
items={scanIntervalUnits}
selected={scanInterval.unit}
Expand Down
2 changes: 1 addition & 1 deletion ui/src/Pages/SettingsPage/SettingsPage.tsx
Expand Up @@ -28,7 +28,7 @@ export const SectionTitle = ({ children, nospace }: SectionTitleProps) => {
}

export const InputLabelTitle = styled.h3.attrs({
className: 'font-semibold',
className: 'font-semibold mt-4',
})``

export const InputLabelDescription = styled.p.attrs({
Expand Down