我在这里https://codesandbox.io/s/popover-opens-on-click-wct7s编写了一个弹出窗口测试,该弹出窗口在浏览器中的行为符合预期,但在酶中却没有。我编写的测试如下:
import Enzyme, { mount } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
import React from "react";
import { Provider } from "styletron-react";
import { Client as Styletron } from "styletron-engine-atomic";
import Example from "../example";
const engine = new Styletron();
Enzyme.configure({ adapter: new Adapter() });
test("Clicking outside of popover should close popover and make popover contents unmount", async () => {
const wrapper = mount(
<Provider value={engine}>
<div data-test-id="app">
<Example />
</div>
</Provider>
);
let p;
const container = wrapper.find("div");
p = wrapper.find("Paragraph1");
const btn = wrapper.find("button");
expect(p).toHaveLength(0);
btn.simulate("click");
p = wrapper.find("Paragraph1");
expect(p).toHaveLength(1);
container.simulate("click");
p = wrapper.find("Paragraph1");
expect(p).toHaveLength(0); // this test fails and text content lingers after clicking outside
});
我想知道如何解决此测试,以使失败的测试遵循浏览器中的实际行为,在浏览器中单击外部后弹出内容消失。