简体   繁体   English

在使用 react-testing-library 进行测试期间,单击两次 material-ui 图标不会将其颜色更改为默认值

[英]Clicking twice material-ui icon does not change its color to default during tests with react-testing-library

I have a material-ui navbar with a couple of material-ui icons on it.我有一个 material-ui 导航栏,上面有几个 material-ui 图标。 One of them is a Bookmarks icon.其中之一是书签图标。 When clicking the icon it becomes blue ( rgb(25, 118, 210) ) and when it is clicked again or somewhere in the page it should become white.单击该图标时,它变为蓝色( rgb(25, 118, 210) ),当再次单击它或页面中的某个位置时,它应该变为白色。 This is achieved using a local variable.这是使用局部变量来实现的。

 <Popover
     ...
     trigger={
       <IconButton classes={{ root: classes.topBarIconButton }}>
          <BookmarksIcon aria-label="bookmarks icon" className={classes.favoriteIcon} />
       </IconButton>
     }
     onEnter={() => setBookmarksOpen(true)}
     onClose={() => setBookmarksOpen(false)}
     >
     <BookmarksList pageTitle={pageTitle} />
 </Popover>

I would like to test the behavior (clicking the button twice) now using react-testing-library.我想现在使用 react-testing-library 测试行为(单击按钮两次)。 I am able to test successfully that the icon becomes blue once clicked.我能够成功测试单击后图标变为蓝色。 But once clicked again I keep receiving the blue color and not the expected white or #fff or a color in rgb format.但是一旦再次单击,我会继续收到蓝色,而不是预期的白色或#fff或 rgb 格式的颜色。

test('clicking bookmarks icon once becomes blue, clicking twice becomes white', () => {
      renderWithAllProviders(<Topbar {...props} />, reduxState);
    
      const bookmarksIcon = screen.getByLabelText('bookmarks icon');
      userEvent.click(bookmarksIcon);
      // bookmarks icon color becomes blue on click
      expect(bookmarksIcon).toHaveStyle({ color: 'rgb(25, 118, 210)' }); // this works as expected
      
      userEvent.click(bookmarksIcon);
      // bookmarks icon color becomes white on second click
      expect(bookmarksIcon).toHaveStyle({ color: 'white' }); // this does not work as expected
    
      // 2nd attempt
      // await waitFor(async () => {
      //   // const asyncBookmarksIcon = await screen.findByLabelText('bookmarks icon');
      //   expect(asyncBookmarksIcon).toHaveStyle({ color: 'white' });
      // });
 });

I get the following error:我收到以下错误:

● clicking bookmarks icon once becomes blue, clicking twice becomes white ● 点击书签图标一次变成蓝色,点击两次变成白色

expect(element).toHaveStyle() - Expected - color: white; + color: rgb(25, 118, 210);

I tried also using the waitFor method but the error becomes even larger in terms of the its output:我也尝试使用waitFor方法,但就其 output 而言,错误变得更大:

Can't perform a React state update on an unmounted component.无法对未安装的组件执行 React state 更新。 This is a no-op, but it indicates a memory leak in your application.这是一个无操作,但它表明您的应用程序中存在 memory 泄漏。 To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.要修复此问题,请在 useEffect 清理 function 中取消所有订阅和异步任务。

included the first error and a big output with printed material ui (probably) native css declarations.包括第一个错误和一个大的 output 和印刷材料 ui(可能)本机 css 声明。

Here is demo of the issue.这是该问题的演示 (better download and run it locally cause it freezes for some reason although when run locally works fine). (更好地下载并在本地运行它会因为某种原因而冻结,尽管在本地运行时可以正常工作)。

Any thoughts or recommendations are welcome.欢迎任何想法或建议。

I ended up asking this question in the official react-testing-library git hub repository.我最终在官方 react-testing-library git hub 存储库中提出了这个问题。

and I got the following answer from one of the contributors.我从其中一位贡献者那里得到了以下答案。

You run your tests in JSDOM which has not full CSS support您在没有完全 CSS 支持的 JSDOM 中运行测试

That means that only partial css behavior can be tested.这意味着只能测试部分 css 行为。

He mentioned that:他提到:

Anything.任何事物。 toHaveStyle has a low confidence depending on how these styles are applied. toHaveStyle的置信度较低,具体取决于这些 styles 的应用方式。 You should use visual regression tests for these types of assertions.您应该对这些类型的断言使用视觉回归测试。

You can find the issue in this link for more information.您可以在此链接中找到问题以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM