我需要测试一个在浏览器中打开新选项卡的函数
openStatementsReport(contactIds) {
window.open(`a_url_${contactIds}`);
}
Run Code Online (Sandbox Code Playgroud)
我想模拟窗口的open函数,这样我就可以验证正确的URL是否传递给open函数.
使用Jest,我不知道如何模仿窗口.我尝试使用模拟函数设置window.open但这种方式不起作用.以下是测试用例
it('correct url is called', () => {
window.open = jest.fn();
statementService.openStatementsReport(111);
expect(window.open).toBeCalled();
});
Run Code Online (Sandbox Code Playgroud)
但它给了我错误
expect(jest.fn())[.not].toBeCalled()
jest.fn() value must be a mock function or spy.
Received:
function: [Function anonymous]
Run Code Online (Sandbox Code Playgroud)
我应该怎么做测试用例?任何建议或提示表示赞赏
在Jest中有没有办法模拟全局对象,比如navigator,Image*?我已经非常放弃了这一点,并将其留给了一系列可模拟的实用方法.例如:
// Utils.js
export isOnline() {
return navigator.onLine;
}
Run Code Online (Sandbox Code Playgroud)
测试这个微小的功能很简单,但很苛刻,而且根本不具有确定性.我可以获得75%的方式,但这是我可以做的:
// Utils.test.js
it('knows if it is online', () => {
const { isOnline } = require('path/to/Utils');
expect(() => isOnline()).not.toThrow();
expect(typeof isOnline()).toBe('boolean');
});
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我对这个间接是好的,我现在可以navigator通过这些实用程序访问:
// Foo.js
import { isOnline } from './Utils';
export default class Foo {
doSomethingOnline() {
if (!isOnline()) throw new Error('Not online');
/* More implementation */
}
}
Run Code Online (Sandbox Code Playgroud)
......并确定性地测试这样......
// Foo.test.js
it('throws when offline', () => {
const Utils = require('../services/Utils');
Utils.isOnline …Run Code Online (Sandbox Code Playgroud) 我正在测试不应该在本地运行并且需要模拟的功能window.location.href:
const usePageTracking = (): void => {
const location = useLocation();
useEffect(() => {
if (!window.location.href.includes("localhost")) {
ReactGA.initialize("UA-000000-01");
ReactGA.pageview(window.location.pathname + window.location.search);
}
}, []);
};
Run Code Online (Sandbox Code Playgroud)
在我的测试中:
describe("usePageTracking", () => {
it("initializes ReactGA", () => {
render(<Example />);
expect(ReactGA.initialize).toHaveBeenCalled();
});
it("tracks page view", () => {
render(<Example />);
expect(ReactGA.pageview).toHaveBeenCalledWith("/");
});
});
Run Code Online (Sandbox Code Playgroud)
注意:关于 Vue有一个相关的问题,但我不清楚这些解决方案是否也适用于 React(有些解决方案不起作用)。
我有以下功能要测试
function tradePage() {
setTimeout(function() {
window.location.pathname = document
.getElementById('button')
.getAttribute('new-page');
}, 100);
}
Run Code Online (Sandbox Code Playgroud)
而且我编写了以下测试:
test('should change page when button is clicked', () => {
var button = document.querySelector('#button');
jest.useFakeTimers();
button.dispatchEvent(createEvent('click'));
jest.runAllTimers();
expect(window.location.pathname).toEqual('/new-url');
});
Run Code Online (Sandbox Code Playgroud)
但是,当我运行测试时,出现以下错误:
expect(received).toEqual
Expected value to equal:
"/new-url"
Received:
"blank"
Run Code Online (Sandbox Code Playgroud)
我已经完成/尝试过的事情
"testURL"设置好了。我发现了这个可能的解决方案(不起作用):
Object.defineProperty(window.location, 'pathname', {
writable: true,
value: '/page/myExample/test',
});
Run Code Online (Sandbox Code Playgroud)还有什么我可以尝试的想法吗?
我有React应用是通过用玩笑和酶进行create-react-app制作的
那么如何获取window.location.pathname测试文件内部的值?
这是我的规格
import React from 'react';
import MovieDetailsBox from '../../src/components/movie_single/MovieDetailsBox';
import { expect } from 'chai';
import { shallow } from 'enzyme';
import { movie_details } from '../../src/data/movies_details'
import { empty_user } from '../../src/helper/sessionHelper';
jest.mock('react-router');
describe('Test <MovieDetailsBox /> Component', () => {
let movie_details_props = {
movie: {...movie_details}
}
let user_props = {
}
const context = { router: { isActive: (a, b) => true } }
const wrapper = shallow(
<MovieDetailsBox movie={movie_details_props}
user={user_props}
currentPath={'/movie/68'}/>, { context …Run Code Online (Sandbox Code Playgroud) jestjs ×5
javascript ×3
mocking ×2
reactjs ×2
unit-testing ×2
babel-jest ×1
dependencies ×1
enzyme ×1
react-hooks ×1
testing ×1