Skip to content

Commit

Permalink
refactor: create Header component
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsojramos committed Jun 14, 2024
1 parent 3cd3a4e commit 814b2f0
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 98 deletions.
26 changes: 26 additions & 0 deletions src/components/Header.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { fireEvent, render, screen } from '@testing-library/react';
import { Header } from './Header';

const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));

describe('components/Header.tsx', () => {
it('should render itself & its children', () => {
const tree = render(<Header>Test Header</Header>);

expect(tree).toMatchSnapshot();
});

it('should navigate back', () => {
render(<Header>Test Header</Header>);

fireEvent.click(screen.getByLabelText('Go Back'));
expect(mockNavigate).toHaveBeenNthCalledWith(1, -1);

expect(mockNavigate).toHaveBeenCalledTimes(1);
expect(mockNavigate).toHaveBeenCalledWith(-1);
});
});
29 changes: 29 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ArrowLeftIcon } from '@primer/octicons-react';
import type { ReactNode } from 'react';
import { useNavigate } from 'react-router-dom';

interface IHeader {
children: ReactNode;
}

export const Header = ({ children }: IHeader) => {
const navigate = useNavigate();
return (
<div className="mx-8 mt-4 flex items-center justify-between py-2">
<button
type="button"
className="focus:outline-none"
title="Go Back"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon
size={20}
className="hover:text-gray-400"
aria-label="Go Back"
/>
</button>

<h3 className="text-lg font-semibold">{children}</h3>
</div>
);
};
124 changes: 124 additions & 0 deletions src/components/__snapshots__/Header.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 3 additions & 21 deletions src/routes/Accounts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ArrowLeftIcon,
KeyIcon,
PersonIcon,
PlusIcon,
Expand All @@ -8,11 +7,10 @@ import {

import { type FC, useCallback, useContext } from 'react';
import { useNavigate } from 'react-router-dom';

import { AppContext } from '../context/App';

import { Header } from '../components/Header';
import { AuthMethodIcon } from '../components/icons/AuthMethodIcon';
import { PlatformIcon } from '../components/icons/PlatformIcon';
import { AppContext } from '../context/App';
import { BUTTON_CLASS_NAME } from '../styles/gitify';
import type { Account } from '../types';
import { getAccountUUID } from '../utils/auth/utils';
Expand Down Expand Up @@ -47,23 +45,7 @@ export const AccountsRoute: FC = () => {

return (
<div className="flex h-screen flex-col" data-testid="accounts">
<div className="mx-8 mt-2 flex items-center justify-between py-2">
<button
type="button"
className="focus:outline-none"
title="Go Back"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon
size={20}
className="hover:text-gray-400"
aria-label="Go Back"
/>
</button>

<h3 className="text-lg font-semibold">Accounts</h3>
</div>

<Header>Accounts</Header>
<div className="flex-grow overflow-x-auto px-8">
<div className="mt-4 flex flex-col text-sm">
{auth.accounts.map((account) => (
Expand Down
34 changes: 6 additions & 28 deletions src/routes/LoginWithOAuthApp.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {
ArrowLeftIcon,
BookIcon,
PersonIcon,
SignInIcon,
} from '@primer/octicons-react';
import { BookIcon, PersonIcon, SignInIcon } from '@primer/octicons-react';
import { type FC, useCallback, useContext } from 'react';
import { Form, type FormRenderProps } from 'react-final-form';
import { useNavigate } from 'react-router-dom';
import { Header } from '../components/Header';
import { Button } from '../components/fields/Button';
import { FieldInput } from '../components/fields/FieldInput';
import { AppContext } from '../context/App';
Expand Down Expand Up @@ -58,7 +53,6 @@ export const validate = (values: IValues): IFormErrors => {

export const LoginWithOAuthApp: FC = () => {
const { loginWithOAuthApp } = useContext(AppContext);
const navigate = useNavigate();

const renderForm = (formProps: FormRenderProps) => {
const { handleSubmit, submitting, pristine, values } = formProps;
Expand Down Expand Up @@ -130,26 +124,10 @@ export const LoginWithOAuthApp: FC = () => {

return (
<div>
<div className="mx-8 mt-4 flex items-center justify-between py-2">
<button
type="button"
className="focus:outline-none"
title="Go Back"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon
size={20}
className="hover:text-gray-400"
aria-label="Go Back"
/>
</button>

<h3 className="justify-center text-lg font-semibold">
<PersonIcon size={20} className="mr-2" />
Login with OAuth App
</h3>
</div>

<Header>
<PersonIcon size={20} className="mr-2" />
Login with OAuth App
</Header>
<div className="px-8">
<Form
initialValues={{
Expand Down
31 changes: 6 additions & 25 deletions src/routes/LoginWithPersonalAccessToken.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
ArrowLeftIcon,
BookIcon,
KeyIcon,
SignInIcon,
} from '@primer/octicons-react';
import { BookIcon, KeyIcon, SignInIcon } from '@primer/octicons-react';
import { type FC, useCallback, useContext, useState } from 'react';
import { Form, type FormRenderProps } from 'react-final-form';
import { useNavigate } from 'react-router-dom';
import { Header } from '../components/Header';
import { Button } from '../components/fields/Button';
import { FieldInput } from '../components/fields/FieldInput';
import { AppContext } from '../context/App';
Expand Down Expand Up @@ -138,25 +134,10 @@ export const LoginWithPersonalAccessToken: FC = () => {

return (
<div>
<div className="mx-8 mt-4 flex items-center justify-between py-2">
<button
type="button"
className="focus:outline-none"
title="Go Back"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon
size={20}
className="hover:text-gray-400"
aria-label="Go Back"
/>
</button>

<h3 className="justify-center text-lg font-semibold">
<KeyIcon size={18} className="mr-2" />
Login with Personal Access Token
</h3>
</div>
<Header>
<KeyIcon size={18} className="mr-2" />
Login with Personal Access Token
</Header>

<div className="px-8">
<Form
Expand Down
20 changes: 2 additions & 18 deletions src/routes/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
ArrowLeftIcon,
CheckIcon,
CommentIcon,
IssueClosedIcon,
Expand All @@ -17,6 +16,7 @@ import {
useState,
} from 'react';
import { useNavigate } from 'react-router-dom';
import { Header } from '../components/Header';
import { Checkbox } from '../components/fields/Checkbox';
import { RadioGroup } from '../components/fields/RadioGroup';
import { AppContext } from '../context/App';
Expand Down Expand Up @@ -55,23 +55,7 @@ export const SettingsRoute: FC = () => {

return (
<div className="flex h-screen flex-col" data-testid="settings">
<div className="mx-8 mt-2 flex items-center justify-between py-2">
<button
type="button"
className="focus:outline-none"
title="Go Back"
onClick={() => navigate(-1)}
>
<ArrowLeftIcon
size={20}
className="hover:text-gray-400"
aria-label="Go Back"
/>
</button>

<h3 className="text-lg font-semibold">Settings</h3>
</div>

<Header>Settings</Header>
<div className="flex-grow overflow-x-auto px-8">
<fieldset className="mb-3">
<legend id="appearance" className="mb-1 mt-2 font-semibold">
Expand Down
2 changes: 1 addition & 1 deletion src/routes/__snapshots__/Accounts.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/routes/__snapshots__/LoginWithOAuthApp.test.tsx.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 814b2f0

Please sign in to comment.