-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe0c848
commit 2be0320
Showing
5 changed files
with
205 additions
and
83 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { AttributePanelRow } from './AttributePanelRow'; | ||
import { locationService } from '@grafana/runtime'; | ||
import { MetricFunction } from 'utils/shared'; | ||
|
||
jest.mock('@grafana/runtime', () => ({ | ||
locationService: { | ||
push: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock('utils/analytics', () => ({ | ||
reportAppInteraction: jest.fn(), | ||
USER_EVENTS_ACTIONS: { | ||
home: { | ||
attribute_panel_item_clicked: 'attribute_panel_item_clicked', | ||
}, | ||
}, | ||
USER_EVENTS_PAGES: { | ||
home: 'home', | ||
}, | ||
})); | ||
|
||
describe('AttributePanelRow', () => { | ||
const mockProps = { | ||
index: 0, | ||
type: 'errors' as MetricFunction, | ||
label: 'Test Label', | ||
labelTitle: 'Label Title', | ||
value: 'Test Text', | ||
valueTitle: 'Text Title', | ||
url: '/test-url', | ||
}; | ||
|
||
it('renders correctly with required props', () => { | ||
render(<AttributePanelRow {...mockProps} />); | ||
|
||
expect(screen.getByText(mockProps.labelTitle)).toBeInTheDocument(); | ||
expect(screen.getByText(mockProps.valueTitle)).toBeInTheDocument(); | ||
expect(screen.getByText(mockProps.label)).toBeInTheDocument(); | ||
expect(screen.getByText(mockProps.value)).toBeInTheDocument(); | ||
}); | ||
|
||
it('navigates to the correct URL on click', () => { | ||
render(<AttributePanelRow {...mockProps} />); | ||
const rowElement = screen.getByText(mockProps.label).closest('div'); | ||
fireEvent.click(rowElement!); | ||
expect(locationService.push).toHaveBeenCalledWith(mockProps.url); | ||
}); | ||
|
||
it('renders the row header only if index is 0', () => { | ||
render(<AttributePanelRow {...mockProps} />); | ||
expect(screen.getByText(mockProps.labelTitle)).toBeInTheDocument(); | ||
}); | ||
|
||
it('does not render the row header only if index is > 0', () => { | ||
render(<AttributePanelRow {...{ ...mockProps, index: 1 }} />); | ||
expect(screen.queryByText(mockProps.labelTitle)).not.toBeInTheDocument(); | ||
}); | ||
}); |
This file contains 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 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,69 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { AttributePanelRows } from './AttributePanelRows'; | ||
import { DataFrame, Field } from '@grafana/data'; | ||
import { MetricFunction } from 'utils/shared'; | ||
|
||
describe('AttributePanelRows', () => { | ||
const createField = (name: string, values: any[], labels: Record<string, string> = {}) => ({ | ||
name, | ||
values, | ||
labels, | ||
}) as Field; | ||
|
||
const createDataFrame = (fields: Field[]) => ({ | ||
fields, | ||
}) as DataFrame; | ||
|
||
const dummySeries = [ | ||
createDataFrame([ | ||
createField('time', []), | ||
createField('Test service 1', [10, 20], { 'resource.service.name': '"Test service 1"' }), | ||
]), | ||
createDataFrame([ | ||
createField('time', []), | ||
createField('Test service 2', [15, 5], { 'resource.service.name': '"Test service 2"' }), | ||
]), | ||
]; | ||
|
||
const dummyDurationSeries = [ | ||
createDataFrame([ | ||
createField('traceIdHidden', ['trace-1', 'trace-2']), | ||
createField('spanID', ['span-1', 'span-2']), | ||
createField('traceName', ['Test name 1', 'Test name 2']), | ||
createField('traceService', ['Test service 1', 'Test service 2']), | ||
createField('duration', [3000, 500]), | ||
]), | ||
]; | ||
|
||
it('renders message if provided', () => { | ||
const msg = 'No data available.'; | ||
render(<AttributePanelRows series={[]} type={'errors' as MetricFunction} message={msg} />); | ||
expect(screen.getByText(msg)).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders an empty container if no series or message is provided', () => { | ||
render(<AttributePanelRows series={[]} type={'errors' as MetricFunction} />); | ||
expect(screen.getByText('No series data')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders error rows sorted by total errors when type is "errors"', () => { | ||
render(<AttributePanelRows series={dummySeries} type={'errors' as MetricFunction} />); | ||
|
||
expect(screen.getAllByText('Total errors').length).toBe(1); | ||
|
||
const labels = screen.getAllByText('Test service', { exact: false }); | ||
expect(labels[0].textContent).toContain('Test service 1'); | ||
expect(labels[1].textContent).toContain('Test service 2'); | ||
}); | ||
|
||
it('renders duration rows sorted by duration when type is not "errors"', () => { | ||
render(<AttributePanelRows series={dummyDurationSeries} type={'duration' as MetricFunction} />); | ||
|
||
expect(screen.getAllByText('Duration').length).toBe(1); | ||
|
||
const labels = screen.getAllByText('Test', { exact: false }); | ||
expect(labels[0].textContent).toContain('Test service 1: Test name 1'); | ||
expect(labels[1].textContent).toContain('Test service 2: Test name 2'); | ||
}); | ||
}); |
This file contains 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 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