-
Notifications
You must be signed in to change notification settings - Fork 0
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
Async testing #21
Changes from all commits
e44b023
36b18cf
484b843
4552596
a538057
8116662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,31 +13,12 @@ class App extends Component { | |
super(); | ||
this.state = { | ||
movies: [], | ||
selectedMovie: 0, | ||
selectedMovie: null, | ||
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 ( | ||
|
@@ -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>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. conditional rendering ftw! |
||
</Col> | ||
</Row> | ||
</Container> | ||
); | ||
} | ||
|
@@ -91,6 +77,25 @@ class App extends Component { | |
</React.Fragment> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fun fact: you can create a react fragment with the shorthand <> </> There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 })) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. duly noted! |
||
.catch((err) => this.setState({ error: err })); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.App { | ||
text-align: center; | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great unit test |
||
}); | ||
}); |
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", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}) | ||
}) |
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(); | ||
}); | ||
}); |
There was a problem hiding this comment.
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.