Skip to content

Commit

Permalink
chore: start rtl transition (#4635)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbo committed Oct 28, 2024
1 parent 78546af commit e83b92f
Show file tree
Hide file tree
Showing 22 changed files with 3,689 additions and 1,968 deletions.
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const overrides = [
node: true,
},
},
{
files: [
'**/*.test.js', '**/*.test.jsx',
],
env: {
'jest/globals': true,
},
plugins: ['jest'],
extends: ['airbnb', 'plugin:sonarjs/recommended'],
rules: { ...finalRules, 'react/jsx-props-no-spreading': 0 },
},
];

module.exports = {
Expand Down
234 changes: 118 additions & 116 deletions admin-client/yarn.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ci/tasks/test-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ set -euo pipefail
cd app

echo "we're rewriting our frontend tests!"
./scripts/wait-for-it.sh db:5432 -- yarn test:prepare && yarn test:server:cover ; status=$?
./scripts/wait-for-it.sh db:5432 -- yarn test; status=$?
exit $status
15 changes: 6 additions & 9 deletions frontend/pages/sites/$siteId/builds/CreateBuildLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import React from 'react';
import PropTypes from 'prop-types';
import { IconRebuild } from '@shared/icons';

function CreateBuildLink(props) {
const {
children, className, handleClick, handlerParams,
} = props;
function CreateBuildLink({
children,
handleClick,
className = 'usa-button',
handlerParams = {},
}) {
function localHandleClick(event) {
event.preventDefault();
const args = Object.keys(handlerParams).map(key => handlerParams[key]);
Expand Down Expand Up @@ -33,9 +35,4 @@ CreateBuildLink.propTypes = {
className: PropTypes.string,
};

CreateBuildLink.defaultProps = {
handlerParams: {},
className: 'usa-button',
};

export default CreateBuildLink;
28 changes: 28 additions & 0 deletions frontend/pages/sites/$siteId/builds/CreateBuildLink.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { spy } from 'sinon';
import CreateBuildLink from './CreateBuildLink';

describe('<CreateBuildLink />', () => {
const props = {
handlerParams: { dish: 'tacos', cuisine: 'mexican' },
handleClick: spy(),
children: 'hey there',
};

test('it renders', () => {
render(<CreateBuildLink {...props} />);
expect(screen.getByRole('button')).toBeInTheDocument();
});

test('it calls the .handleClick function, passing handler params', () => {
render(<CreateBuildLink {...props} />);
const handler = props.handleClick;
const params = props.handlerParams;

fireEvent.click(screen.getByRole('button'));
expect(handler.calledOnce).toBeTruthy();
expect(handler.calledWith(...Object.keys(params).map(key => params[key]))).toBeTruthy();
});
});
6 changes: 1 addition & 5 deletions frontend/shared/BranchViewLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const getViewLink = (branch, site) => {
return `https://${domain.names.split(',')[0]}`;
};

export const BranchViewLink = ({ branchName, site, showIcon }) => {
export const BranchViewLink = ({ branchName, site, showIcon = false }) => {
const domain = getViewLink(branchName, site);

return (
Expand All @@ -56,8 +56,4 @@ BranchViewLink.propTypes = {
showIcon: PropTypes.bool,
};

BranchViewLink.defaultProps = {
showIcon: false,
};

export default connect()(BranchViewLink);
107 changes: 107 additions & 0 deletions frontend/shared/BranchViewLink.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import { BranchViewLink } from './BranchViewLink';

const proxyDomain = process.env.PROXY_DOMAIN;

describe('<BranchViewLink/>', () => {
const awsBucketName = 'test-bucket';
const siteDomain = 'prod-url.com';
const demoDomain = 'demo-url.com';
const proxyOrigin = `https://${awsBucketName}.${proxyDomain}`;
const unprovisionedS3Key = '/this/is/unprovisoned/branch';

const defaultBranch = 'default-branch';
const demoBranch = 'demo-branch';
const VIEW_BUILD = 'View site preview';

const testSite = {
defaultBranch,
demoBranch,
domain: `https://${siteDomain}`,
demoDomain: `https://${demoDomain}`,
owner: 'test-owner',
repository: 'test-repo',
awsBucketName,
s3ServiceName: 'federalist-production-s3',
domains: [
{
names: siteDomain,
siteBranchConfigId: 123,
state: 'provisioned',
},
{
names: demoDomain,
siteBranchConfigId: 256,
state: 'provisioned',
},
{
names: 'unprovisioned.gov',
siteBranchConfigId: 411,
state: 'pending',
},
],
siteBranchConfigs: [
{
branch: defaultBranch,
id: 123,
},
{
branch: demoBranch,
id: 256,
},
{
s3Key: unprovisionedS3Key,
branch: 'unprovisioned-branch',
id: 411,
},
],
};

let props;

beforeEach(() => {
props = {
branchName: 'branch-name',
site: testSite,
};
});

it("renders a link to the default branch's site", () => {
props.branchName = 'default-branch';
render(<BranchViewLink {...props} />);
const anchor = screen.getByRole('link');
expect(anchor).toHaveAttribute('href', `https://${siteDomain}`);
expect(anchor).toHaveTextContent(VIEW_BUILD);
});

it("renders a link to the demo branch's site", () => {
props.branchName = 'demo-branch';
render(<BranchViewLink {...props} />);
const anchor = screen.getByRole('link');
expect(anchor).toHaveAttribute('href', `https://${demoDomain}`);
expect(anchor).toHaveTextContent(VIEW_BUILD);
});

it('renders the preview link to site branch when the domain is not provisioned', () => {
props.branchName = 'unprovisioned-branch';
render(<BranchViewLink {...props} />);
const anchor = screen.getByRole('link');
expect(anchor).toHaveAttribute('href', `${proxyOrigin}${unprovisionedS3Key}`);
expect(anchor).toHaveTextContent(VIEW_BUILD);
});

it('renders a preview link to the other branches', () => {
const branchName = 'some-other-branch';
const updatedProps = { ...props, branchName };
render(<BranchViewLink {...updatedProps} />);
const anchor = screen.getByRole('link');
expect(anchor).toHaveAttribute(
'href',
`${proxyOrigin}/preview/${testSite.owner}/${testSite.repository}/${branchName}`
);
expect(anchor).toHaveTextContent(VIEW_BUILD);
});
});
16 changes: 6 additions & 10 deletions frontend/shared/ExpandableArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import shortid from 'shortid';
// Based on the USWDS Accordion, but only ever has a single
// item that can be expanded or collapsed

function ExpandableArea(props) {
function ExpandableArea({
title,
bordered = false,
children = null,
isExpanded = false,
}) {
const id = `expandable-area-${shortid.generate()}`;
const {
bordered, children, isExpanded, title,
} = props;
return (
<div className={`usa-accordion ${bordered ? 'usa-accordion--bordered' : ''} width-full`}>
<div className="usa-accordion__heading">
Expand Down Expand Up @@ -40,10 +42,4 @@ ExpandableArea.propTypes = {
isExpanded: PropTypes.bool,
};

ExpandableArea.defaultProps = {
bordered: false,
children: null,
isExpanded: false,
};

export default ExpandableArea;
19 changes: 19 additions & 0 deletions frontend/shared/ExpandableArea.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import ExpandableArea from './ExpandableArea';

describe('<ExpandableArea/>', () => {
it('renders', () => {
const title = 'Test Title';
render(
<ExpandableArea title={title}>
<p>hello</p>
</ExpandableArea>
);

const button = screen.getByRole('button');
expect(button).toHaveTextContent(title);
});
});
13 changes: 2 additions & 11 deletions frontend/shared/GitHubLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { IconGitHub, IconBranch } from './icons';
const BASE = 'https://github.com';

const GitHubLink = ({
owner, repository, text, branch, sha, icon, isButton = false, ...props
owner, repository, text, branch = null, sha = null, icon = 'repo', isButton = false,
}) => {
const baseHref = `${BASE}/${owner}/${repository}`;

let { title } = props;
let href = baseHref;
let title = 'View repository on GitHub';

if (branch) {
href = `${baseHref}/tree/${encodeURIComponent(branch)}`;
Expand Down Expand Up @@ -60,16 +60,7 @@ GitHubLink.propTypes = {
branch: PropTypes.string,
isButton: PropTypes.bool,
sha: PropTypes.string,
title: PropTypes.string,
icon: PropTypes.string,
};

GitHubLink.defaultProps = {
branch: null,
isButton: false,
sha: null,
icon: 'repo',
title: 'View repository on GitHub',
};

export default GitHubLink;
62 changes: 62 additions & 0 deletions frontend/shared/GitHubLink.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import GitHubLink from './GitHubLink';

describe('<GitHubLink/>', () => {
it('renders', () => {
const props = { owner: 'owner', repository: 'a-repo', text: 'link text' };
render(<GitHubLink {...props} />);

const anchor = screen.getByRole('link');
expect(anchor).toHaveClass('repo-link');

expect(anchor).toHaveAttribute('href', 'https://github.com/owner/a-repo');
expect(anchor).toHaveAttribute('title', 'View repository on GitHub');
expect(anchor).toHaveTextContent('link text');

// TODO: actually render svg with https://react-svgr.com/docs/node-api/ in tests
// const icon = screen.getByTitle('icon-github');
// expect(icon).toBeInTheDocument();
});

it('can link to a branch', () => {
const props = {
text: 'link text', owner: 'pumpkin-pie', repository: 'candle', branch: 'the-branch',
};

render(<GitHubLink {...props} />);

const anchor = screen.getByRole('link');
expect(anchor).toHaveClass('repo-link');
expect(anchor).toHaveAttribute('href', 'https://github.com/pumpkin-pie/candle/tree/the-branch');
expect(anchor).toHaveAttribute('title', 'View branch on GitHub');
});

it('encodes the branch name', () => {
const props = {
text: 'boop', owner: 'spam', repository: 'potato', branch: '#-hash-#',
};
render(<GitHubLink {...props} />);

const anchor = screen.getByRole('link');
expect(anchor).toHaveClass('repo-link');
expect(anchor).toHaveAttribute('href', 'https://github.com/spam/potato/tree/%23-hash-%23');
});

it('links to a specific commit', () => {
const props = {
text: 'boop', owner: 'zookeeni', repository: 'veggies', sha: '123A',
};

render(<GitHubLink {...props} />);

const anchor = screen.getByRole('link');
expect(anchor).toHaveClass('repo-link');

const commitUrl = `https://github.com/${props.owner}/${props.repository}/commit/${props.sha}`;

expect(anchor).toHaveAttribute('href', commitUrl);
});
});
12 changes: 12 additions & 0 deletions frontend/shared/LoadingIndicator.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import LoadingIndicator from './LoadingIndicator';

describe('<LoadingIndicator/>', () => {
it('renders', () => {
render(<LoadingIndicator />);
screen.getByText('Loading...');
});
});
7 changes: 1 addition & 6 deletions frontend/shared/SelectSiteEngine.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function makeOptions(opts) {
}

const SelectSiteEngine = ({
value, onChange, name, id, className,
value, onChange, name, id, className = '',
}) => (
<select
className={`usa-select ${className}`}
Expand All @@ -47,9 +47,4 @@ SelectSiteEngine.propTypes = {
className: PropTypes.string,
};

SelectSiteEngine.defaultProps = {
className: '',
onChange: () => {},
};

export default SelectSiteEngine;
Loading

0 comments on commit e83b92f

Please sign in to comment.