如何编写测试以确保该方法reloadFn
确实重新加载了窗口?我找到了这个资源,但不清楚在给定函数中发生窗口重载时编写测试时如何期望窗口重载。谢谢您的帮助!
const reloadFn = () => {
window.location.reload(true);
}
这是解决方案,但为了更好的组织而进行了重构:
describe('test window location\'s reload function', () => {
const { reload } = window.location;
beforeAll(() => {
Object.defineProperty(window.location, 'reload', {
configurable: true,
});
window.location.reload = jest.fn();
});
afterAll(() => {
window.location.reload = reload;
});
it('mocks reload function', () => {
expect(jest.isMockFunction(window.location.reload)).toBe(true);
});
it('calls reload function', () => {
reloadFn(); // as defined above..
expect(window.location.reload).toHaveBeenCalled();
});
});
谢谢 :)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句