为了测试目的,将dom做出反应

时间:2017-12-01 15:33:50

标签: reactjs jestjs enzyme reactjs-testutils

大家好,我对任何可以提供帮助的人都有疑问。是否有任何帮助函数将document.body转换为React结构并像getElementBy一样访问它?所以我可以通过React Names而不是classNames或TagNames访问各个组件?

已实现的代码示例如下所示:

it("something, changes something", () => {
  const longFormatWithTimeCalendar = mount(<SingleCalendar {...longFormatWithTimeProps}/>)
  longFormatWithTimeCalendar.find("input").first().simulate("click")
  const timepickersHourInput = document.body.getElementsByTagName("input")[0]
  timepickersHourInput.value = "11"
  ReactTestUtils.Simulate.change(timepickersHourInput)
  expect(longFormatWithTimeCalendar.find("input").first().props().value).toBe("25 Dec 2017 11:00")
  longFormatWithTimeCalendar.unmount()
})

我使用document.body.getEmentsByTagName,因为此组件呈现在已安装的组件之外。

0 个答案:

没有答案