Javascript 带Jest的单元测试requestAnimationFrame
当我调整单元测试时,我遇到了一个问题,这让我有一段时间处于混乱状态。我在开玩笑,没有反应,没有酶,也没有其他兄弟 我在一个函数中停止了,在返回之前,我有两个条件,不知道如何调用和测试Javascript 带Jest的单元测试requestAnimationFrame,javascript,unit-testing,jestjs,Javascript,Unit Testing,Jestjs,当我调整单元测试时,我遇到了一个问题,这让我有一段时间处于混乱状态。我在开玩笑,没有反应,没有酶,也没有其他兄弟 我在一个函数中停止了,在返回之前,我有两个条件,不知道如何调用和测试rAFrequestAnimationFrame的回调。我的登录功能如下所示: const myFn = (dispatch, options) => { const { state } = options; const { isScrolling } = state; const s
rAF
requestAnimationFrame
的回调。我的登录功能如下所示:
const myFn = (dispatch, options) => {
const { state } = options;
const { isScrolling } = state;
const scrollOptions = { behavior: 'smooth', block: 'end' };
const wrapper = document.getElementById(options.wrapperId);
const target = document.getElementById(options.elementId);
wrapper && wrapper.addEventListener('scroll', () => {
wrapper.scrollHeight - (wrapper.offsetHeight + wrapper.scrollTop + OFFSET) <= 20
?
dispatch(scrollingOn(state))
:
dispatch(scrollingOff(state));
});
return setTimeout(() => {
if (target && isScrolling) {
target.scrollIntoView(scrollOptions);
}
}, 500);
};
export const ScrollIntoView = (state, elementId, wrapperId, interval) => [myFn, { state, elementId, wrapperId}];
我的DOM模拟看起来像:
scrollIntoView: jest.fn(),
addEventListener: jest.fn(listener => listener),
scrollHeight: 123,
clientHeight: 122,
scrollTop: 123,
offsetHeight: 200,
focus: jest.fn(),
scroll: jest.fn(),
id: 'mockedElement',
style: {
overflowY: 'inherit',
height: 1
}
};
export const domMock = () => {
return {
getElementById: () => htmlElement,
addEventListener: jest.fn(listener => listener)
};
};
And the place where I'm stacked is line 59 where I'm trying test rAF.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/LgB6Z.png
I would be thankful for if someone to share experience in testing rAF and how can test the callback.
我看到这里的问题是在第一眼看到的时候就调度了scroll事件。
scrollIntoView: jest.fn(),
addEventListener: jest.fn(listener => listener),
scrollHeight: 123,
clientHeight: 122,
scrollTop: 123,
offsetHeight: 200,
focus: jest.fn(),
scroll: jest.fn(),
id: 'mockedElement',
style: {
overflowY: 'inherit',
height: 1
}
};
export const domMock = () => {
return {
getElementById: () => htmlElement,
addEventListener: jest.fn(listener => listener)
};
};
And the place where I'm stacked is line 59 where I'm trying test rAF.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/LgB6Z.png
I would be thankful for if someone to share experience in testing rAF and how can test the callback.