Created
May 27, 2020 15:54
-
-
Save Aperyon/511a5cf3101e67840fc791fa7dd93bca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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