|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | 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'; |
18 | 19 |
|
19 | 20 | import ResultItem from './ResultItem';
|
20 | 21 | import * as markers from './ResultItem.markers';
|
21 | 22 | import traceGenerator from '../../../demo/trace-generators';
|
22 | 23 | import transformTraceData from '../../../model/transform-trace-data';
|
23 | 24 |
|
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 | +}); |
25 | 39 |
|
26 | 40 | 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'); |
31 | 54 | expect(serviceTags).toHaveLength(trace.services.length);
|
32 | 55 | });
|
33 | 56 |
|
34 | 57 | 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'); |
37 | 71 | expect(serviceTags).toHaveLength(0);
|
38 | 72 | });
|
39 | 73 |
|
40 | 74 | 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(); |
45 | 112 | });
|
0 commit comments