-
Notifications
You must be signed in to change notification settings - Fork 17
code improvements and test coverage #1411
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
Open
jrozner
wants to merge
1
commit into
main
Choose a base branch
from
worktree-frontend-modernize
base: main
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.
Open
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion
1
frontend/src/components/bullet_chooser/evidence_type_chooser.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
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
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
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
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
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
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,24 @@ | ||
| import { Component, type ReactNode, type ErrorInfo } from 'react' | ||
| import ErrorDisplay from 'src/components/error_display' | ||
|
|
||
| type Props = { children: ReactNode } | ||
| type State = { error: Error | null } | ||
|
|
||
| export default class ErrorBoundary extends Component<Props, State> { | ||
| state: State = { error: null } | ||
|
|
||
| static getDerivedStateFromError(error: Error): State { | ||
| return { error } | ||
| } | ||
|
|
||
| componentDidCatch(_error: Error, _info: ErrorInfo) { | ||
| // Errors are surfaced via the render method | ||
| } | ||
|
|
||
| render() { | ||
| if (this.state.error) { | ||
| return <ErrorDisplay err={this.state.error} /> | ||
| } | ||
| return this.props.children | ||
| } | ||
| } | ||
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,112 @@ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { render, screen } from '@testing-library/react' | ||
| import { userEvent } from '@testing-library/user-event' | ||
| import Form from './index' | ||
|
|
||
| describe('Form', () => { | ||
| it('renders children', () => { | ||
| render( | ||
| <Form result={null} loading={false} onSubmit={() => {}}> | ||
| <input placeholder="Name" /> | ||
| </Form>, | ||
| ) | ||
| expect(screen.getByPlaceholderText('Name')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders submit button when submitText is provided', () => { | ||
| render(<Form result={null} loading={false} onSubmit={() => {}} submitText="Save" />) | ||
| expect(screen.getByText('Save')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('does not render submit button when submitText is absent', () => { | ||
| render(<Form result={null} loading={false} onSubmit={() => {}} />) | ||
| expect(screen.queryByRole('button')).toBeNull() | ||
| }) | ||
|
|
||
| it('renders cancel button when onCancel is provided', () => { | ||
| render( | ||
| <Form | ||
| result={null} | ||
| loading={false} | ||
| onSubmit={() => {}} | ||
| onCancel={() => {}} | ||
| cancelText="Cancel" | ||
| />, | ||
| ) | ||
| expect(screen.getByText('Cancel')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('calls onSubmit when form is submitted', async () => { | ||
| const onSubmit = vi.fn((e) => e.preventDefault()) | ||
| const user = userEvent.setup() | ||
| render( | ||
| <Form result={null} loading={false} onSubmit={onSubmit} submitText="Go"> | ||
| <input /> | ||
| </Form>, | ||
| ) | ||
| await user.click(screen.getByText('Go')) | ||
| expect(onSubmit).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('calls onCancel when cancel button is clicked', async () => { | ||
| const onCancel = vi.fn() | ||
| const user = userEvent.setup() | ||
| render( | ||
| <Form | ||
| result={null} | ||
| loading={false} | ||
| onSubmit={() => {}} | ||
| onCancel={onCancel} | ||
| cancelText="Cancel" | ||
| />, | ||
| ) | ||
| await user.click(screen.getByText('Cancel')) | ||
| expect(onCancel).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('displays an error result message', () => { | ||
| render( | ||
| <Form | ||
| result={{ err: new Error('Something went wrong') }} | ||
| loading={false} | ||
| onSubmit={() => {}} | ||
| />, | ||
| ) | ||
| expect(screen.getByText('Something went wrong')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('displays a success result message', () => { | ||
| render(<Form result={{ success: 'Saved!' }} loading={false} onSubmit={() => {}} />) | ||
| expect(screen.getByText('Saved!')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('shows loading state on submit button', () => { | ||
| render(<Form result={null} loading={true} onSubmit={() => {}} submitText="Save" />) | ||
| // Button renders a spinner when loading; the text may be hidden or replaced | ||
| const button = screen.getByRole('button') | ||
| expect(button).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('disables cancel when loading', () => { | ||
| render( | ||
| <Form | ||
| result={null} | ||
| loading={true} | ||
| onSubmit={() => {}} | ||
| onCancel={() => {}} | ||
| cancelText="Cancel" | ||
| />, | ||
| ) | ||
| // Cancel button is disabled when loading | ||
| const buttons = screen.getAllByRole('button') | ||
| const cancelButton = buttons.find((b) => b.textContent === 'Cancel') | ||
| expect(cancelButton).toBeDisabled() | ||
| }) | ||
|
|
||
| it('disables submit when disableSubmit is true', () => { | ||
| render( | ||
| <Form result={null} loading={false} onSubmit={() => {}} submitText="Save" disableSubmit />, | ||
| ) | ||
| expect(screen.getByRole('button')).toBeDisabled() | ||
| }) | ||
| }) |
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
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
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
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,80 @@ | ||
| import { describe, it, expect, vi } from 'vitest' | ||
| import { render, screen, within } from '@testing-library/react' | ||
| import { userEvent } from '@testing-library/user-event' | ||
| import Modal from './index' | ||
|
|
||
| describe('Modal', () => { | ||
| it('renders the title', () => { | ||
| render( | ||
| <Modal title="My Modal" onRequestClose={() => {}}> | ||
| {'.'} | ||
| </Modal>, | ||
| ) | ||
| expect(screen.getByText('My Modal')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders children', () => { | ||
| render( | ||
| <Modal title="Test" onRequestClose={() => {}}> | ||
| <p>Modal content here</p> | ||
| </Modal>, | ||
| ) | ||
| expect(screen.getByText('Modal content here')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders via a portal into document.body', () => { | ||
| const { baseElement } = render( | ||
| <Modal title="Portal Test" onRequestClose={() => {}}> | ||
| <span>portal child</span> | ||
| </Modal>, | ||
| ) | ||
| expect(within(baseElement).getByText('Portal Test')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('calls onRequestClose when the backdrop is clicked', async () => { | ||
| const user = userEvent.setup() | ||
| const onRequestClose = vi.fn() | ||
| render( | ||
| <Modal title="Close test" onRequestClose={onRequestClose}> | ||
| <button>inside</button> | ||
| </Modal>, | ||
| ) | ||
| const backdrop = document.body.querySelector('[aria-modal]')?.parentElement | ||
| if (backdrop) { | ||
| await user.pointer({ target: backdrop, keys: '[MouseLeft]' }) | ||
| } | ||
| expect(onRequestClose).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('does not call onRequestClose when the inner modal is clicked', async () => { | ||
| const user = userEvent.setup() | ||
| const onRequestClose = vi.fn() | ||
| render( | ||
| <Modal title="No close" onRequestClose={onRequestClose}> | ||
| <button>inside modal</button> | ||
| </Modal>, | ||
| ) | ||
| await user.click(screen.getByText('No close')) | ||
| expect(onRequestClose).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('has role="dialog" and aria-modal on the dialog element', () => { | ||
| render( | ||
| <Modal title="ARIA test" onRequestClose={() => {}}> | ||
| {'.'} | ||
| </Modal>, | ||
| ) | ||
| const dialog = screen.getByRole('dialog') | ||
| expect(dialog).toHaveAttribute('aria-modal', 'true') | ||
| }) | ||
|
|
||
| it('dialog is labelled by the title', () => { | ||
| render( | ||
| <Modal title="Labelled Modal" onRequestClose={() => {}}> | ||
| {'.'} | ||
| </Modal>, | ||
| ) | ||
| const dialog = screen.getByRole('dialog', { name: 'Labelled Modal' }) | ||
| expect(dialog).toBeInTheDocument() | ||
| }) | ||
| }) |
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
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
Oops, something went wrong.
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.
🚩 ErrorBoundary has no recovery mechanism
The
ErrorBoundarycomponent setsstate.errorwhen an error is caught viagetDerivedStateFromError, but provides no way to reset the state back tonull. Once triggered, the user is permanently stuck on the<ErrorDisplay>with no navigation or retry button. A common pattern is to reset on location change (viacomponentDidUpdatechecking akeyprop orlocationfrom the router) or to provide a "Try Again" button that callsthis.setState({ error: null }). Since this boundary wraps the entire app at the root, recovery is especially important.Was this helpful? React with 👍 or 👎 to provide feedback.