Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async testing #21

Merged
merged 6 commits into from
Dec 7, 2020
Merged
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
45 changes: 25 additions & 20 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,12 @@ class App extends Component {
super();
this.state = {
movies: [],
selectedMovie: 0,
selectedMovie: null,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can the initial value for this be an empty object instead of null? Always best to avoid mutating the data type.

error: "",
loader: true,
};
}

componentDidMount() {
getMovies()
.then((response) => this.setState({ movies: response.movies }))
.then(this.setState({ loader: false }))
.catch((err) => this.setState({ error: err }));
}

returnToHome = (event) => {
this.setState({
selectedMovie: 0,
});
};

handleClick = (event) => {
getMovie(event.target.id)
.then((response) => this.setState({ selectedMovie: response.movie }))
.catch((err) => this.setState({ error: err }));
};

render() {
if (this.state.loader) {
return (
Expand All @@ -55,6 +36,11 @@ class App extends Component {
/>
</Col>
</Row>
<Row className="d-flex justify-content-center">
<Col md="auto">
{this.state.error && <Alert variant="danger">{this.state.error}</Alert>}
Copy link
Owner

Choose a reason for hiding this comment

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

conditional rendering ftw!

</Col>
</Row>
</Container>
);
}
Expand Down Expand Up @@ -91,6 +77,25 @@ class App extends Component {
</React.Fragment>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fun fact: you can create a react fragment with the shorthand <> </>

Copy link
Owner

Choose a reason for hiding this comment

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

that is a fun fact!

);
}

componentDidMount() {
getMovies()
.then((response) => this.setState({ movies: response.movies }))
.then(this.setState({ loader: false }))
Copy link
Collaborator

Choose a reason for hiding this comment

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

If the loading message is dependent solely on if there are movies rendered or not, we can remove this part of state and just check on this.state.moves! However if more than one condition can set this to true, then it can stay.

Copy link
Owner

Choose a reason for hiding this comment

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

duly noted!

.catch((err) => this.setState({ error: err }));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Be sure that "err" is a string as the initial state indicates, and not an object with an error message inside it.

}

returnToHome = (event) => {
this.setState({
selectedMovie: 0,
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like the default value of selectedMovie should be 0 instead of null!

});
};

handleClick = (event) => {
getMovie(event.target.id)
.then((response) => this.setState({ selectedMovie: response.movie }))
.catch((err) => this.setState({ error: err }));
};
}

export default App;
2 changes: 1 addition & 1 deletion src/App.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.App {
text-align: center;
}
}
1 change: 1 addition & 0 deletions src/App.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import App from "./App";
Expand Down
4 changes: 2 additions & 2 deletions src/Components/FeaturedMovies/FeaturedMovies.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ function FeaturedMovies() {
<img
className="d-block w-100"
src="https://images.unsplash.com/photo-1521967906867-14ec9d64bee8?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80"
alt="This bitch"
alt="bed eater"
/>

<Carousel.Caption>
<h3>This bitch</h3>
<h3>Don't eat in the bed</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</Carousel.Caption>
</Carousel.Item>
Expand Down
60 changes: 57 additions & 3 deletions src/Components/MovieCard/MovieCard.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
//it should display a movie poster
import React from "react";
import { render, screen } from "@testing-library/react";
import MovieCard from "./MovieCard";
import userEvent from "@testing-library/user-event";

//it should display a movie title
const handleClick = jest.fn();
const mockMovieCard = {
movies: [
{
id: 1,
title: "Movie Title",
poster_path: "someURL",
backdrop_path: "someURL",
release_date: "2019-12-04",
overview: "Some overview",
average_rating: 6,
},
{
id: 2,
title: "Second Movie Title",
poster_path: "someURL",
backdrop_path: "someURL",
release_date: "2019-12-04",
overview: "Some overview",
average_rating: 4,
},
],
};

// should be able to click on a movie card
Copy link
Collaborator

Choose a reason for hiding this comment

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

I LOVE the pseudocoding! This is a great workflow!

describe("MovieCard", () => {
it("should display a poster, title, release Date, and rating", () => {
render(
<MovieCard
poster={mockMovieCard.movies[0].poster_path}
title={mockMovieCard.movies[0].title}
release={mockMovieCard.movies[0].release_date}
rating={mockMovieCard.movies[0].average_rating}
/>
);
const movieCardTitle = screen.getByText("Movie Title");
const movieCardPoster = screen.getByRole("img");
const movieCardReleaseDate = screen.getByText("Released: 2019-12-04");
const movieCardRating = screen.getByText("Average Rating: 6");

expect(movieCardPoster).toBeInTheDocument();
expect(movieCardTitle).toBeInTheDocument();
expect(movieCardReleaseDate).toBeInTheDocument();
expect(movieCardRating).toBeInTheDocument();
});

it("should invoke a function when a movie card is clicked", () => {
render(<MovieCard handleClick={handleClick} />);
const movieCardPoster = screen.getByRole("img");

userEvent.click(movieCardPoster);

expect(handleClick).toHaveBeenCalled();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Great unit test

});
});
28 changes: 28 additions & 0 deletions src/Components/NavBar/NavBar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { render, screen } from "@testing-library/react";
import NavBar from './NavBar';
import userEvent from '@testing-library/user-event';

const returnHome = jest.fn();

describe("NavBar", () => {
Copy link
Owner

Choose a reason for hiding this comment

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

future note: For Iteration 5, we could add a search input into the navbar

it("should render a navbar with a home button", () => {
render(<NavBar />);
const navBar = screen.getByText("Rancid Tomatillos")
const navBarButton = screen.getByText("Home")

expect(navBar).toBeInTheDocument();
expect(navBarButton).toBeInTheDocument();
});

it("should invoke a function when clicked", () => {
render(<NavBar
returnToHome={returnHome}
/>);
const navBarButton = screen.getByText("Home");

userEvent.click(navBarButton);

expect(returnHome).toHaveBeenCalled();
})
})
62 changes: 62 additions & 0 deletions src/Components/SelectedMovie/SelectedMovie.test.js
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 SelectedMovie from "./SelectedMovie";

const mockMovieData = {
movie: {
id: 1,
title: "Movie Title",
poster_path: "someURL",
backdrop_path: "someURL",
release_date: "December",
overview: "Some overview",
average_rating: 6,
genres: ["Drama"],
budget: 63000000,
revenue: 100853753,
runtime: 139,
tagline: "Movie Tagline",
},
};

describe("Selected Movie", () => {
it("Should render the title, tagline, movie backdrop, overview, release date, rating, genres, budget, revenue, and runtime", () => {
render(
<SelectedMovie
title={mockMovieData.movie.title}
tagline={mockMovieData.movie.tagline}
poster={mockMovieData.movie.poster_path}
backdrop={mockMovieData.movie.backdrop_path}
overview={mockMovieData.movie.overview}
release={mockMovieData.movie.release_date}
rating={mockMovieData.movie.average_rating}
genres={mockMovieData.movie.genres}
budget={mockMovieData.movie.budget}
revenue={mockMovieData.movie.revenue}
runtime={mockMovieData.movie.runtime}
/>
);

let movieTitle = screen.getByText("Movie Title");
let movieBackdrop = screen.getByRole("img");
let movieReleaseDate = screen.getByText("Release Date: December");
let movieOverview = screen.getByText("Overview: Some overview");
let movieAverageRating = screen.getByText("Rating: 6");
let movieGenres = screen.getByText("Drama");
let movieBudget = screen.getByText("Budget: $63000000");
let movieRevenue = screen.getByText("Revenue: $100853753");
let movieRuntime = screen.getByText("Duration: 139 minutes");
let movieTagline = screen.getByText("Movie Tagline");

expect(movieTitle).toBeInTheDocument();
expect(movieBackdrop).toBeInTheDocument();
expect(movieReleaseDate).toBeInTheDocument();
expect(movieOverview).toBeInTheDocument();
expect(movieAverageRating).toBeInTheDocument();
expect(movieGenres).toBeInTheDocument();
expect(movieBudget).toBeInTheDocument();
expect(movieRevenue).toBeInTheDocument();
expect(movieRuntime).toBeInTheDocument();
expect(movieTagline).toBeInTheDocument();
});
});