Merge pull request #669 from acelaya-forks/feature/rtl

Feature/rtl
This commit is contained in:
Alejandro Celaya 2022-06-11 17:12:29 +02:00 committed by GitHub
commit f9909713d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 65 deletions

View file

@ -35,7 +35,7 @@ export const ShortUrlVisitsHeader = ({ shortUrlDetail, shortUrlVisits, goBack }:
<VisitsHeader title={visitsStatsTitle} goBack={goBack} visits={visits} shortUrl={shortUrl}> <VisitsHeader title={visitsStatsTitle} goBack={goBack} visits={visits} shortUrl={shortUrl}>
<hr /> <hr />
<div>Created: {renderDate()}</div> <div>Created: {renderDate()}</div>
<div> <div className="long-url-container">
{`${title ? 'Title' : 'Long URL'}: `} {`${title ? 'Title' : 'Long URL'}: `}
{loading && <small>Loading...</small>} {loading && <small>Loading...</small>}
{!loading && <ExternalLink href={longLink}>{title ?? longLink}</ExternalLink>} {!loading && <ExternalLink href={longLink}>{title ?? longLink}</ExternalLink>}

View file

@ -1,20 +1,28 @@
import { shallow } from 'enzyme'; /* eslint-disable no-console */
import { Card, Nav } from 'reactstrap'; import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import { NavPillItem, NavPills } from '../../src/utils/NavPills'; import { NavPillItem, NavPills } from '../../src/utils/NavPills';
describe('<NavPills />', () => { describe('<NavPills />', () => {
let originalError: typeof console.error;
beforeEach(() => {
originalError = console.error;
console.error = () => {}; // Suppress errors logged during this test
});
afterEach(() => {
console.error = originalError;
});
it.each([ it.each([
['Foo'], ['Foo'],
[<span key="1">Hi!</span>], [<span key="1">Hi!</span>],
[[<NavPillItem key="1" to="" />, <span key="2">Hi!</span>]], [[<NavPillItem key="1" to="" />, <span key="2">Hi!</span>]],
])('throws error when any of the children is not a NavPillItem', (children) => { ])('throws error when any of the children is not a NavPillItem', (children) => {
expect.assertions(1); expect.assertions(1);
expect(() => render(<NavPills>{children}</NavPills>)).toThrow(
try { 'Only NavPillItem children are allowed inside NavPills.',
shallow(<NavPills>{children}</NavPills>); );
} catch (e: any) {
expect(e.message).toEqual('Only NavPillItem children are allowed inside NavPills.');
}
}); });
it.each([ it.each([
@ -22,20 +30,27 @@ describe('<NavPills />', () => {
[true], [true],
[false], [false],
])('renders provided items', (fill) => { ])('renders provided items', (fill) => {
const wrapper = shallow( const { container } = render(
<NavPills fill={fill}> <MemoryRouter>
<NavPillItem to="1">1</NavPillItem> <NavPills fill={fill}>
<NavPillItem to="2">2</NavPillItem> <NavPillItem to="1">1</NavPillItem>
<NavPillItem to="3">3</NavPillItem> <NavPillItem to="2">2</NavPillItem>
</NavPills>, <NavPillItem to="3">3</NavPillItem>
</NavPills>
</MemoryRouter>,
); );
const card = wrapper.find(Card);
const nav = wrapper.find(Nav);
expect(card).toHaveLength(1); const links = screen.getAllByRole('link');
expect(card.prop('body')).toEqual(true); expect(links).toHaveLength(3);
expect(nav).toHaveLength(1); links.forEach((link, index) => {
expect(nav.prop('pills')).toEqual(true); expect(link).toHaveTextContent(`${index + 1}`);
expect(nav.prop('fill')).toEqual(!!fill); expect(link).toHaveAttribute('href', `/${index + 1}`);
});
if (fill) {
expect(container.querySelector('.nav')).toHaveClass('nav-fill');
} else {
expect(container.querySelector('.nav')).not.toHaveClass('nav-fill');
}
}); });
}); });

View file

@ -1,20 +1,19 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen, waitFor } from '@testing-library/react';
import { ExternalLink } from 'react-external-link'; import userEvent from '@testing-library/user-event';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { formatDistance, parseISO } from 'date-fns';
import { ShortUrlVisitsHeader } from '../../src/visits/ShortUrlVisitsHeader'; import { ShortUrlVisitsHeader } from '../../src/visits/ShortUrlVisitsHeader';
import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail'; import { ShortUrlDetail } from '../../src/short-urls/reducers/shortUrlDetail';
import { ShortUrlVisits } from '../../src/visits/reducers/shortUrlVisits'; import { ShortUrlVisits } from '../../src/visits/reducers/shortUrlVisits';
import { Time } from '../../src/utils/Time';
describe('<ShortUrlVisitsHeader />', () => { describe('<ShortUrlVisitsHeader />', () => {
let wrapper: ShallowWrapper; const dateCreated = '2018-01-01T10:00:00+00:00';
const dateCreated = '2018-01-01T10:00:00+01:00';
const longUrl = 'https://foo.bar/bar/foo'; const longUrl = 'https://foo.bar/bar/foo';
const shortUrlVisits = Mock.of<ShortUrlVisits>({ const shortUrlVisits = Mock.of<ShortUrlVisits>({
visits: [{}, {}, {}], visits: [{}, {}, {}],
}); });
const goBack = jest.fn(); const goBack = jest.fn();
const createWrapper = (title?: string | null) => { const setUp = (title?: string | null) => {
const shortUrlDetail = Mock.of<ShortUrlDetail>({ const shortUrlDetail = Mock.of<ShortUrlDetail>({
shortUrl: { shortUrl: {
shortUrl: 'https://doma.in/abc123', shortUrl: 'https://doma.in/abc123',
@ -24,32 +23,32 @@ describe('<ShortUrlVisitsHeader />', () => {
}, },
loading: false, loading: false,
}); });
return {
wrapper = shallow( user: userEvent.setup(),
<ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />, ...render(
); <ShortUrlVisitsHeader shortUrlDetail={shortUrlDetail} shortUrlVisits={shortUrlVisits} goBack={goBack} />,
),
return wrapper; };
}; };
beforeEach(() => createWrapper()); it('shows when the URL was created', async () => {
afterEach(() => wrapper.unmount()); const { user } = setUp();
const dateElement = screen.getByText(`${formatDistance(new Date(), parseISO(dateCreated))} ago`);
it('shows when the URL was created', () => { expect(dateElement).toBeInTheDocument();
const time = wrapper.find(Time).first(); expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
await user.hover(dateElement);
expect(time.prop('date')).toEqual(dateCreated); await waitFor(() => expect(screen.getByRole('tooltip')).toHaveTextContent('2018-01-01 10:00'));
}); });
it.each([ it.each([
[null, longUrl], [null, `Long URL: ${longUrl}`],
[undefined, longUrl], [undefined, `Long URL: ${longUrl}`],
['My cool title', 'My cool title'], ['My cool title', 'Title: My cool title'],
])('shows the long URL and title', (title, expectedContent) => { ])('shows the long URL and title', (title, expectedContent) => {
const wrapper = createWrapper(title); const { container } = setUp(title);
const longUrlLink = wrapper.find(ExternalLink).last();
expect(longUrlLink.prop('href')).toEqual(longUrl); expect(container.querySelector('.long-url-container')).toHaveTextContent(expectedContent);
expect(longUrlLink.prop('children')).toEqual(expectedContent); expect(screen.getByRole('link', { name: title ?? longUrl })).toHaveAttribute('href', longUrl);
}); });
}); });

View file

@ -1,35 +1,27 @@
import { shallow, ShallowWrapper } from 'enzyme'; import { render, screen } from '@testing-library/react';
import { Mock } from 'ts-mockery'; import { Mock } from 'ts-mockery';
import { Tag } from '../../src/tags/helpers/Tag';
import { TagVisitsHeader } from '../../src/visits/TagVisitsHeader'; import { TagVisitsHeader } from '../../src/visits/TagVisitsHeader';
import { TagVisits } from '../../src/visits/reducers/tagVisits'; import { TagVisits } from '../../src/visits/reducers/tagVisits';
import { ColorGenerator } from '../../src/utils/services/ColorGenerator'; import { ColorGenerator } from '../../src/utils/services/ColorGenerator';
describe('<TagVisitsHeader />', () => { describe('<TagVisitsHeader />', () => {
let wrapper: ShallowWrapper;
const tagVisits = Mock.of<TagVisits>({ const tagVisits = Mock.of<TagVisits>({
tag: 'foo', tag: 'foo',
visits: [{}, {}, {}], visits: [{}, {}, {}, {}],
}); });
const goBack = jest.fn(); const goBack = jest.fn();
const colorGenerator = Mock.of<ColorGenerator>({ isColorLightForKey: () => false, getColorForKey: () => 'red' });
beforeEach(() => { const setUp = () => render(<TagVisitsHeader tagVisits={tagVisits} goBack={goBack} colorGenerator={colorGenerator} />);
wrapper = shallow(
<TagVisitsHeader tagVisits={tagVisits} goBack={goBack} colorGenerator={Mock.all<ColorGenerator>()} />,
);
});
afterEach(() => wrapper.unmount());
it('shows expected visits', () => { it('shows expected visits', () => {
expect(wrapper.prop('visits')).toEqual(tagVisits.visits); const { container } = setUp();
expect(screen.getAllByText('Visits for')).toHaveLength(2);
expect(container.querySelector('.badge:not(.tag)')).toHaveTextContent(`Visits: ${tagVisits.visits.length}`);
}); });
it('shows title for tag', () => { it('shows title for tag', () => {
const title = shallow(wrapper.prop('title')); const { container } = setUp();
const tag = title.find(Tag).first(); expect(container.querySelector('.badge.tag')).toHaveTextContent(tagVisits.tag);
expect(tag.prop('text')).toEqual(tagVisits.tag);
title.unmount();
}); });
}); });