Estoy tratando de entender el código en la parte inferior. Proviene de esta página: https://reactjs.org/docs/testing-recipes.html#act Estoy tratando de reproducir este caso para que UseEffect funcione durante la prueba. Cuál es la diferencia entre :
Promise.resolve({ json: () => Promise.resolve(fakeUser) })y
Promise.resolve({ json: fakeUser })??
No entiendo.
import React from "react"; import { render, unmountComponentAtNode } from "react-dom"; import { act } from "react-dom/test-utils"; import User from "./user"; let container = null; beforeEach(() => { // setup a DOM element as a render target container = document.createElement("div"); document.body.appendChild(container); }); afterEach(() => { // cleanup on exiting unmountComponentAtNode(container); container.remove(); container = null; }); it("renders user data", async () => { const fakeUser = { name: "Joni Baez", age: "32", address: "123, Charming Avenue" }; jest.spyOn(global, "fetch").mockImplementation(() => Promise.resolve({ json: () => Promise.resolve(fakeUser) }) ); // Use the asynchronous version of act to apply resolved promises await act(async () => { render(<User id="123" />, container); }); expect(container.querySelector("summary").textContent).toBe(fakeUser.name); expect(container.querySelector("strong").textContent).toBe(fakeUser.age); expect(container.textContent).toContain(fakeUser.address); // remove the mock to ensure tests are completely isolated global.fetch.mockRestore(); });