Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,48 @@
import React from "react";
import { Link } from "react-router-dom";
import { Link, useHistory } from "react-router-dom";
import { HeaderMain, RoundedButton } from "./HeaderStyled";
import { useAuth } from "../../context/AuthProvider";

import "./header.styles.css";

// interface iHeader {

// }

const Header = (): JSX.Element => {
const Header = () => {
const data = [
{ id: 1, title: "Day" },
{ id: 2, title: "Week" },
{ id: 3, title: "Month" },
{ id: 4, title: "Year" },
{ id: 5, title: "Five Year" },
];

const auth = useAuth();
const history = useHistory();

const logoutUser = () => {
auth.logout();
history.push("/");
}

const generateNavLinks = () => {
if (auth.user?.authenticated) {
return (<a onClick={logoutUser}>Sign out</a>)
}

return (
<>
<Link className="login" to="/login">
Login
</Link>
<Link className="signup" to="/signup">
Signup
</Link>
</>
);
};

return (
<HeaderMain dark={true}>
<div className="left">
Expand All @@ -32,12 +59,7 @@ const Header = (): JSX.Element => {
<Link to="/protected">
Protected
</Link>
<Link className="login" to="/login">
Login
</Link>
<Link className="signup" to="/signup">
Signup
</Link>
{generateNavLinks()}
</div>
</HeaderMain>
);
Expand Down
13 changes: 13 additions & 0 deletions src/components/Header/header.styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@
.signup {
margin-right: 40px;
}

.right > a {
color: white;
text-decoration: underline;
}

.right > a:visited{
color: white;
}

.right > a:hover{
cursor: pointer;
}
3 changes: 3 additions & 0 deletions src/components/Login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
import { useAuth } from "../../context/AuthProvider";
import type User from "../../types/User";
import './Login.css'
import { useHistory } from "react-router-dom";

import {
LoginForm,
Expand All @@ -22,6 +23,7 @@ const Login = () => {

const [form, setForm] = useState(formState);
const auth = useAuth();
const history = useHistory();

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
Expand All @@ -34,6 +36,7 @@ const Login = () => {
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
auth.login( form );
history.push("/");
};

return (
Expand Down
3 changes: 3 additions & 0 deletions src/components/Signup/Signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
import { useAuth } from "../../context/AuthProvider";
import type User from "../../types/User";
import "./Signup.css";
import { useHistory } from "react-router-dom";

const Signup = () => {
const formState: User = {
Expand All @@ -16,6 +17,7 @@ const Signup = () => {

const [form, setForm] = useState(formState);
const auth = useAuth();
const history = useHistory();

const handleChange = (event: ChangeEvent<HTMLInputElement>) => {
const { name, value } = event.target;
Expand All @@ -29,6 +31,7 @@ const Signup = () => {
// prevent page reload
event.preventDefault()
auth.signup( form );
history.push("/");
}

return (
Expand Down
19 changes: 19 additions & 0 deletions src/components/Signup/tests/Signup.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import Signup from "../../Signup";

describe("Signup component", () => {
test("pages renders correctly", () => {
const {queryByLabelText, queryByRole} = render(<Signup/>);
expect(queryByLabelText("Email")).toBeTruthy();
expect(queryByLabelText("Password")).toBeTruthy();
expect(queryByRole("button")).toBeVisible();
//TODO: check if button is rendered.
});
Comment on lines +6 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're just testing if elements are rendered into the DOM, you can use the same methods from screen. This avoids having to keep using destructuring queries from render(...). Kent Dodds talks about it here.

Since you're using jest-dom, you could change .toBeTruthy() to be toBeInTheDocument(), since it's a little more accurate in stating that those elements are rendered. The same for the button as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:+1 @shelbyhurns


test("page signs in user after signing up", () => {
render(<Signup/>);
//TODO: check for email and password inputs
//TODO: check if user is signed in
});
});