Skip to content

Commit

Permalink
Add link to timeline view from comparison view and selection (#313)
Browse files Browse the repository at this point in the history
* Add link to TraceDiffHeader & CohortTable, Make DiffSelection clickable

Signed-off-by: Everett Ross <[email protected]>

* Use getUrl util, use NewWindowIcon

Signed-off-by: Everett Ross <[email protected]>

* Add test for TraceTimelineLink className

Signed-off-by: Everett Ross <[email protected]>

* Add DiffSelection, ResultItem, TraceHeader tests

Signed-off-by: Everett Ross <[email protected]>

* Fix typos

Signed-off-by: Everett Ross <[email protected]>

* Change NewWindowIcon large behavior, remove link header

Signed-off-by: Everett Ross <[email protected]>

* Fix typo, remove unused TraceTimelineLink className prop

Signed-off-by: Everett Ross <[email protected]>

* Update snapshots to match enzyme version

Signed-off-by: Everett Ross <[email protected]>
  • Loading branch information
everett980 authored and tiffon committed Jan 18, 2019
1 parent 6597593 commit 8e1a66e
Show file tree
Hide file tree
Showing 18 changed files with 767 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Link } from 'react-router-dom';

import ResultItemTitle from './ResultItemTitle';
import { getUrl } from '../../TraceDiff/url';
import { getUrl as getTracePageUrl } from '../../TracePage/url';
import { fetchedState } from '../../../constants';

import type { FetchedTrace } from '../../../types';
Expand All @@ -31,7 +32,8 @@ type Props = {
traces: FetchedTrace[],
};

const CTA_MESSAGE = <h2 className="ub-m0">Compare traces by selecting result items</h2>;
// Exported for tests
export const CTA_MESSAGE = <h2 className="ub-m0">Compare traces by selecting result items</h2>;

export default class DiffSelection extends React.PureComponent<Props> {
props: Props;
Expand All @@ -57,7 +59,9 @@ export default class DiffSelection extends React.PureComponent<Props> {
duration={data.duration}
error={error}
isInDiffCohort
linkTo={getTracePageUrl(id)}
state={state}
targetBlank
toggleComparison={toggleComparison}
traceID={id}
traceName={data.traceName}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import React from 'react';
import { shallow } from 'enzyme';

import DiffSelection, { CTA_MESSAGE } from './DiffSelection.js';
import ResultItemTitle from './ResultItemTitle';
import { fetchedState } from '../../../constants';

describe('DiffSelection', () => {
const toggleComparison = () => {};
const traces = [
{
id: 'trace-id-0',
data: {
duration: 0,
traceName: 'trace-name-0',
},
error: new Error('error-0'),
state: fetchedState.DONE,
},
{
id: 'trace-id-1',
// deliberately missing data to test default
error: new Error('error-1'),
state: fetchedState.DONE,
},
{
id: 'trace-id-2',
// deliberately missing data to test default
error: new Error('error-2'),
state: fetchedState.ERROR,
},
];

it('renders a trace as expected', () => {
const wrapper = shallow(
<DiffSelection traces={traces.slice(0, 1)} toggleComparison={toggleComparison} />
);

expect(wrapper.find(ResultItemTitle).length).toBe(1);
expect(wrapper).toMatchSnapshot();
});

it('renders multiple traces as expected', () => {
const wrapper = shallow(<DiffSelection traces={traces} toggleComparison={toggleComparison} />);

expect(wrapper.find(ResultItemTitle).length).toBe(traces.length);
expect(wrapper).toMatchSnapshot();
});

it('renders CTA_MESSAGE when given empty traces array', () => {
const wrapper = shallow(<DiffSelection traces={[]} toggleComparison={toggleComparison} />);
expect(wrapper.contains(CTA_MESSAGE)).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Props = {
isInDiffCohort: boolean,
linkTo: ?string,
state: ?FetchedState,
targetBlank?: boolean,
toggleComparison: (string, boolean) => void,
traceID: string,
traceName: string,
Expand All @@ -47,8 +48,9 @@ export default class ResultItemTitle extends React.PureComponent<Props> {
disableComparision: false,
durationPercent: 0,
error: undefined,
state: fetchedState.DONE,
linkTo: null,
targetBlank: false,
state: fetchedState.DONE,
};

toggleComparison = () => {
Expand All @@ -58,22 +60,27 @@ export default class ResultItemTitle extends React.PureComponent<Props> {

render() {
const {
disableComparision,
duration,
durationPercent,
error,
isInDiffCohort,
linkTo,
state,
targetBlank,
traceID,
traceName,
disableComparision,
} = this.props;
// Use a div when the ResultItemTitle doesn't link to anything
let WrapperComponent = 'div';
const wrapperProps: { [string]: string } = { className: 'ResultItemTitle--item ub-flex-auto' };
if (linkTo) {
WrapperComponent = Link;
wrapperProps.to = linkTo;
if (targetBlank) {
wrapperProps.target = '_blank';
wrapperProps.rel = 'noopener noreferrer';
}
}
const isErred = state === fetchedState.ERROR;
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import React from 'react';
import { Checkbox } from 'antd';
import { shallow } from 'enzyme';
import { Link } from 'react-router-dom';

import ResultItemTitle from './ResultItemTitle';
import { fetchedState } from '../../../constants';

describe('ResultItemTitle', () => {
const defaultProps = {
duration: 150,
durationPercent: 10,
isInDiffCohort: true,
linkTo: 'linkToValue',
toggleComparison: jest.fn(),
traceID: 'trace-id-longer-than-8',
traceName: 'traceNameValue',
};
let wrapper;

beforeEach(() => {
defaultProps.toggleComparison.mockReset();
wrapper = shallow(<ResultItemTitle {...defaultProps} />);
});

it('renders as expected', () => {
expect(wrapper).toMatchSnapshot();
});

describe('Checkbox', () => {
it('does not render toggleComparison checkbox when props.disableComparision is true', () => {
expect(wrapper.find(Checkbox).length).toBe(1);
wrapper.setProps({ disableComparision: true });
expect(wrapper.find(Checkbox).length).toBe(0);
});

it('is disabled iff props.state === fetchedState.ERROR', () => {
expect(wrapper.find(Checkbox).prop('disabled')).toBe(false);
wrapper.setProps({ state: fetchedState.ERROR });
expect(wrapper.find(Checkbox).prop('disabled')).toBe(true);
});

it('is checked iff props.state !== fetchedState.ERROR && props.isInDiffCohort', () => {
[true, false].forEach(isInDiffCohort => {
[fetchedState.ERROR, fetchedState.DONE].forEach(state => {
wrapper.setProps({ isInDiffCohort, state });
expect(wrapper.find(Checkbox).prop('checked')).toBe(state !== fetchedState.ERROR && isInDiffCohort);
});
});
});

it('calls props.toggleComparison with correct arguments onChange', () => {
wrapper.find(Checkbox).prop('onChange')();
expect(defaultProps.toggleComparison).toHaveBeenCalledWith(
defaultProps.traceID,
defaultProps.isInDiffCohort
);
wrapper.setProps({ isInDiffCohort: !defaultProps.isInDiffCohort });
wrapper.find(Checkbox).prop('onChange')();
expect(defaultProps.toggleComparison).toHaveBeenLastCalledWith(
defaultProps.traceID,
!defaultProps.isInDiffCohort
);
});
});

describe('WrapperComponent', () => {
it('renders <Link> when linkTo is provided', () => {
expect(wrapper.find(Link).length).toBe(1);
wrapper.setProps({ linkTo: null });
expect(wrapper.find(Link).length).toBe(0);
});

it('<Link> targets _blank and sets rel when targetBlank is true', () => {
expect(wrapper.find(Link).prop('target')).toBeUndefined();
expect(wrapper.find(Link).prop('rel')).toBeUndefined();
wrapper.setProps({ targetBlank: true });
expect(wrapper.find(Link).prop('target')).toBe('_blank');
expect(wrapper.find(Link).prop('rel')).toBe('noopener noreferrer');
});

it('hides formated duration when duration is not provided', () => {
const initialSpanCount = wrapper.find('span').length;
wrapper.setProps({ duration: null });
expect(wrapper.find('span').length).toBe(initialSpanCount - 1);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DiffSelection renders a trace as expected 1`] = `
<div
className="DiffSelection is-non-empty ub-mb3"
>
<div
className="DiffSelection--selectedItems"
>
<ResultItemTitle
disableComparision={false}
duration={0}
durationPercent={0}
error={[Error: error-0]}
isInDiffCohort={true}
key="trace-id-0"
linkTo="/trace/trace-id-0"
state="FETCH_DONE"
targetBlank={true}
toggleComparison={[Function]}
traceID="trace-id-0"
traceName="trace-name-0"
/>
</div>
<div
className="DiffSelection--message"
>
<React.Fragment>
<Button
block={false}
className="ub-right"
disabled={true}
ghost={false}
loading={false}
prefixCls="ant-btn"
type="primary"
>
Compare Traces
</Button>
<h2
className="ub-m0"
>
1
Selected for comparison
</h2>
</React.Fragment>
</div>
</div>
`;

exports[`DiffSelection renders multiple traces as expected 1`] = `
<div
className="DiffSelection is-non-empty ub-mb3"
>
<div
className="DiffSelection--selectedItems"
>
<ResultItemTitle
disableComparision={false}
duration={0}
durationPercent={0}
error={[Error: error-0]}
isInDiffCohort={true}
key="trace-id-0"
linkTo="/trace/trace-id-0"
state="FETCH_DONE"
targetBlank={true}
toggleComparison={[Function]}
traceID="trace-id-0"
traceName="trace-name-0"
/>
<ResultItemTitle
disableComparision={false}
durationPercent={0}
error={[Error: error-1]}
isInDiffCohort={true}
key="trace-id-1"
linkTo="/trace/trace-id-1"
state="FETCH_DONE"
targetBlank={true}
toggleComparison={[Function]}
traceID="trace-id-1"
/>
<ResultItemTitle
disableComparision={false}
durationPercent={0}
error={[Error: error-2]}
isInDiffCohort={true}
key="trace-id-2"
linkTo="/trace/trace-id-2"
state="FETCH_ERROR"
targetBlank={true}
toggleComparison={[Function]}
traceID="trace-id-2"
/>
</div>
<div
className="DiffSelection--message"
>
<React.Fragment>
<Link
replace={false}
to="/trace/trace-id-0...trace-id-1?cohort=trace-id-0&cohort=trace-id-1"
>
<Button
block={false}
className="ub-right"
disabled={false}
ghost={false}
loading={false}
prefixCls="ant-btn"
type="primary"
>
Compare Traces
</Button>
</Link>
<h2
className="ub-m0"
>
2
Selected for comparison
</h2>
</React.Fragment>
</div>
</div>
`;
Loading

0 comments on commit 8e1a66e

Please sign in to comment.