Skip to content

Commit

Permalink
refac: migrate TraceHeader from enzyme to RTL
Browse files Browse the repository at this point in the history
  • Loading branch information
EshaanAgg committed Nov 10, 2023
1 parent 2821fdb commit 6b2dc84
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,77 @@
// limitations under the License.

import * as React from 'react';
import { shallow } from 'enzyme';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import TraceHeader, { Attrs, EmptyAttrs } from './TraceHeader';
import { fetchedState } from '../../../constants';

describe('TraceHeader', () => {
const props = {
duration: 700,
error: { errorKey: 'errorValue' },
traceID: 'trace-id',
traceName: 'trace name',
};
let wrapper;
describe(`TraceHeader`, () => {
const renderWithProps = passedProps => {
const originalProps = {
duration: 700,
error: { errorKey: 'errorValue' },
traceID: 'trace-id',
traceName: 'trace name',
};

beforeEach(() => {
wrapper = shallow(<TraceHeader {...props} />);
});
const props = {
...originalProps,
...passedProps,
};

it('renders as expected', () => {
expect(wrapper).toMatchSnapshot();
});
render(<TraceHeader {...props} />);
};

it('renders populated attrs component when props.state === fetchedState.DONE', () => {
wrapper.setProps({
startTime: 150,
totalSpans: 50,
state: fetchedState.DONE,
it('renders as expected', () => {
renderWithProps({});
expect(screen.getAllByTestId('TraceDiffHeader--traceHeader').length).toBe(1);
});
expect(wrapper).toMatchSnapshot();
});

it('renders "Select a Trace..." when props.traceID is not provided ', () => {
wrapper.setProps({
traceID: null,
it('renders populated Attrs component when props.state === fetchedState.DONE', () => {
renderWithProps({
startTime: 150,
totalSpans: 50,
state: fetchedState.DONE,
});
expect(screen.getAllByTestId('TraceDiffHeader--traceAttr').length).toBe(3);
});

it('renders "Select a Trace..." when props.traceID is not provided ', () => {
renderWithProps({
traceID: null,
});
expect(screen.getByText('Select a Trace...'));
});
expect(wrapper.find('.u-tx-muted').text()).toBe('Select a Trace...');
});

describe('EmptyAttrs', () => {
it('renders as expected', () => {
expect(shallow(<EmptyAttrs />)).toMatchSnapshot();
render(<EmptyAttrs />);
expect(screen.getAllByTestId('TraceDiffHeader--traceAttr').length).toBe(1);
expect(screen.getByTestId('TraceDiffHeader--traceAttr').textContent.trim()).toBe('');
});
});

describe('Attrs', () => {
it('renders as expected when provided props', () => {
expect(shallow(<Attrs duration={700} startTime={150} totalSpans={50} />)).toMatchSnapshot();
render(<Attrs duration={700} startTime={150} totalSpans={50} />);

// Test that the shown values are correctly formatted
expect(screen.getByText('January 1, 1970, 12:00:00 am'));
expect(screen.getByTestId('TraceDiffHeader--traceAttr--duration').textContent).toBe('700μs');
expect(screen.getByTestId('TraceDiffHeader--traceAttr--spans').textContent).toBe('50');
});

it('Attrs renders as expected when missing props', () => {
expect(shallow(<Attrs />)).toMatchSnapshot();
render(<Attrs />);

// Test that the default values are correctly
expect(screen.getByText('January 1, 1970, 12:00:00 am'));
expect(screen.getByTestId('TraceDiffHeader--traceAttr--duration').textContent).toBe('0μs');
expect(screen.getByTestId('TraceDiffHeader--traceAttr--spans').textContent).toBe('0');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ type AttrsProps = {
// exported for tests
export function EmptyAttrs() {
return (
<ul className="TraecDiffHeader--traceAttributes">
<li className="TraecDiffHeader--traceAttr">&nbsp;</li>
<ul className="TraceDiffHeader--traceAttributes">
<li className="TraceDiffHeader--traceAttr" data-testid="TraceDiffHeader--traceAttr">
&nbsp;
</li>
</ul>
);
}
Expand All @@ -55,18 +57,19 @@ export function EmptyAttrs() {
export function Attrs(props: AttrsProps) {
const { startTime, duration, totalSpans } = props;
return (
<ul className="TraecDiffHeader--traceAttributes">
<li className="TraecDiffHeader--traceAttr">
<strong>
<ul className="TraceDiffHeader--traceAttributes">
<li className="TraceDiffHeader--traceAttr" data-testid="TraceDiffHeader--traceAttr">
<strong data-testid="TraceDiffHeader--traceAttr--date">
<RelativeDate value={(startTime || 0) / 1000} includeTime fullMonthName />
</strong>
</li>
<li className="TraecDiffHeader--traceAttr">
<li className="TraceDiffHeader--traceAttr" data-testid="TraceDiffHeader--traceAttr">
<span className="u-tx-muted">Duration: </span>
<strong>{formatDuration(duration || 0)}</strong>
<strong data-testid="TraceDiffHeader--traceAttr--duration">{formatDuration(duration || 0)}</strong>
</li>
<li className="TraecDiffHeader--traceAttr">
<span className="u-tx-muted">Spans: </span> <strong>{totalSpans || 0}</strong>
<li className="TraceDiffHeader--traceAttr" data-testid="TraceDiffHeader--traceAttr">
<span className="u-tx-muted">Spans: </span>{' '}
<strong data-testid="TraceDiffHeader--traceAttr--spans">{totalSpans || 0}</strong>
</li>
</ul>
);
Expand All @@ -75,9 +78,10 @@ export function Attrs(props: AttrsProps) {
export default function TraceHeader(props: Props) {
const { duration, error, startTime, state, traceID, totalSpans, traceName } = props;
const AttrsComponent = state === fetchedState.DONE ? Attrs : EmptyAttrs;

return (
<div className="TraecDiffHeader--traceHeader">
<h1 className="TraecDiffHeader--traceTitle">
<div className="TraceDiffHeader--traceHeader" data-testid="TraceDiffHeader--traceHeader">
<h1 className="TraceDiffHeader--traceTitle">
<span>
{traceID ? (
<React.Fragment>
Expand All @@ -91,7 +95,7 @@ export default function TraceHeader(props: Props) {
<span className="u-tx-muted">Select a Trace...</span>
)}
</span>
<IoChevronDown className="TraecDiffHeader--traceTitleChevron" />
<IoChevronDown className="TraceDiffHeader--traceTitleChevron" />
</h1>
<AttrsComponent startTime={startTime} duration={duration} totalSpans={totalSpans} />
</div>
Expand Down

This file was deleted.

0 comments on commit 6b2dc84

Please sign in to comment.