Skip to content

Instantly share code, notes, and snippets.

View ivan-ha's full-sized avatar
👨‍💻
cannot read property 'status' of undefined 😄

Ivan Ha ivan-ha

👨‍💻
cannot read property 'status' of undefined 😄
View GitHub Profile
@ivan-ha
ivan-ha / app.js
Last active June 1, 2019 07:48
react-hooks-test-traditional-way
class App extends React.Component {
state = { users: [] };
componentDidMount() {
axios("https://randomuser.me/api/?results=10&inc=name,login&nat=us").then(
response => {
this.setState({
users: response.data.results
});
}
@ivan-ha
ivan-ha / app.js
Created June 1, 2019 07:49
react-hooks-test-using-hooks
const App = () => {
const [users, setUsers] = useState([]);
useEffect(() => {
(async () => {
try {
const response = await axios(
"https://randomuser.me/api/?results=10&inc=name,login&nat=us"
);
setUsers(response.data.results);
@ivan-ha
ivan-ha / app.js
Created June 1, 2019 07:57
react-hooks-test-custom-hooks
const App = () => {
const users = useRandomUsers();
return (
<div className="App">
<header className="App-header">
<ol>
{users &&
users.map(({ name, login }) => (
<li key={login.uuid}>
@ivan-ha
ivan-ha / useRandomUsers.test.js
Created June 1, 2019 10:00
react-hooks-test-failed-unit-test
jest.mock("axios");
describe("useRandomUsers", () => {
it("call API and return results", async () => {
axios.mockImplementation(() => Promise.resolve({ data: mockResponse }));
const users = useRandomUsers();
expect(users.current).toStrictEqual(mockResponse);
});
});
@ivan-ha
ivan-ha / useRandomUsers.test.js
Last active June 1, 2019 13:00
react-hooks-test-custom-hooks
import { renderHook, act } from "react-hooks-testing-library";
jest.mock("axios");
describe("useRandomUsers", () => {
it("call API and return results", async () => {
axios.mockImplementation(() => Promise.resolve({ data: mockResponse }));
const { result, waitForNextUpdate } = renderHook(() => useRandomUsers());
expect(result.current).toStrictEqual([]);