-
Notifications
You must be signed in to change notification settings - Fork 0
fix: add copy button to error display and stabilize error re-renders (#37) #38
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
claude
wants to merge
6
commits into
main
Choose a base branch
from
claude/copy-error-text
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ebfdd5d
fix: add copy button to error display and stabilize error re-renders …
github-actions[bot] 5965f69
fix: use null-byte delimiter in #each key to match serializeErrors
github-actions[bot] 37a3d33
fix: cancel copy timeout on teardown and add queries serializeErrors …
github-actions[bot] c2e1a30
fix layout
DTCurrie b4c6688
fix: harden serializeErrors separator and add icon-swap test
github-actions[bot] d70a839
fix: create userEvent instance with advanceTimers for fake-timer test
github-actions[bot] 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,5 @@ | ||
| --- | ||
| '@viamrobotics/test-widgets': patch | ||
| --- | ||
|
|
||
| Add copy button to error display and prevent re-rendering unchanged errors during query polling |
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 { render, screen, waitFor } from '@testing-library/svelte' | ||
| import userEvent from '@testing-library/user-event' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import Subject from '../error.svelte' | ||
|
|
||
| describe('<ErrorDisplay>', () => { | ||
| let user: ReturnType<typeof userEvent.setup> | ||
|
|
||
| beforeEach(() => { | ||
| user = userEvent.setup() | ||
| }) | ||
|
|
||
| it('renders error name and message', () => { | ||
| const error = new Error('Something went wrong') | ||
| error.name = 'TestError' | ||
|
|
||
| render(Subject, { lastError: error }) | ||
| expect(screen.getByText('TestError: Something went wrong')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('does not render when lastError is null', () => { | ||
| const { container } = render(Subject, { lastError: null }) | ||
| expect(container.querySelector('p')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('does not render when lastError is undefined', () => { | ||
| const { container } = render(Subject, { lastError: undefined }) | ||
| expect(container.querySelector('p')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('renders a copy button', () => { | ||
| const error = new Error('Something went wrong') | ||
| render(Subject, { lastError: error }) | ||
| expect(screen.getByRole('button', { name: /copy error/iu })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('copies error text to clipboard when copy button is clicked', async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined) | ||
| vi.stubGlobal('navigator', { clipboard: { writeText } }) | ||
|
|
||
| const error = new Error('Something went wrong') | ||
| error.name = 'TestError' | ||
| render(Subject, { lastError: error }) | ||
|
|
||
| const copyButton = screen.getByRole('button', { name: /copy error/iu }) | ||
| await user.click(copyButton) | ||
|
|
||
| expect(writeText).toHaveBeenCalledWith('TestError: Something went wrong') | ||
| }) | ||
|
|
||
| it('shows check icon after successful copy and restores content-copy icon after timeout', async () => { | ||
| vi.useFakeTimers() | ||
| const timerUser = userEvent.setup({ advanceTimers: vi.advanceTimersByTime.bind(vi) }) | ||
| const writeText = vi.fn().mockResolvedValue(undefined) | ||
| vi.stubGlobal('navigator', { clipboard: { writeText } }) | ||
|
|
||
| const error = new Error('Something went wrong') | ||
| error.name = 'TestError' | ||
| render(Subject, { lastError: error }) | ||
|
|
||
| expect(screen.getByTestId('icon-content-copy')).toBeInTheDocument() | ||
|
|
||
| const copyButton = screen.getByRole('button', { name: /copy error/iu }) | ||
| await timerUser.click(copyButton) | ||
|
|
||
| await waitFor(() => expect(screen.getByTestId('icon-check')).toBeInTheDocument()) | ||
| expect(screen.queryByTestId('icon-content-copy')).not.toBeInTheDocument() | ||
|
|
||
| vi.runAllTimers() | ||
|
|
||
| await waitFor(() => expect(screen.getByTestId('icon-content-copy')).toBeInTheDocument()) | ||
| expect(screen.queryByTestId('icon-check')).not.toBeInTheDocument() | ||
|
|
||
| vi.useRealTimers() | ||
| }) | ||
| }) | ||
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,98 @@ | ||
| import type { QueryObserverResult } from '@tanstack/svelte-query' | ||
|
|
||
| import { render, screen } from '@testing-library/svelte' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import Subject from '../queries.svelte' | ||
|
|
||
| const createErrorQuery = (name: string, message: string): QueryObserverResult => { | ||
| const error = new Error(message) | ||
| error.name = name | ||
| return { | ||
| data: null, | ||
| error, | ||
| isError: true, | ||
| isLoading: false, | ||
| isPending: false, | ||
| isSuccess: false, | ||
| status: 'error', | ||
| } as unknown as QueryObserverResult | ||
| } | ||
|
|
||
| const createSuccessQuery = (data: unknown = null): QueryObserverResult => | ||
| ({ | ||
| data, | ||
| error: null, | ||
| isError: false, | ||
| isLoading: false, | ||
| isPending: false, | ||
| isSuccess: true, | ||
| status: 'success', | ||
| }) as unknown as QueryObserverResult | ||
|
|
||
| const createLoadingQuery = (): QueryObserverResult => | ||
| ({ | ||
| data: undefined, | ||
| error: null, | ||
| isError: false, | ||
| isLoading: true, | ||
| isPending: true, | ||
| isSuccess: false, | ||
| status: 'pending', | ||
| }) as unknown as QueryObserverResult | ||
|
|
||
| describe('<Queries> serializeErrors guard', () => { | ||
| it('guard passes: updates displayed errors when error content changes', async () => { | ||
| const { rerender } = render(Subject, { queries: [createErrorQuery('ErrorA', 'First error')] }) | ||
| expect(screen.getByText('ErrorA: First error')).toBeInTheDocument() | ||
|
|
||
| await rerender({ queries: [createErrorQuery('ErrorB', 'Second error')] }) | ||
|
|
||
| expect(screen.queryByText('ErrorA: First error')).not.toBeInTheDocument() | ||
| expect(screen.getByText('ErrorB: Second error')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('guard blocks: preserves DOM element when same error content is re-provided', async () => { | ||
| const { rerender } = render(Subject, { | ||
| queries: [createErrorQuery('TestError', 'Same message')], | ||
| }) | ||
| const element = screen.getByText('TestError: Same message') | ||
|
|
||
| // Rerender with a new object that has identical content — guard should block state update | ||
| await rerender({ queries: [createErrorQuery('TestError', 'Same message')] }) | ||
|
|
||
| expect(screen.getByText('TestError: Same message')).toBe(element) | ||
| }) | ||
|
|
||
| it('guard resets: clears errors when all queries succeed', async () => { | ||
| const { rerender } = render(Subject, { queries: [createErrorQuery('TestError', 'An error')] }) | ||
| expect(screen.getByText('TestError: An error')).toBeInTheDocument() | ||
|
|
||
| await rerender({ queries: [createSuccessQuery()] }) | ||
|
|
||
| expect(screen.queryByText('TestError: An error')).not.toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('guard preserves errors during loading (errors are null while polling)', async () => { | ||
| const { rerender } = render(Subject, { queries: [createErrorQuery('TestError', 'An error')] }) | ||
| expect(screen.getByText('TestError: An error')).toBeInTheDocument() | ||
|
|
||
| // During polling queries may briefly be in loading state with null errors | ||
| await rerender({ queries: [createLoadingQuery()] }) | ||
|
|
||
| expect(screen.getByText('TestError: An error')).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('guard blocks: new error object with same content as existing does not update state', async () => { | ||
| const { rerender } = render(Subject, { | ||
| queries: [createErrorQuery('TestError', 'Duplicate content')], | ||
| }) | ||
| const element = screen.getByText('TestError: Duplicate content') | ||
|
|
||
| // A second error object with the same name+message is a duplicate — guard should block | ||
| const newQuery = createErrorQuery('TestError', 'Duplicate content') | ||
| await rerender({ queries: [newQuery] }) | ||
|
|
||
| expect(screen.getByText('TestError: Duplicate content')).toBe(element) | ||
| }) | ||
| }) |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.