【发布时间】:2017-01-03 14:34:23
【问题描述】:
我在 onEnter/onLeave 运行了一些逻辑。我在 onEnter 使用了一些 setInterval 并在 onLeave 使用 clearInterval 清除它们。
如何对上述案例进行单元测试?
<Route
component={App}
onEnter={onEnterApp}
onLeave={onLeaveApp}
path="/app">
下面是我的测试用例,但它失败了,
import React from 'react';
import App from '../components/views/app.jsx';
import {shallow, mount, render} from 'enzyme';
import {expect, assert} from 'chai';
import sinon from 'sinon';
import {mountWithIntl} from '../test_utils/intl-enzyme-test-helper.js';
describe(‘App renders correctly', () => {
it('When mounting set intervals', () => {
const wrapper = mountWithIntl(<App/>);
expect(window.x).to.exist;
expect(window.y).to.exist;
});
it('When unmounting clear intervals', () => {
const wrapper = mountWithIntl(<App/>);
wrapper.unmount();
expect(window.x).to.not.exist;
expect(window.y).to.not.exist;
});
});
【问题讨论】:
-
你试过
global而不是windows -
谢谢。这是一个改进,但没有解决问题。
标签: unit-testing reactjs react-router jestjs