Skip to content

Commit 01c42e0

Browse files
authored
Migrate ResultItem tests (#2742)
## Which problem is this PR solving? - Part of #1668 ## Description of the changes - Migrates `ResultItem` test ## How was this change tested? - Test itself. ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: nojaf <[email protected]>
1 parent ad84ea6 commit 01c42e0

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

packages/jaeger-ui/src/components/SearchTracePage/SearchResults/ResultItem.test.js

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,100 @@
1313
// limitations under the License.
1414

1515
import React from 'react';
16-
import { Tag } from 'antd';
17-
import { shallow } from 'enzyme';
16+
import { render, screen } from '@testing-library/react';
17+
import '@testing-library/jest-dom';
18+
import { MemoryRouter } from 'react-router-dom';
1819

1920
import ResultItem from './ResultItem';
2021
import * as markers from './ResultItem.markers';
2122
import traceGenerator from '../../../demo/trace-generators';
2223
import transformTraceData from '../../../model/transform-trace-data';
2324

24-
const trace = transformTraceData(traceGenerator.trace({}));
25+
// Helper function to wrap component with Router
26+
const renderWithRouter = ui => {
27+
return render(ui, { wrapper: MemoryRouter });
28+
};
29+
30+
let trace; // Use let to allow modification in tests
31+
32+
beforeEach(() => {
33+
// Reset trace data before each test.
34+
// Some tests modify the trace object (e.g., adding tags).
35+
// Resetting ensures that each test starts with a clean, unmodified trace,
36+
// preventing side effects between tests and maintaining test isolation.
37+
trace = transformTraceData(traceGenerator.trace({}));
38+
});
2539

2640
it('<ResultItem /> should render base case correctly', () => {
27-
const wrapper = shallow(<ResultItem trace={trace} durationPercent={50} linkTo="" />);
28-
const numberOfSpanText = wrapper.find(`[data-test="${markers.NUM_SPANS}"]`).first().render().text();
29-
const serviceTags = wrapper.find(`[data-test="${markers.SERVICE_TAGS}"]`).find(Tag);
30-
expect(numberOfSpanText).toBe(`${trace.spans.length} Spans`);
41+
renderWithRouter(
42+
<ResultItem
43+
trace={trace}
44+
durationPercent={50}
45+
linkTo=""
46+
toggleComparison={() => {}}
47+
isInDiffCohort={false}
48+
disableComparision={false}
49+
/>
50+
);
51+
expect(screen.getByTestId(markers.NUM_SPANS)).toHaveTextContent(`${trace.spans.length} Spans`);
52+
const serviceTagsContainer = screen.getByTestId(markers.SERVICE_TAGS);
53+
const serviceTags = serviceTagsContainer.querySelectorAll('li > .ResultItem--serviceTag');
3154
expect(serviceTags).toHaveLength(trace.services.length);
3255
});
3356

3457
it('<ResultItem /> should not render any ServiceTags when there are no services', () => {
35-
const wrapper = shallow(<ResultItem trace={{ ...trace, services: [] }} durationPercent={50} linkTo="" />);
36-
const serviceTags = wrapper.find(`[data-test="${markers.SERVICE_TAGS}"]`).find(Tag);
58+
const traceWithoutServices = { ...trace, services: [] };
59+
renderWithRouter(
60+
<ResultItem
61+
trace={traceWithoutServices}
62+
durationPercent={50}
63+
linkTo=""
64+
toggleComparison={() => {}}
65+
isInDiffCohort={false}
66+
disableComparision={false}
67+
/>
68+
);
69+
const serviceTagsContainer = screen.getByTestId(markers.SERVICE_TAGS);
70+
const serviceTags = serviceTagsContainer.querySelectorAll('li > .ResultItem--serviceTag');
3771
expect(serviceTags).toHaveLength(0);
3872
});
3973

4074
it('<ResultItem /> should render error icon on ServiceTags that have an error tag', () => {
41-
trace.spans[0].tags.push({ key: 'error', value: true });
42-
const wrapper = shallow(<ResultItem trace={trace} durationPercent={50} linkTo="" />);
43-
const errorServiceTags = wrapper.find('.ResultItem--errorIcon').getElements();
44-
expect(errorServiceTags).toHaveLength(1);
75+
// Assume trace has services and spans from the generator. Assert this assumption.
76+
expect(trace.services).toBeDefined();
77+
expect(trace.services.length).toBeGreaterThan(0);
78+
expect(trace.spans).toBeDefined();
79+
expect(trace.spans.length).toBeGreaterThan(0);
80+
81+
// Find the first span belonging to the first service.
82+
const firstService = trace.services[0];
83+
const spanWithError = trace.spans.find(span => span.process.serviceName === firstService.name);
84+
85+
// Assert that a span for the first service was found. If not, the fixture is wrong.
86+
expect(spanWithError).toBeDefined();
87+
88+
// Add the error tag directly, initializing tags array if necessary.
89+
spanWithError.tags = spanWithError.tags || [];
90+
spanWithError.tags.push({ key: 'error', value: true });
91+
92+
renderWithRouter(
93+
<ResultItem
94+
trace={trace}
95+
durationPercent={50}
96+
linkTo=""
97+
toggleComparison={() => {}}
98+
isInDiffCohort={false}
99+
disableComparision={false}
100+
/>
101+
);
102+
103+
const serviceTagsContainer = screen.getByTestId(markers.SERVICE_TAGS);
104+
// Find the tag associated with the service that should have the error
105+
const errorTag = Array.from(serviceTagsContainer.querySelectorAll('li > .ResultItem--serviceTag')).find(
106+
tag => tag.textContent.includes(firstService.name)
107+
);
108+
109+
// Assert that the specific service tag is found and has the error icon
110+
expect(errorTag).toBeDefined();
111+
expect(errorTag.querySelector('.ResultItem--errorIcon')).toBeInTheDocument();
45112
});

packages/jaeger-ui/src/components/SearchTracePage/SearchResults/ResultItem.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default class ResultItem extends React.PureComponent<Props, State> {
101101
<Link to={linkTo}>
102102
<Row>
103103
<Col span={4} className="ub-p2">
104-
<Tag className="ub-m1" data-test={markers.NUM_SPANS}>
104+
<Tag className="ub-m1" data-testid={markers.NUM_SPANS}>
105105
{this.state.numSpans} Span{this.state.numSpans > 1 && 's'}
106106
</Tag>
107107
{Boolean(this.state.numErredSpans) && (
@@ -111,7 +111,7 @@ export default class ResultItem extends React.PureComponent<Props, State> {
111111
)}
112112
</Col>
113113
<Col span={16} className="ub-p2">
114-
<ul className="ub-list-reset" data-test={markers.SERVICE_TAGS}>
114+
<ul className="ub-list-reset" data-testid={markers.SERVICE_TAGS}>
115115
{_sortBy(services, s => s.name).map(service => {
116116
const { name, numberOfSpans: count } = service;
117117
return (

0 commit comments

Comments
 (0)