-
Notifications
You must be signed in to change notification settings - Fork 462
Add annotation label unit tests #4930
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
150 changes: 150 additions & 0 deletions
150
application/ui/src/features/annotator/annotations/annotation-labels.component.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,150 @@ | ||
// Copyright (C) 2025 Intel Corporation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getMockedLabel } from 'mocks/mock-labels'; | ||
import { render } from 'test-utils/render'; | ||
import { describe, expect, it, vi } from 'vitest'; | ||
|
||
import { Label } from '../types'; | ||
import { AnnotationLabels } from './annotation-labels.component'; | ||
|
||
const mockZoom = { scale: 1, maxZoomIn: 10, hasAnimation: false, translate: { x: 0, y: 0 } }; | ||
|
||
vi.mock('src/components/zoom/zoom.provider', () => ({ | ||
useZoom: () => mockZoom, | ||
})); | ||
|
||
describe('AnnotationLabels', () => { | ||
const mockOnRemove = vi.fn(); | ||
|
||
afterEach(() => { | ||
mockOnRemove.mockClear(); | ||
}); | ||
|
||
it('renders placeholder when no labels provided', () => { | ||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={[]} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const text = container.querySelector('text'); | ||
jpggvilaca marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
expect(text).toBeInTheDocument(); | ||
expect(text?.textContent).toBe('No label'); | ||
}); | ||
|
||
it('renders single label with name and color', () => { | ||
const label = getMockedLabel({ name: 'Person', color: '#FF0000' }); | ||
|
||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={[label]} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const text = container.querySelector('text'); | ||
expect(text?.textContent).toBe('Person'); | ||
|
||
const rect = container.querySelector('rect'); | ||
expect(rect).toHaveAttribute('fill', '#FF0000'); | ||
}); | ||
|
||
it('renders multiple labels horizontally', () => { | ||
const labels: Label[] = [ | ||
getMockedLabel({ id: '1', name: 'Person', color: '#FF0000' }), | ||
getMockedLabel({ id: '2', name: 'Car', color: '#00FF00' }), | ||
]; | ||
|
||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={labels} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const texts = container.querySelectorAll('text'); | ||
expect(texts).toHaveLength(4); // 2 labels + 2 close buttons (x) | ||
|
||
// Check label names | ||
expect(texts[0]?.textContent).toBe('Person'); | ||
expect(texts[2]?.textContent).toBe('Car'); | ||
|
||
// Check close buttons | ||
expect(texts[1]?.textContent).toBe('x'); | ||
expect(texts[3]?.textContent).toBe('x'); | ||
}); | ||
|
||
it('calls onRemove when close button clicked', () => { | ||
const label = getMockedLabel({ id: 'label-1', name: 'Person' }); | ||
|
||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={[label]} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const closeButtonGroup = container.querySelector('g[style*="pointer"]'); | ||
expect(closeButtonGroup).toBeInTheDocument(); | ||
|
||
closeButtonGroup?.dispatchEvent(new Event('pointerdown', { bubbles: true, cancelable: true })); | ||
jpggvilaca marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
expect(mockOnRemove).toHaveBeenCalledTimes(1); | ||
expect(mockOnRemove).toHaveBeenCalledWith('label-1'); | ||
}); | ||
|
||
it('adjusts sizes based on zoom scale', () => { | ||
mockZoom.scale = 2; | ||
const label = getMockedLabel({ name: 'Person' }); | ||
|
||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={[label]} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const text = container.querySelector('text'); | ||
const fontSize = text?.getAttribute('font-size'); | ||
|
||
// Font size should be 14 / 2 = 7 | ||
expect(fontSize).toBe('7'); | ||
}); | ||
|
||
it('prevents event propagation on close button click', () => { | ||
const label = getMockedLabel({ id: 'label-1' }); | ||
const mockParentHandler = vi.fn(); | ||
|
||
const { container } = render( | ||
<svg onPointerDown={mockParentHandler}> | ||
<AnnotationLabels labels={[label]} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const closeButtonGroup = container.querySelector('g[style*="pointer"]'); | ||
const event = new Event('pointerdown', { bubbles: true, cancelable: true }); | ||
|
||
closeButtonGroup?.dispatchEvent(event); | ||
|
||
expect(mockOnRemove).toHaveBeenCalled(); | ||
// Parent handler should not be called due to stopPropagation | ||
expect(mockParentHandler).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('renders labels with correct positioning (no overlap)', () => { | ||
const labels: Label[] = [ | ||
getMockedLabel({ id: '1', name: 'A', color: '#FF0000' }), | ||
getMockedLabel({ id: '2', name: 'B', color: '#00FF00' }), | ||
]; | ||
|
||
const { container } = render( | ||
<svg> | ||
<AnnotationLabels labels={labels} onRemove={mockOnRemove} /> | ||
</svg> | ||
); | ||
|
||
const rects = container.querySelectorAll('rect'); | ||
const firstX = parseFloat(rects[0]?.getAttribute('x') || '0'); | ||
const secondX = parseFloat(rects[2]?.getAttribute('x') || '0'); | ||
|
||
// Second label should be positioned after the first | ||
expect(secondX).toBeGreaterThan(firstX); | ||
}); | ||
}); |
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.