JSX 檔案
// Checks to see if image is cached
const isCached = src => {
const img = new Image(); // eslint-disable-line
img.src = src;
const complete = img.complete;
img.src = "";
return complete;
};
const [isHighRes, lazySetIsHighRes] = useState(
!isCached(`${images[0].normal}?wid=200&hei=200`)
);
useEffect(() => {
// If image is cached load the high res image after the ATF_DONE event
if (!isHighRes) {
const setHighRes = () => {
lazySetIsHighRes(true);
window.removeEventListener("ATF_DONE", setHighRes);
};
window.setTimeout(() => {
window.addEventListener("ATF_DONE", setHighRes);
return () => {
window.removeEventListener("ATF_DONE", setHighRes);
};
}, ATF_TIMEOUT)
}
return null;
}, []);
spec.jsx我嘗試在
beforeEach(() => {
sandbox = sinon.sandbox.create();
sinon.stub(window, "addEventListener");
sinon.stub(window, "removeEventListener");
sandbox.stub(window, "Image").callsFake(() => {
const image = new Image.wrappedMethod();
sandbox.stub(image, "complete").value(complete);
return image;
});
it.only("should load low res image if image is cached", () => {
const clock = sinon.useFakeTimers();
clock.tick(8000);
expect(window.addEventListener).to.have.been.calledWith("ATF_DONE", "setHighRes");
})
uj5u.com熱心網友回復:
看起來您可以手動分派事件以觸發對我有用的回呼
window.dispatchEvent(new Event("ATF_DONE"));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/427125.html
上一篇:如何為CDK階段撰寫Junit
