-
Notifications
You must be signed in to change notification settings - Fork 408
toBeVisible
not working as expected with display:none
set on parent component, when using material-ui Grid
#444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm inclined to think that this is related to the fact that CSS is often not mounted/attached to the document when running in a test environment. So the grid element around that field has a CSS class name that's supposed to make it The one thing in your issue report that makes me doubt that this is the reason, is that you mention that if you switch things over to As you hinted, having an example app with tests showing this happening would be great. |
@gnapse thanks for the reply and sorry for the delay in answering. I managed to create a reproduction of this, and it is here: https://codesandbox.io/s/react-testing-library-tobevisible-69djj0 I'll update the issue description as well. Discovering more stuff about this, it appears to me that the assertion works if we're using normal divs, but not if I'm using material-ui Grid container and Grid item. Is this not meant to work? I may be missing something here. The repro should contain all details. Any help is appreciated, thanks so much! |
toBeVisible
not working as expected with display:none
set on parent componenttoBeVisible
not working as expected with display:none
set on parent component, when using material-ui Grid
I've identified that the assertion actually does not pass in 2 cases:
|
First, I wanted to clarify that the issue I thought this was about never implied that the class names are not set in the html. They always are. The issue I suspected is about the fact that, depending on how your CSS system works, you may not have the However, thanks to your sandbox example, I have confirmed that this is not the case here. This is a real head scratcher. I have no idea what's going on there. To give you an idea of the things I tried, I was able to break the
This mimics the markup you get when you use At this point, the only thing I can think of to do is to run this locally with a local version of jest-dom that would have some debugging output in place to see where is the logic failing. I'm not sure when I'll be able to dedicate significant time to this. Any help to further pinpoint the issue, even if not fully solving it, is appreciated. Also, feel free to ping me in a week or two if I have not come back with more comments here. |
@gnapse thanks so much for your input! I'll keep at it myself as well, and try to come up with more investigation notes in the next few days. In the meantime, I say we'll keep this open in case anyone else wants to contribute! Thanks again! |
One last thought that came to my mind before I switch off of this issue for now. It occurs to me that there could be a bug or issue in jsdom with the way different CSS rules are combined, not following the specs in terms of the order in which CSS rules are processed. Specifically, take a look at the styles of these divs when inspecting them in a real browser: Clearly, the I have not dug more into this for now. Just throwing it out there as a potential angle from which to attack this. |
@gnapse Have you by any chance had any time to look further into this? Thanks again for the help you've already provided! |
No, I have not dug more into this since my last comment. |
It is JSDOM's fault for sure. I added some console to the first breaking test:
and the output was:
So as you can see, the stylesheets are in the correct order but Testing Library's logic is ok, the issue is JSDOM gives the wrong value at |
Thanks for digging in, @aquelehugo, and for gathering all this extra level of details. I guess we can close this now. Maybe we should also comment explicitly in that other jsdom issue providing reference to this case, which may be helpful for whoever works on fixing it. |
@gnapse it's nothing. I don't know if they will ever bother to do something about it though :( Anyway, as a workaround, Object.defineProperty(window.HTMLStyleElement.prototype, "innerText", {
get() {
return this.textContent;
},
set(content) {
this.textContent = content;
}
}); |
<!-- How to write a good PR title: - Follow [the Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/). - Give as much context as necessary and as little as possible - Prefix it with [WIP] while it’s a work in progress --> ## Self Checklist - [x] I wrote a PR title in **English** and added an appropriate **label** to the PR. - [x] I wrote the commit message in **English** and to follow [**the Conventional Commits specification**](https://www.conventionalcommits.org/en/v1.0.0/). - [x] I [added the **changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md) about the changes that needed to be released. (or didn't have to) - [x] I wrote or updated **documentation** related to the changes. (or didn't have to) - [x] I wrote or updated **tests** related to the changes. (or didn't have to) - [x] I tested the changes in various browsers. (or didn't have to) - Windows: Chrome, Edge, (Optional) Firefox - macOS: Chrome, Edge, Safari, (Optional) Firefox ## Related Issue <!-- Please link to issue if one exists --> - #1733 ## Summary <!-- Please brief explanation of the changes made --> Migrate `TextField` component with scss ## Details <!-- Please elaborate description of the changes --> - 타이포그래피 믹스인을 추가합니다. TextFieldWrapper 내부에 `Text` 컴포넌트를 추가로 만들 수는 없는 상황인데, 타이포그래피 토큰은 사용해야 했습니다. Text 컴포넌트 내부에서도 해당 믹스인사용하여 중복을 줄이고자 했습니다. - style, className과 중복되던 inputStyle, inputClassName을 제거. (데스크에서도 className으로 오버라이드해서 사용중) - TextField named export로 변경 - 내부 useMemo 컴포넌트로 분리 등 리팩토링 - `TextFieldVariant` 의 값을 명시적인 문자열 타입으로 변경 - NOTE) #1053 의 이슈때문에 `TextFieldSize` 의 값은 문자열 타입으로 변경하지 않았습니다. ### Breaking change? (Yes/No) <!-- If Yes, please describe the impact and migration path for users --> Yes ## References <!-- Please list any other resources or points the reviewer should be aware of --> - testing-library/jest-dom#444 (주석 처리한 테스트 관련. jest dom이 class 명시도 기반의 스타일링을 제대로 모킹하지 못하는 거 같아요)
There is a small problem when checking its visibility. The element should be not visible but for some reason the styles are not being applied correctly on the test, but on localhost looks fine and works as intended. The following github issue has something to do with my problem: testing-library/jest-dom#444
Uh oh!
There was an error while loading. Please reload this page.
@testing-library/jest-dom
version:^5.16.1
node
version:v14.15.1
npm
(oryarn
) version:6.14.8
react-testing-library
version:^12.1.2
Relevant code or config:
What you did:
I tried to test the fact that the Field is hidden, using the
.toBeVisible
matcher. The assertion looked something like this:The assertion fails even if I'm using
getByRole
.What happened:
I have also noticed that if instead of
display:none
, I usevisibility:hidden
oropacity:0
, it works as expected, it's just with thedisplay:"none"
that it doesn't work. Unfortunately, it is impossible for me to change the code to use the any of the two options mentioned, as they have a different behaviour. I needdisplay:none
in this case.Reproduction:
https://codesandbox.io/s/react-testing-library-tobevisible-69djj0
Problem description:
As per the documentation, .toBeVisible should be working even if
display:none
on a parent component is used, and it appears it is not.Suggested solution:
This should just be working as per docs. I'm referencing the docs from https://github.com/testing-library/jest-dom#tobevisible
The text was updated successfully, but these errors were encountered: