【问题标题】:Jest not finding className开玩笑找不到类名
【发布时间】:2018-12-19 15:00:56
【问题描述】:

我正在努力确定为什么我的玩笑测试永远找不到我的组件类/ 这是调试语句的组件输出

  <FileLine file={{...}} readOnly={false} onSelectedRevisionChanged={[Function: mockConstructor]}>
  <tr className="warning">
    <td className="col-xs-2">
      Data
    </td>
    <td className="col-xs-2">
      <select className="revision-selection form-control" value="1" onChange={[Function: value]}>
        <option value="1">
          1
        </option>
        <option value="2">
          2
        </option>
      </select>
    </td>
    <td className="col-xs-2">
      Loading...
    </td>

我开玩笑的测试是检查以下内容

it.only('sends selected revision when changed', () => {		
		const selectionCallBack = jest.fn();
		const component = mount(<FileLine file={fileUnderTest} readOnly={false} onSelectedRevisionChanged={selectionCallBack} />);

		//This will change the drop down to a different value
		const revisionSelection = component.find('revision-selection');
		console.log(component.debug());
		expect(revisionSelection.length).to.equal(1);

		revisionSelection.simulate('change', { target: { value: '2' } });

		expect(selectionCallBack).toBeCalledWith('packageRoot/F5___/Apps/GM/GM.E86_r2');
	});

expect 语句始终表明 revisionSelection 始终未定义。考虑到它在输出中显示了该类,这怎么可能是非常困惑的。

【问题讨论】:

  • 您在选择器component.find('revision-selection'); 中错过了.,应该是component.find('.revision-selection');
  • 我已经厌倦了,仍然有完全相同的结果
  • 如果您console.log(component) 会发生什么?你在输出中看到select 标记了吗?
  • @codenamev 返回 ReactWrapper {}

标签: javascript reactjs jestjs enzyme


【解决方案1】:

如果component.find('.revision-selection')(注意查找中的前导.)返回undefined,则很有可能该组件未呈现。

我会检查一下:

expect(component.find('.revision-selection')).to.have.lengthOf(1);

然后更新您的revisionSelection 分配以定位第一个匹配项:

const revisionSelection = component.find('.revision-selection').first();

【讨论】:

    猜你喜欢
    • 2019-12-28
    • 2021-11-17
    • 2018-10-12
    • 2021-05-30
    • 2021-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多