Enzyme 对象没有有用的方法。到。长度

Enzyme 对象没有有用的方法。到。长度,enzyme,ava,Enzyme,Ava,我有以下测试 import test from 'ava'; import React from 'react'; import { shallow } from 'enzyme'; import A from '../../../src/components/A/index'; import B from '../../../src/components/B/index'; console.log('loaded component test'); test('shallow', t =&

我有以下测试

import test from 'ava';
import React from 'react';
import { shallow } from 'enzyme';

import A from '../../../src/components/A/index';
import B from '../../../src/components/B/index';
console.log('loaded component test');

test('shallow', t => {
  const wrapper = shallow(<A />);
  t.is(wrapper.find(B).length, 38);
});

问题是我需要一个完整的DOM来测试这些嵌套组件。我补充说

/** setup.js
 * provides a document for enzyme mount tests
 * simply require this file to make the environment available
 */
import test from 'ava';
var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property);
    global[property] = document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js'
};

test(t => {
  t.pass('DOM setup'); // silences ava warning
});
然后我使用了
mount

import test from 'ava';
import React from 'react';
import { mount, shallow } from 'enzyme';
import setup from '../../setup';

import A from '../../../src/components/A/index';
import B from '../../../src/components/B/index';

test('should display no B if A given no props', t => {
  const wrapper = mount(<A />);
  t.is(wrapper.find(B).length, 0);
});

test('should display two Bs if A given two B via props', t => {
  const testBs = [
    { id: 1},
    { id: 2},
  ];

  const wrapper = mount(<A Bees={testBs} />);
  t.is(wrapper.find(B).length, 2);
});
从“ava”导入测试;
从“React”导入React;
从“酶”导入{mount,shallow};
从“../../setup”导入安装程序;
从“../../../src/components/A/index”导入A;
从“../../../src/components/B/index”导入B;
测试('如果给定的没有道具,则不应显示B',t=>{
const wrapper=mount();
t、 是(wrapper.find(B).length,0);
});
测试('如果通过道具给定两个B,则应显示两个B',t=>{
常数testBs=[
{id:1},
{id:2},
];
const wrapper=mount();
t、 是(包装器。查找(B)。长度,2);
});

注意:在将
B
传递到
find()
时,我没有使用jsx语法。它还帮助升级了
ava
babel core
版本,并添加了
es2017
预设以获得异步/等待支持。

问题是我需要一个完整的DOM来测试这些嵌套组件。我补充说

/** setup.js
 * provides a document for enzyme mount tests
 * simply require this file to make the environment available
 */
import test from 'ava';
var jsdom = require('jsdom').jsdom;

var exposedProperties = ['window', 'navigator', 'document'];

global.document = jsdom('');
global.window = document.defaultView;
Object.keys(document.defaultView).forEach((property) => {
  if (typeof global[property] === 'undefined') {
    exposedProperties.push(property);
    global[property] = document.defaultView[property];
  }
});

global.navigator = {
  userAgent: 'node.js'
};

test(t => {
  t.pass('DOM setup'); // silences ava warning
});
然后我使用了
mount

import test from 'ava';
import React from 'react';
import { mount, shallow } from 'enzyme';
import setup from '../../setup';

import A from '../../../src/components/A/index';
import B from '../../../src/components/B/index';

test('should display no B if A given no props', t => {
  const wrapper = mount(<A />);
  t.is(wrapper.find(B).length, 0);
});

test('should display two Bs if A given two B via props', t => {
  const testBs = [
    { id: 1},
    { id: 2},
  ];

  const wrapper = mount(<A Bees={testBs} />);
  t.is(wrapper.find(B).length, 2);
});
从“ava”导入测试;
从“React”导入React;
从“酶”导入{mount,shallow};
从“../../setup”导入安装程序;
从“../../../src/components/A/index”导入A;
从“../../../src/components/B/index”导入B;
测试('如果给定的没有道具,则不应显示B',t=>{
const wrapper=mount();
t、 是(wrapper.find(B).length,0);
});
测试('如果通过道具给定两个B,则应显示两个B',t=>{
常数testBs=[
{id:1},
{id:2},
];
const wrapper=mount();
t、 是(包装器。查找(B)。长度,2);
});
注意:在将
B
传递到
find()
时,我没有使用jsx语法。它还帮助升级了
ava
babel core
版本,并添加了
es2017
预设以获得异步/等待支持