我正在尝试测试如果浏览器没有 Flash 插件将不会显示的组件的一部分。该组件借助swfObject和下面提到的逻辑来检测 flash 插件。
MyComponent.js
export default class MyComponent extends Component {
static propTypes = {
// props...
};
static contextTypes = {
router: PropTypes.object.isRequired,
};
constructor() {
super();
this.state = {
isMobile: true
};
}
componentDidMount() {
const flashVersion = require('../../../client/utils/detectFlash')();
if ((flashVersion && flashVersion.major !== 0)) {
/* eslint-disable */
this.setState({isMobile: false});
/* eslint-enable */
}
}
//...
render() {
//...
return (
//...
{ !this.state.isMobile && (
<div className="xyz">
<p>XYZ: this content only shows up when flash has been detected</p>
</div>)
}
);
}
}
MyComponent-test.js
import React from 'react';
import {mount} from 'enzyme';
import chai, {expect} from 'chai';
import chaiEnzyme from 'chai-enzyme';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
import {MyComponent} from '../../common/components';
chai.use(chaiEnzyme());
describe('<MyComponent />', () => {
describe('mobile/flash disabled', () => {
const mockStore = configureStore({});
const store = mockStore({});
it('Does not render xyz', () => {
const wrapper = mount(
<Provider store={store} key="provider">
<MyComponent {...params}/>
</Provider>
);
expect(wrapper.find('.xyz').to.have.length(0));
});
});
});
问题是 this.state.isMobile 在 karma 启动 chrome 并检测到 flash 插件时设置为 false。您可以想象,如果需要手动禁用 Chrome 的 flash 插件,该测试也无法工作。