我正在尝试创建一个窗口对象以传递到包含窗口变量的测试中。测试如下:
const baseProps = {};
Enzyme.configure({ adapter: new Adapter() });
const baseWrapper = shallow(
<DivisionOrderIndex {...baseProps} />,
);
describe('<DivisionOrderIndex />', () => {
test('renders the MsDataGridServer', () => {
expect(baseWrapper.find(MsDataGridBulkDelete))
.toHaveLength(1);
});
我收到的错误消息是:
TypeError: Cannot read property 'read_only' of undefined
300 | </TitleButton>
301 | </div>
> 302 | {!window.jsdata.read_only && (
| ^
303 | <div id="page-buttons">
304 | <a href="/division_order_create" className="btn btn-primary">Create New</a>
305 | </div>
我试图像这样将数据添加到我的窗口对象中:
describe('<DivisionOrderIndex />', () => {
Object.defineProperty(window, 'jsdata', {
read_only: false,
});
test('renders the MsDataGridServer', () => {
expect(baseWrapper.find(MsDataGridBulkDelete))
.toHaveLength(1);
});
但我仍然遇到相同的错误。这是传递窗口对象或变量的正确方法吗?任何输入将不胜感激。
答案 0 :(得分:0)
这不是您向对象添加属性的方式。您在这里所做的是添加描述您的对象的描述符属性,您只能在其中设置其中的少数几个。 writable
,configurable
等
您需要像这样在setupTests.js文件中模拟窗口对象:
window.jsdata = {
read_only: false
}