Skip to content

Commit 58c36ba

Browse files
test: migrate SearchableSelect and TraceId tests from Enzyme to RTL (#2869)
Converted tests for SearchableSelect and TraceId components from Enzyme to React Testing Library. Replaced snapshot and Enzyme-specific logic with RTL assertions while maintaining full test coverage. --------- Signed-off-by: Vishvamsinh Vaghela <[email protected]>
1 parent 677e893 commit 58c36ba

File tree

3 files changed

+71
-96
lines changed

3 files changed

+71
-96
lines changed

packages/jaeger-ui/src/components/common/SearchableSelect.test.js

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,21 @@
1313
// limitations under the License.
1414

1515
import React from 'react';
16-
import { shallow } from 'enzyme';
17-
16+
import { render, screen } from '@testing-library/react';
17+
import userEvent from '@testing-library/user-event';
18+
import '@testing-library/jest-dom';
1819
import { Select } from 'antd';
1920
import SearchableSelect, { filterOptionsByLabel } from './SearchableSelect';
2021

2122
describe('SearchableSelect', () => {
22-
let wrapper;
23-
2423
const options = [
2524
{ label: 'Test 1', value: 'test1' },
2625
{ label: 'Test 2', value: 'test2' },
2726
{ label: 'Test 3', value: 'test3' },
2827
];
2928

30-
beforeEach(() => {
31-
wrapper = shallow(
29+
it('renders all options when dropdown is opened', async () => {
30+
render(
3231
<SearchableSelect>
3332
{options.map((option, i) => (
3433
<Select.Option key={option.value} value={option.value} data-testid={`option-${i}`}>
@@ -37,64 +36,62 @@ describe('SearchableSelect', () => {
3736
))}
3837
</SearchableSelect>
3938
);
40-
});
4139

42-
it('SearchableSelect renders with all props and options', () => {
43-
expect(wrapper).toMatchSnapshot();
44-
});
40+
const select = screen.getByRole('combobox');
41+
await userEvent.click(select);
4542

46-
it('search is enabled', () => {
47-
expect(wrapper.props().showSearch).toBe(true);
43+
await Promise.all([
44+
screen.findByTestId('option-0'),
45+
screen.findByTestId('option-1'),
46+
screen.findByTestId('option-2'),
47+
]);
48+
49+
expect(screen.getByTestId('option-0')).toHaveTextContent('Test 1');
50+
expect(screen.getByTestId('option-1')).toHaveTextContent('Test 2');
51+
expect(screen.getByTestId('option-2')).toHaveTextContent('Test 3');
4852
});
4953

50-
it('renders all the options correctly', () => {
51-
const ops = wrapper.find(Select.Option);
54+
it('filters options based on input when showSearch is enabled', async () => {
55+
render(
56+
<SearchableSelect>
57+
<Select.Option value="apple">Apple</Select.Option>
58+
<Select.Option value="banana">Banana</Select.Option>
59+
</SearchableSelect>
60+
);
5261

53-
expect(ops.length).toBe(3);
62+
const select = screen.getByRole('combobox');
63+
await userEvent.click(select);
64+
await userEvent.type(select, 'ban');
5465

55-
ops.forEach((op, i) => {
56-
expect(op.props().value).toBe(options[i].value);
57-
expect(op.props().children).toBe(options[i].label);
58-
});
66+
expect(screen.getByText('Banana')).toBeInTheDocument();
67+
expect(screen.queryByText('Apple')).not.toBeInTheDocument();
5968
});
6069
});
6170

6271
describe('filterOptionsByLabel', () => {
63-
const options = [
64-
{
65-
children: 'Test 1',
66-
label: 'Test 1',
67-
value: 'test1',
68-
},
69-
];
72+
const option = {
73+
children: 'Test 1',
74+
label: 'Test 1',
75+
value: 'test1',
76+
};
7077

7178
it('should return true when passed empty input', () => {
72-
const input = filterOptionsByLabel('', options[0]);
73-
74-
expect(input).toBe(true);
79+
expect(filterOptionsByLabel('', option)).toBe(true);
7580
});
7681

7782
it('should return true when passed matching lowercase string', () => {
78-
const input = filterOptionsByLabel('test', options[0]);
79-
80-
expect(input).toBe(true);
83+
expect(filterOptionsByLabel('test', option)).toBe(true);
8184
});
8285

8386
it('should return true when passed matching uppercase string', () => {
84-
const input = filterOptionsByLabel('TEST', options[0]);
85-
86-
expect(input).toBe(true);
87+
expect(filterOptionsByLabel('TEST', option)).toBe(true);
8788
});
8889

8990
it('should return false when passed non-matching', () => {
90-
const input = filterOptionsByLabel('jaeger', options[0]);
91-
92-
expect(input).toBe(false);
91+
expect(filterOptionsByLabel('jaeger', option)).toBe(false);
9392
});
9493

9594
it('should return false when passed null option', () => {
96-
const input = filterOptionsByLabel('jaeger', null);
97-
98-
expect(input).toBe(false);
95+
expect(filterOptionsByLabel('jaeger', null)).toBe(false);
9996
});
10097
});

packages/jaeger-ui/src/components/common/TraceId.test.js

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

1515
import React from 'react';
16-
import { shallow } from 'enzyme';
16+
import { render, screen } from '@testing-library/react';
17+
import '@testing-library/jest-dom';
1718
import { TraceId } from './TraceId';
1819
import { getConfigValue } from '../../utils/config/get-config';
1920

@@ -26,8 +27,6 @@ describe('TraceIdDisplayLength', () => {
2627
const MOCK_TRACE_ID = '12345678901234567890';
2728
const CUSTOM_CLASS = 'custom-class';
2829

29-
let wrapper;
30-
3130
const defaultProps = {
3231
traceId: MOCK_TRACE_ID,
3332
};
@@ -36,26 +35,28 @@ describe('TraceIdDisplayLength', () => {
3635
jest.clearAllMocks();
3736
});
3837

39-
const createWrapper = (props = {}) => {
40-
return shallow(<TraceId {...defaultProps} {...props} />);
38+
const renderComponent = (props = {}) => {
39+
render(<TraceId {...defaultProps} {...props} />);
4140
};
4241

4342
describe('TraceIdDisplayLength Component', () => {
4443
it('renders the default traceIdLength 7', () => {
4544
getConfigValue.mockReturnValue(undefined);
46-
wrapper = createWrapper();
45+
renderComponent();
4746

48-
const displayedText = wrapper.text();
49-
expect(displayedText).toEqual(MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH));
47+
const displayed = MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH);
48+
expect(screen.getByText(displayed)).toBeInTheDocument();
49+
expect(screen.queryByText(MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH + 1))).not.toBeInTheDocument();
5050
});
5151

5252
it('renders the config length when provided', () => {
5353
const configuredLength = 5;
5454
getConfigValue.mockReturnValue(configuredLength);
55-
wrapper = createWrapper();
55+
renderComponent();
5656

57-
const displayedText = wrapper.text();
58-
expect(displayedText).toEqual(MOCK_TRACE_ID.slice(0, configuredLength));
57+
const displayed = MOCK_TRACE_ID.slice(0, configuredLength);
58+
expect(screen.getByText(displayed)).toBeInTheDocument();
59+
expect(screen.queryByText(MOCK_TRACE_ID.slice(0, configuredLength + 1))).not.toBeInTheDocument();
5960
});
6061
});
6162

@@ -65,36 +66,43 @@ describe('TraceIdDisplayLength', () => {
6566
const configuredLength = 10;
6667
getConfigValue.mockReturnValue(configuredLength);
6768

68-
wrapper = createWrapper({ traceId: shortTraceId });
69-
expect(wrapper.text()).toEqual(shortTraceId);
69+
renderComponent({ traceId: shortTraceId });
70+
expect(screen.getByText(shortTraceId)).toBeInTheDocument();
7071
});
7172

72-
it('renders when traceId is undefiend', () => {
73-
wrapper = createWrapper({ traceId: '' });
74-
expect(wrapper.text()).toEqual('');
73+
it('renders an empty <small> element when traceId is an empty string', () => {
74+
const { container } = render(<TraceId traceId="" />);
75+
const el = container.querySelector('small');
76+
expect(el).toBeInTheDocument();
77+
expect(el).toBeEmptyDOMElement();
7578
});
7679
});
7780

7881
describe('Style handling', () => {
7982
it('applies custom className when provided', () => {
80-
wrapper = createWrapper({ className: CUSTOM_CLASS });
81-
expect(wrapper.hasClass(CUSTOM_CLASS)).toBe(true);
83+
getConfigValue.mockReturnValue(undefined);
84+
renderComponent({ className: CUSTOM_CLASS });
85+
86+
const el = screen.getByText(MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH));
87+
expect(el).toHaveClass(CUSTOM_CLASS);
8288
});
8389

8490
it('default classes for styling', () => {
85-
wrapper = createWrapper();
86-
expect(wrapper.hasClass('TraceIDLength')).toBe(true);
87-
expect(wrapper.hasClass('u-tx-muted')).toBe(true);
91+
renderComponent();
92+
const el = screen.getByText(MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH));
93+
94+
expect(el).toHaveClass('TraceIDLength');
95+
expect(el).toHaveClass('u-tx-muted');
8896
});
8997

9098
it('adds a length-based class depending on the configuration', () => {
9199
getConfigValue.mockReturnValue(DEFAULT_LENGTH);
92-
wrapper = createWrapper();
93-
expect(wrapper.hasClass('TraceIDLength--short')).toBe(true);
100+
renderComponent();
101+
expect(screen.getByText(MOCK_TRACE_ID.slice(0, DEFAULT_LENGTH))).toHaveClass('TraceIDLength--short');
94102

95103
getConfigValue.mockReturnValue(32);
96-
wrapper = createWrapper();
97-
expect(wrapper.hasClass('TraceIDLength--full')).toBe(true);
104+
renderComponent();
105+
expect(screen.getByText(MOCK_TRACE_ID.slice(0, 32))).toHaveClass('TraceIDLength--full');
98106
});
99107
});
100108
});

packages/jaeger-ui/src/components/common/__snapshots__/SearchableSelect.test.js.snap

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)