Created VisitsHeader test

This commit is contained in:
Alejandro Celaya 2018-09-08 09:31:44 +02:00
parent eb0f219403
commit d37e7ca7ce
4 changed files with 53 additions and 14 deletions

View file

@ -11,7 +11,7 @@ export default function ExternalLink(props) {
return ( return (
<a target="_blank" rel="noopener noreferrer" href={href} {...rest}> <a target="_blank" rel="noopener noreferrer" href={href} {...rest}>
{children} {children || href}
</a> </a>
); );
} }

View file

@ -7,7 +7,6 @@ import { Card } from 'reactstrap';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import DateInput from '../common/DateInput'; import DateInput from '../common/DateInput';
import MutedMessage from '../utils/MuttedMessage'; import MutedMessage from '../utils/MuttedMessage';
import { serverType } from '../servers/prop-types/index';
import { getShortUrlVisits, shortUrlVisitsType } from './reducers/shortUrlVisits'; import { getShortUrlVisits, shortUrlVisitsType } from './reducers/shortUrlVisits';
import { import {
processBrowserStats, processBrowserStats,
@ -27,7 +26,6 @@ export class ShortUrlsVisitsComponent extends React.Component {
processCountriesStats: PropTypes.func, processCountriesStats: PropTypes.func,
processReferrersStats: PropTypes.func, processReferrersStats: PropTypes.func,
match: PropTypes.object, match: PropTypes.object,
selectedServer: serverType,
getShortUrlVisits: PropTypes.func, getShortUrlVisits: PropTypes.func,
shortUrlVisits: shortUrlVisitsType, shortUrlVisits: shortUrlVisitsType,
getShortUrlDetail: PropTypes.func, getShortUrlDetail: PropTypes.func,
@ -59,8 +57,6 @@ export class ShortUrlsVisitsComponent extends React.Component {
render() { render() {
const { const {
match: { params },
selectedServer,
processOsStats, processOsStats,
processBrowserStats, processBrowserStats,
processCountriesStats, processCountriesStats,
@ -68,8 +64,6 @@ export class ShortUrlsVisitsComponent extends React.Component {
shortUrlVisits, shortUrlVisits,
shortUrlDetail, shortUrlDetail,
} = this.props; } = this.props;
const serverUrl = selectedServer ? selectedServer.url : '';
const shortLink = `${serverUrl}/${params.shortCode}`;
const renderVisitsContent = () => { const renderVisitsContent = () => {
const { visits, loading, error } = shortUrlVisits; const { visits, loading, error } = shortUrlVisits;
@ -110,7 +104,7 @@ export class ShortUrlsVisitsComponent extends React.Component {
return ( return (
<div className="shlink-container"> <div className="shlink-container">
<VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} shortLink={shortLink} /> <VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} />
<section className="mt-4"> <section className="mt-4">
<div className="row"> <div className="row">
@ -145,7 +139,7 @@ export class ShortUrlsVisitsComponent extends React.Component {
} }
const ShortUrlsVisits = connect( const ShortUrlsVisits = connect(
pick([ 'selectedServer', 'shortUrlVisits', 'shortUrlDetail' ]), pick([ 'shortUrlVisits', 'shortUrlDetail' ]),
{ getShortUrlVisits, getShortUrlDetail } { getShortUrlVisits, getShortUrlDetail }
)(ShortUrlsVisitsComponent); )(ShortUrlsVisitsComponent);

View file

@ -1,7 +1,6 @@
import { Card, UncontrolledTooltip } from 'reactstrap'; import { Card, UncontrolledTooltip } from 'reactstrap';
import Moment from 'react-moment'; import Moment from 'react-moment';
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import ExternalLink from '../utils/ExternalLink'; import ExternalLink from '../utils/ExternalLink';
import './VisitsHeader.scss'; import './VisitsHeader.scss';
import { shortUrlDetailType } from './reducers/shortUrlDetail'; import { shortUrlDetailType } from './reducers/shortUrlDetail';
@ -10,12 +9,14 @@ import { shortUrlVisitsType } from './reducers/shortUrlVisits';
const propTypes = { const propTypes = {
shortUrlDetail: shortUrlDetailType.isRequired, shortUrlDetail: shortUrlDetailType.isRequired,
shortUrlVisits: shortUrlVisitsType.isRequired, shortUrlVisits: shortUrlVisitsType.isRequired,
shortLink: PropTypes.string,
}; };
export function VisitsHeader({ shortUrlDetail, shortUrlVisits, shortLink }) { export function VisitsHeader({ shortUrlDetail, shortUrlVisits }) {
const { shortUrl, loading } = shortUrlDetail; const { shortUrl, loading } = shortUrlDetail;
const { visits } = shortUrlVisits; const { visits } = shortUrlVisits;
const shortLink = shortUrl && shortUrl.shortUrl ? shortUrl.shortUrl : '';
const longLink = shortUrl && shortUrl.longUrl ? shortUrl.longUrl : '';
const renderDate = () => ( const renderDate = () => (
<span> <span>
<b id="created" className="visits-header__created-at"><Moment fromNow>{shortUrl.dateCreated}</Moment></b> <b id="created" className="visits-header__created-at"><Moment fromNow>{shortUrl.dateCreated}</Moment></b>
@ -30,7 +31,7 @@ export function VisitsHeader({ shortUrlDetail, shortUrlVisits, shortLink }) {
<Card className="bg-light" body> <Card className="bg-light" body>
<h2> <h2>
<span className="badge badge-main float-right">Visits: {visits.length}</span> <span className="badge badge-main float-right">Visits: {visits.length}</span>
Visit stats for <ExternalLink href={shortLink}>{shortLink}</ExternalLink> Visit stats for <ExternalLink href={shortLink} />
</h2> </h2>
<hr /> <hr />
{shortUrl.dateCreated && ( {shortUrl.dateCreated && (
@ -44,7 +45,7 @@ export function VisitsHeader({ shortUrlDetail, shortUrlVisits, shortLink }) {
Long URL: Long URL:
&nbsp; &nbsp;
{loading && <small>Loading...</small>} {loading && <small>Loading...</small>}
{!loading && <ExternalLink href={shortUrl.longUrl}>{shortUrl.longUrl}</ExternalLink>} {!loading && <ExternalLink href={longLink} />}
</div> </div>
</Card> </Card>
</header> </header>

View file

@ -0,0 +1,44 @@
import React from 'react';
import { shallow } from 'enzyme';
import Moment from 'react-moment';
import { VisitsHeader } from '../../src/visits/VisitsHeader';
import ExternalLink from '../../src/utils/ExternalLink';
describe('<VisitsHeader />', () => {
let wrapper;
const shortUrlDetail = {
shortUrl: {
longUrl: 'https://foo.bar/bar/foo',
dateCreated: '2018-01-01T10:00:00+01:00',
},
loading: false,
};
const shortUrlVisits = {
visits: [{}, {}, {}],
};
beforeEach(() => {
wrapper = shallow(
<VisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} shortLink="foo" />
);
});
afterEach(() => wrapper.unmount());
it('shows the amount of visits', () => {
const visitsBadge = wrapper.find('.badge');
expect(visitsBadge.text()).toEqual(`Visits: ${shortUrlVisits.visits.length}`);
});
it('shows when the URL was created', () => {
const moment = wrapper.find(Moment).first();
expect(moment.prop('children')).toEqual(shortUrlDetail.shortUrl.dateCreated);
});
it('shows the long URL', () => {
const longUrlLink = wrapper.find(ExternalLink).last();
expect(longUrlLink.prop('href')).toEqual(shortUrlDetail.shortUrl.longUrl);
});
});