my-component.js
import axios from "axios";
import React from "react";
const UnitTest = () => {
const [todo, set_todo] = React.useState({});
React.useState(() => {
const onMounted = async () => {
const getTodo = await axios.get("https://jsonplaceholder.typicode.com/todos/1");
set_todo(getTodo.data);
};
onMounted();
}, []);
return (
<div id="UnitTest">
<p>{todo.title}</p>
</div>
);
};
export default UnitTest;
api response
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
my-component.test.js
import { mount } from "enzyme";
import UnitTest from "../../src/pages/unit-test";
describe("UnitTest component", () => {
let wrapper = mount(<UnitTest />);
it("should render the component", () => {
wrapper = mount(<UnitTest />);
console.log(wrapper.debug());
expect(wrapper.find("p").text()).toEqual("delectus aut autem");
});
});
test result
How to make my <p> tag contain delectus aut autem when console.log(wrapper.debug()) and is it possible if i want to update the state (set_todo) to be
{
"userId": 2,
"id": 3,
"title": "second title",
"completed": true
}
from my test file and update the assertion become expect(wrapper.find("p").text()).toEqual("second titile"); ?
