使用酶测试道具钻孔

时间:2019-06-06 21:42:20

标签: javascript reactjs jestjs enzyme

我想编写一个集成测试来断言,当父组件向子组件钻某些值或属性时,该组件会接收到所述值并正确地渲染它们。下面有两个组件示例和一个示例测试。当然,测试不准确,但是我想知道如何使用酶来完成这项工作?谢谢!

sampleComponent.js:

import React from 'react';

const SampleComponent = () => (
  <div test-attr="div">
    <SampleChildComponent title="Sample title" />
  </div>
);

export default SampleComponent;

sampleChildComponent.js:

import React from 'react';

const SampleChildComponent = ({ title }) => <h3 test-attr="h">{title}</h3>;

export default SampleChildComponent;

sampleComponent.test.js:

import React from 'react';
import { shallow } from 'enzyme';
import SampleComponent from './sampleComponent';
import SampleChildComponent from './sampleChildComponent';

test('renders component without errors', () => {
  const wrapper = shallow(<SampleComponent />);
  const childWrapper = shallow(<SampleChildComponent />);
  expect(childWrapper.text()).toEqual('sample title');
});

1 个答案:

答案 0 :(得分:2)

要渲染子组件,您应该使用Action.Signin而不是mount

shallow