-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add data retention time configuration component to admin panel #239
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
Draft
Copilot
wants to merge
7
commits into
master
Choose a base branch
from
copilot/add-data-retention-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc5fde3
Initial plan
Copilot 5a34d68
Add DataRetentionSelector and AdminDataRetention components
Copilot 24a919a
Fix code review issues - remove unnecessary try-catch and unused import
Copilot 4459fa4
Improve input handling and add loading state delay
Copilot bcab89d
Address PR feedback: use Stack, move to bottom, add indefinite retent…
Copilot a33b3e2
Fix minimum value handling and document day calculation assumptions
Copilot 92d7e87
Fix test failures - add IDs to TextFields and update test assertions
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,88 @@ | ||
| import React from "react"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import DataRetentionSelector, { | ||
| TimeUnit, | ||
| DataRetentionValue, | ||
| } from "./DataRetentionSelector"; | ||
|
|
||
| describe("DataRetentionSelector", () => { | ||
| const defaultValue: DataRetentionValue = { | ||
| value: 30, | ||
| unit: TimeUnit.DAYS, | ||
| }; | ||
|
|
||
| test("renders with default value", () => { | ||
| const onChange = jest.fn(); | ||
| render(<DataRetentionSelector value={defaultValue} onChange={onChange} />); | ||
|
|
||
| expect(screen.getByLabelText("Retention Time")).toHaveValue(30); | ||
| expect(screen.getByLabelText("Unit")).toHaveTextContent("Days"); | ||
| }); | ||
|
|
||
| test("calls onChange when value changes", () => { | ||
| const onChange = jest.fn(); | ||
| render(<DataRetentionSelector value={defaultValue} onChange={onChange} />); | ||
|
|
||
| const valueInput = screen.getByLabelText("Retention Time"); | ||
| userEvent.clear(valueInput); | ||
| userEvent.type(valueInput, "60"); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith({ | ||
| value: 60, | ||
| unit: TimeUnit.DAYS, | ||
| }); | ||
| }); | ||
|
|
||
| test("calls onChange when unit changes", () => { | ||
| const onChange = jest.fn(); | ||
| render(<DataRetentionSelector value={defaultValue} onChange={onChange} />); | ||
|
|
||
| const unitSelect = screen.getByLabelText("Unit"); | ||
| userEvent.click(unitSelect); | ||
| userEvent.click(screen.getByText("Weeks")); | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith({ | ||
| value: 30, | ||
| unit: TimeUnit.WEEKS, | ||
| }); | ||
| }); | ||
|
|
||
| test("displays all time unit options", () => { | ||
| const onChange = jest.fn(); | ||
| render(<DataRetentionSelector value={defaultValue} onChange={onChange} />); | ||
|
|
||
| const unitSelect = screen.getByLabelText("Unit"); | ||
| userEvent.click(unitSelect); | ||
|
|
||
| expect(screen.getAllByText("Days").length).toBeGreaterThan(0); | ||
| expect(screen.getByText("Weeks")).toBeInTheDocument(); | ||
| expect(screen.getByText("Months")).toBeInTheDocument(); | ||
| expect(screen.getByText("Years")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("can be disabled", () => { | ||
| const onChange = jest.fn(); | ||
| render( | ||
| <DataRetentionSelector | ||
| value={defaultValue} | ||
| onChange={onChange} | ||
| disabled={true} | ||
| /> | ||
| ); | ||
|
|
||
| const valueInput = screen.getByLabelText("Retention Time"); | ||
| const unitSelect = screen.getByLabelText("Unit"); | ||
|
|
||
| expect(valueInput).toBeDisabled(); | ||
| expect(unitSelect).toHaveAttribute("aria-disabled", "true"); | ||
| }); | ||
|
|
||
| test("minimum allowed value is 1", () => { | ||
| const onChange = jest.fn(); | ||
| render(<DataRetentionSelector value={defaultValue} onChange={onChange} />); | ||
|
|
||
| const valueInput = screen.getByLabelText("Retention Time"); | ||
| expect(valueInput).toHaveAttribute("min", "1"); | ||
| }); | ||
| }); |
This file contains hidden or 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,77 @@ | ||
| import React from "react"; | ||
| import { Stack, TextField, MenuItem } from "@mui/material"; | ||
|
|
||
| export enum TimeUnit { | ||
| DAYS = "days", | ||
| WEEKS = "weeks", | ||
| MONTHS = "months", | ||
| YEARS = "years", | ||
| } | ||
|
|
||
| export type DataRetentionValue = { | ||
| value: number; | ||
| unit: TimeUnit; | ||
| }; | ||
|
|
||
| type DataRetentionSelectorProps = { | ||
| value: DataRetentionValue; | ||
| onChange: (value: DataRetentionValue) => void; | ||
| disabled?: boolean; | ||
| }; | ||
|
|
||
| const DataRetentionSelector: React.FC<DataRetentionSelectorProps> = ({ | ||
| value, | ||
| onChange, | ||
| disabled = false, | ||
| }) => { | ||
| const handleValueChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| const newValue = parseInt(event.target.value, 10); | ||
| if (isNaN(newValue) && event.target.value !== "") { | ||
| return; | ||
| } | ||
| onChange({ | ||
| ...value, | ||
| value: isNaN(newValue) ? 1 : Math.max(1, newValue), | ||
| }); | ||
| }; | ||
|
|
||
| const handleUnitChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| onChange({ | ||
| ...value, | ||
| unit: event.target.value as TimeUnit, | ||
| }); | ||
| }; | ||
|
|
||
| return ( | ||
| <Stack direction="row" spacing={2} alignItems="center"> | ||
| <TextField | ||
| id="retention-time" | ||
| label="Retention Time" | ||
| type="number" | ||
| value={value.value} | ||
| onChange={handleValueChange} | ||
| disabled={disabled} | ||
| inputProps={{ | ||
| min: 1, | ||
| }} | ||
| sx={{ flex: 1 }} | ||
| /> | ||
| <TextField | ||
| id="retention-unit" | ||
| select | ||
| label="Unit" | ||
| value={value.unit} | ||
| onChange={handleUnitChange} | ||
| disabled={disabled} | ||
| sx={{ flex: 1 }} | ||
| > | ||
| <MenuItem value={TimeUnit.DAYS}>Days</MenuItem> | ||
| <MenuItem value={TimeUnit.WEEKS}>Weeks</MenuItem> | ||
| <MenuItem value={TimeUnit.MONTHS}>Months</MenuItem> | ||
| <MenuItem value={TimeUnit.YEARS}>Years</MenuItem> | ||
| </TextField> | ||
| </Stack> | ||
| ); | ||
| }; | ||
|
|
||
| export default DataRetentionSelector; |
This file contains hidden or 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
157 changes: 157 additions & 0 deletions
157
src/scenes/AdminControl/Components/AdminDataRetention.test.tsx
This file contains hidden or 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,157 @@ | ||
| import React from "react"; | ||
| import AdminDataRetention from "./AdminDataRetention"; | ||
| import { render } from "../../../test-utils"; | ||
| import * as models from "../../../models"; | ||
| import { userRoles } from "../../../apiConsts"; | ||
| import { screen, waitFor } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
|
|
||
| const preloadedState = { | ||
| whoami: { | ||
| user: new models.User({ roles: [userRoles.user, userRoles.admin] }), | ||
| }, | ||
| loadingReducer: { | ||
| GET_WHOAMI: false, | ||
| }, | ||
| tenantId: "tenant-id", | ||
| }; | ||
|
|
||
| describe("AdminDataRetention", () => { | ||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test("renders the component with default values", async () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| expect( | ||
| screen.getByText("Set data retention time") | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText( | ||
| "Determine how long data should be retained before automatic deletion" | ||
| ) | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByLabelText( | ||
| "Retain data indefinitely (no automatic deletion)" | ||
| ) | ||
| ).toBeChecked(); | ||
| expect(screen.queryByLabelText("Retention Time")).toBeNull(); | ||
| }); | ||
|
|
||
| test("allows toggling indefinite retention", () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| const checkbox = screen.getByLabelText( | ||
| "Retain data indefinitely (no automatic deletion)" | ||
| ); | ||
| expect(checkbox).toBeChecked(); | ||
|
|
||
| userEvent.click(checkbox); | ||
|
|
||
| expect(checkbox).not.toBeChecked(); | ||
| expect(screen.getByLabelText("Retention Time")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("Unit")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("allows changing retention time value", () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| // Uncheck indefinite retention first | ||
| const checkbox = screen.getByLabelText( | ||
| "Retain data indefinitely (no automatic deletion)" | ||
| ); | ||
| userEvent.click(checkbox); | ||
|
|
||
| const valueInput = screen.getByLabelText("Retention Time"); | ||
| userEvent.clear(valueInput); | ||
| userEvent.type(valueInput, "60"); | ||
|
|
||
| expect(valueInput).toHaveValue(60); | ||
| }); | ||
|
|
||
| test("allows changing time unit", () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| // Uncheck indefinite retention first | ||
| const checkbox = screen.getByLabelText( | ||
| "Retain data indefinitely (no automatic deletion)" | ||
| ); | ||
| userEvent.click(checkbox); | ||
|
|
||
| const unitSelect = screen.getByLabelText("Unit"); | ||
| userEvent.click(unitSelect); | ||
| userEvent.click(screen.getByText("Weeks")); | ||
|
|
||
| // Check that the unit field now displays "Weeks" | ||
| expect(unitSelect).toHaveTextContent("Weeks"); | ||
| }); | ||
|
|
||
| test("save button is enabled and clickable", () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| const saveButton = screen.getByRole("button", { | ||
| name: "Save retention settings", | ||
| }); | ||
| expect(saveButton).not.toBeDisabled(); | ||
|
|
||
| userEvent.click(saveButton); | ||
| // Button should be clickable without errors | ||
| }); | ||
|
|
||
| test("shows warning dialog for short retention periods", async () => { | ||
| render(<AdminDataRetention />, { preloadedState }); | ||
|
|
||
| // Uncheck indefinite retention | ||
| const checkbox = screen.getByLabelText( | ||
| "Retain data indefinitely (no automatic deletion)" | ||
| ); | ||
| userEvent.click(checkbox); | ||
|
|
||
| // Set a short retention period (5 days) | ||
| const valueInput = screen.getByLabelText("Retention Time"); | ||
| userEvent.clear(valueInput); | ||
| userEvent.type(valueInput, "5"); | ||
|
|
||
| // Click save | ||
| const saveButton = screen.getByRole("button", { | ||
| name: "Save retention settings", | ||
| }); | ||
| userEvent.click(saveButton); | ||
|
|
||
| // Warning dialog should appear | ||
| await waitFor(() => { | ||
| expect( | ||
| screen.getByText("Short Retention Period Warning") | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| test("displays forbidden message if user is not admin", () => { | ||
| render(<AdminDataRetention />, { | ||
| preloadedState: { | ||
| ...preloadedState, | ||
| whoami: { user: new models.User({ roles: [userRoles.user] }) }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.queryByText("Set data retention time")).toBeNull(); | ||
| expect( | ||
| screen.getByText("You don't have permission to view this page.") | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| test("displays skeleton while loading", () => { | ||
| render(<AdminDataRetention />, { | ||
| preloadedState: { | ||
| ...preloadedState, | ||
| loadingReducer: { | ||
| GET_WHOAMI: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.queryByText("Set data retention time")).toBeNull(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please place the component at the bottom instead of the top
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to bottom of AdminControl in commit a33b3e2. The component now appears after AdminAddLocation.