Skip to content

Instantly share code, notes, and snippets.

@Aperyon
Created May 27, 2020 15:54
Show Gist options
  • Save Aperyon/511a5cf3101e67840fc791fa7dd93bca to your computer and use it in GitHub Desktop.
Save Aperyon/511a5cf3101e67840fc791fa7dd93bca to your computer and use it in GitHub Desktop.
import React from "react";
export default function SmartBookDetail() {
const [isDeleteClicked, setDeleteClicked] = React.useState(false);
const { state: book, fetchBook, deleteBook } = React.useContext(BookContext);
const bookUrl = utils.buildBookUrlFromUri();
React.useEffect(() => {
fetchBook(bookUrl);
}, []);
// Writing unit tests for this function would require the test to become
// integration test and start with mocking API request.
function onDeleteClick() {
console.log("Book Deleted");
deleteBook(bookUrl);
setDeleteClicked(true);
}
if (book === null) {
return <p>Loading...</p>;
}
return (
<DumbBookDetail
book={book}
onDeleteClick={onDeleteClick}
isDeleteClicked={isDeleteClicked}
/>
);
}
// There is barely anything to test in this component
export default function DumbBookDetail({
book,
onDeleteClick,
isDeleteClicked,
}) {
return (
<>
<p>Book: {book.title}</p>
<button onClick={onDeleteClick}>Delete book</button>
<p>Is delete clicked: {isDeleteClicked}</p>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment