【发布时间】:2020-01-09 15:12:30
【问题描述】:
我正在尝试测试我的锚标记。一旦我点击它,我想看看window.location.href是否是我所期望的。
我尝试渲染锚点,点击它,然后测试window.location.href:
test('should navigate to ... when link is clicked', () => {
const { getByText } = render(<a href="https://test.com">Click Me</a>);
const link = getByText('Click Me');
fireEvent.click(link);
expect(window.location.href).toBe("https://www.test.com/");
});
我希望测试能够通过,但 window.location.href 只是 "http://localhost/",这意味着无论出于何种原因它都不会更新。我什至尝试用await wait 包装我的期望,但这也不起作用。我找不到太多关于使用react-testing-library 测试锚点的信息。也许有比我正在做的更好的方法来测试它们。 ????♂️
【问题讨论】:
标签: reactjs jestjs anchor href react-testing-library