-
Notifications
You must be signed in to change notification settings - Fork 0
test: more tests for admin/events, also added alias for /src/__tests_ - $test #147
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 all commits
Commits
Show all changes
2 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
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
114 changes: 114 additions & 0 deletions
114
frontend/src/components/admin/events/__tests__/EventDetailsModal.test.ts
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,114 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { render, screen } from '@testing-library/svelte'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { setupAnimationMock } from '$test/test-utils'; | ||
| import { createMockEventDetail } from '$routes/admin/__tests__/test-utils'; | ||
|
|
||
| vi.mock('@lucide/svelte', async () => | ||
| (await import('$test/test-utils')).createMockIconModule('X')); | ||
| vi.mock('$components/EventTypeIcon.svelte', async () => | ||
| (await import('$test/test-utils')).createMockSvelteComponent('<span>icon</span>')); | ||
|
|
||
| import EventDetailsModal from '../EventDetailsModal.svelte'; | ||
|
|
||
| function renderModal(overrides: Partial<{ | ||
| event: ReturnType<typeof createMockEventDetail> | null; | ||
| open: boolean; | ||
| }> = {}) { | ||
| const onClose = vi.fn(); | ||
| const onReplay = vi.fn(); | ||
| const onViewRelated = vi.fn(); | ||
| const event = 'event' in overrides ? overrides.event : createMockEventDetail(); | ||
| const open = overrides.open ?? true; | ||
| const result = render(EventDetailsModal, { props: { event, open, onClose, onReplay, onViewRelated } }); | ||
| return { ...result, onClose, onReplay, onViewRelated }; | ||
| } | ||
|
|
||
| describe('EventDetailsModal', () => { | ||
| beforeEach(() => { | ||
| setupAnimationMock(); | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('renders nothing when closed', () => { | ||
| renderModal({ open: false }); | ||
| expect(screen.queryByText('Event Details')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders nothing when event is null', () => { | ||
| renderModal({ event: null }); | ||
| expect(screen.queryByText('Basic Information')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows modal title and basic information heading when open with event', () => { | ||
| renderModal(); | ||
| expect(screen.getByText('Event Details')).toBeInTheDocument(); | ||
| expect(screen.getByText('Basic Information')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { label: 'Event ID', value: 'evt-1' }, | ||
| { label: 'Event Type', value: 'execution_completed' }, | ||
| { label: 'Correlation ID', value: 'corr-123' }, | ||
| { label: 'Aggregate ID', value: 'exec-456' }, | ||
| ])('displays $label with value "$value"', ({ label, value }) => { | ||
| renderModal(); | ||
| expect(screen.getByText(label)).toBeInTheDocument(); | ||
| expect(screen.getByText(value)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows full event JSON in pre block', () => { | ||
| const detail = createMockEventDetail(); | ||
| renderModal({ event: detail }); | ||
| expect(screen.getByText('Full Event Data')).toBeInTheDocument(); | ||
| const pre = document.querySelector('pre'); | ||
| expect(pre?.textContent).toContain('"event_id"'); | ||
| expect(pre?.textContent).toContain('"evt-1"'); | ||
| }); | ||
|
|
||
| it('shows related events section with clickable buttons', () => { | ||
| renderModal(); | ||
| expect(screen.getByText('Related Events')).toBeInTheDocument(); | ||
| expect(screen.getByText('execution_started')).toBeInTheDocument(); | ||
| expect(screen.getByText('pod_created')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onViewRelated with event_id when clicking a related event', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onViewRelated } = renderModal(); | ||
| await user.click(screen.getByText('execution_started')); | ||
| expect(onViewRelated).toHaveBeenCalledWith('rel-1'); | ||
| }); | ||
|
|
||
| it('hides related events section when no related events', () => { | ||
| const detail = createMockEventDetail(); | ||
| detail.related_events = []; | ||
| renderModal({ event: detail }); | ||
| expect(screen.queryByText('Related Events')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onReplay with event_id when Replay Event button is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onReplay } = renderModal(); | ||
| await user.click(screen.getByRole('button', { name: 'Replay Event' })); | ||
| expect(onReplay).toHaveBeenCalledWith('evt-1'); | ||
| }); | ||
|
|
||
| it('calls onClose when Close button in footer is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onClose } = renderModal(); | ||
| await user.click(screen.getByRole('button', { name: 'Close' })); | ||
| expect(onClose).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { case: 'null', value: null }, | ||
| { case: 'undefined', value: undefined }, | ||
| ])('shows "-" when correlation_id is $case', ({ value }) => { | ||
| const detail = createMockEventDetail(); | ||
| detail.event.metadata.correlation_id = value as undefined; | ||
| renderModal({ event: detail }); | ||
| const cells = screen.getAllByText('-'); | ||
| expect(cells.length).toBeGreaterThanOrEqual(1); | ||
| }); | ||
| }); |
71 changes: 71 additions & 0 deletions
71
frontend/src/components/admin/events/__tests__/EventFilters.test.ts
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,71 @@ | ||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { render, screen } from '@testing-library/svelte'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { EVENT_TYPES, createDefaultEventFilters } from '$lib/admin/events/eventTypes'; | ||
|
|
||
| import EventFilters from '../EventFilters.svelte'; | ||
|
|
||
| function renderFilters(overrides: Partial<{ onApply: () => void; onClear: () => void }> = {}) { | ||
| const onApply = overrides.onApply ?? vi.fn(); | ||
| const onClear = overrides.onClear ?? vi.fn(); | ||
| const filters = createDefaultEventFilters(); | ||
| const result = render(EventFilters, { props: { filters, onApply, onClear } }); | ||
| return { ...result, onApply, onClear }; | ||
| } | ||
|
|
||
| describe('EventFilters', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('renders heading and both action buttons', () => { | ||
| renderFilters(); | ||
| expect(screen.getByText('Filter Events')).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: 'Clear All' })).toBeInTheDocument(); | ||
| expect(screen.getByRole('button', { name: 'Apply' })).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { id: 'event-types-filter', label: 'Event Types' }, | ||
| { id: 'search-filter', label: 'Search' }, | ||
| { id: 'correlation-filter', label: 'Correlation ID' }, | ||
| { id: 'aggregate-filter', label: 'Aggregate ID' }, | ||
| { id: 'user-filter', label: 'User ID' }, | ||
| { id: 'service-filter', label: 'Service' }, | ||
| { id: 'start-time-filter', label: 'Start Time' }, | ||
| { id: 'end-time-filter', label: 'End Time' }, | ||
| ])('renders "$label" filter with id=$id', ({ id, label }) => { | ||
| renderFilters(); | ||
| expect(screen.getByLabelText(label)).toBeInTheDocument(); | ||
| expect(document.getElementById(id)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('event types select lists all EVENT_TYPES as options', () => { | ||
| renderFilters(); | ||
| const select = screen.getByLabelText('Event Types') as HTMLSelectElement; | ||
| const options = Array.from(select.options).map(o => o.value); | ||
| expect(options).toEqual(EVENT_TYPES); | ||
| }); | ||
|
|
||
| it('calls onApply when Apply button is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onApply } = renderFilters(); | ||
| await user.click(screen.getByRole('button', { name: 'Apply' })); | ||
| expect(onApply).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('calls onClear when Clear All button is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const { onClear } = renderFilters(); | ||
| await user.click(screen.getByRole('button', { name: 'Clear All' })); | ||
| expect(onClear).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('text inputs accept user input', async () => { | ||
| const user = userEvent.setup(); | ||
| renderFilters(); | ||
| const searchInput = screen.getByLabelText('Search') as HTMLInputElement; | ||
| await user.type(searchInput, 'test query'); | ||
| expect(searchInput.value).toBe('test query'); | ||
| }); | ||
| }); |
62 changes: 62 additions & 0 deletions
62
frontend/src/components/admin/events/__tests__/EventStatsCards.test.ts
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,62 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { render, screen } from '@testing-library/svelte'; | ||
| import { createMockStats } from '$routes/admin/__tests__/test-utils'; | ||
|
|
||
| import EventStatsCards from '../EventStatsCards.svelte'; | ||
|
|
||
| function renderCards(stats: ReturnType<typeof createMockStats> | null, totalEvents = 500) { | ||
| return render(EventStatsCards, { props: { stats, totalEvents } }); | ||
| } | ||
|
|
||
| describe('EventStatsCards', () => { | ||
| it('renders nothing when stats is null', () => { | ||
| const { container } = renderCards(null); | ||
| expect(container.textContent?.trim()).toBe(''); | ||
| }); | ||
|
|
||
| it('shows all four stat cards when stats provided', () => { | ||
| renderCards(createMockStats()); | ||
| expect(screen.getByText('Events (Last 24h)')).toBeInTheDocument(); | ||
| expect(screen.getByText('Error Rate (24h)')).toBeInTheDocument(); | ||
| expect(screen.getByText('Avg Execution Time (24h)')).toBeInTheDocument(); | ||
| expect(screen.getByText('Active Users (24h)')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('displays total_events with locale formatting and totalEvents denominator', () => { | ||
| renderCards(createMockStats({ total_events: 1500 }), 10000); | ||
| expect(screen.getByText((1500).toLocaleString())).toBeInTheDocument(); | ||
| expect(screen.getByText(new RegExp(`of ${(10000).toLocaleString()} total`))).toBeInTheDocument(); | ||
| }); | ||
HardMax71 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it.each([ | ||
| { error_rate: 5, expectedText: '5%', expectedClass: 'text-red-600' }, | ||
| { error_rate: 0, expectedText: '0%', expectedClass: 'text-green-600' }, | ||
| ])('error rate $error_rate shows "$expectedText" with $expectedClass', ({ error_rate, expectedText, expectedClass }) => { | ||
| const { container } = renderCards(createMockStats({ error_rate })); | ||
| const el = screen.getByText(expectedText); | ||
| expect(el).toBeInTheDocument(); | ||
| expect(container.querySelector(`.${expectedClass}`)).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('formats avg_processing_time to 2 decimal places', () => { | ||
| renderCards(createMockStats({ avg_processing_time: 3.456 })); | ||
| expect(screen.getByText('3.46s')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows "0s" when avg_processing_time is falsy', () => { | ||
| renderCards(createMockStats({ avg_processing_time: 0 })); | ||
| expect(screen.getByText('0s')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('shows active user count from top_users array length', () => { | ||
| renderCards(createMockStats({ | ||
| top_users: [ | ||
| { user_id: 'u1', count: 10 }, | ||
| { user_id: 'u2', count: 5 }, | ||
| { user_id: 'u3', count: 2 }, | ||
| ], | ||
| })); | ||
| expect(screen.getByText('3')).toBeInTheDocument(); | ||
| expect(screen.getByText('with events')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.