Skip to content
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

feat: make toBeVisible take into account 'hidden' attribute #97

Closed
IlCallo opened this issue Mar 27, 2019 · 13 comments · Fixed by #113
Closed

feat: make toBeVisible take into account 'hidden' attribute #97

IlCallo opened this issue Mar 27, 2019 · 13 comments · Fixed by #113

Comments

@IlCallo
Copy link
Contributor

IlCallo commented Mar 27, 2019

Hey folks,
I noticed toBeVisible matcher ignore hidden attribute.
It may be a good idea to check that too.

@dirkluijk
Copy link
Collaborator

Weird. Spectator just uses jQuery for that $(el).is(':visible'), which should take hidden attribute into account.

But I know that hidden is not always really hidden, it may depend on the browsers default styling. What browser are you using for your unit test?

@IlCallo
Copy link
Contributor Author

IlCallo commented Mar 30, 2019

As you can see here, the hidden attribute is not taken into account (or, at least, it's not mentioned).

I'm using Jest as testing framework.

@dirkluijk
Copy link
Collaborator

dirkluijk commented Mar 30, 2019

I tried to reproduce (we use Jest too), but I noticed that the matchers doesn't work at all on Jest.

Found out that jsdom does not support jQuery :visible and :hidden selectors: jsdom/jsdom#1048. Let's wait for them to have it fixed.

Maybe it would work if Spectator downgrades to jQuery 1. Not sure if that is a good idea.

@IlCallo
Copy link
Contributor Author

IlCallo commented Mar 30, 2019

It doesn't seem like they are going to fix this at all...
Do you think it would be possible to create an "out-of-jquery" solution?

@dirkluijk
Copy link
Collaborator

We could also just change it to $(el).is(':visible') && !$(el).is('[hidden]') and $(el).is(':hidden') || $(el).is('[hidden]').

@NetanelBasal what do you think?

@NetanelBasal
Copy link
Member

Will it work for you?

@IlCallo
Copy link
Contributor Author

IlCallo commented Mar 30, 2019

I guess yes for my use case, but it won't solve problems with jQuery and jsdom 🤔

@NetanelBasal
Copy link
Member

You can submit a PR that replace this with native JS implementation if you want.

@IlCallo
Copy link
Contributor Author

IlCallo commented Jun 11, 2019

I'm trying to change the implementation but I would like to keep

$(el).is(':visible') && !$(el).is('[hidden]') and $(el).is(':hidden') || $(el).is('[hidden]').

for Jasmine (to keep the code cleaner and more performant) and only switch back to jQuery 1 implementation for Jest, probably something like

"hidden" !== elem.type &&
  $(elem).css("display") !== "none" &&
  $(elem).css("visibility") !== "hidden" &&
  !$(elem).is('[hidden]')

Is it possible to register different version of the matcher for the two testing frameworks?
Or is it possible to know which testing framework is running the test at runtime inside the matcher code?

Another possibility is the one proposed here which changes the getClientsRect prototype implementation to make it work keeping current implementation even in jsdom (adding the 'hidden' attribute check).

Is there a place where I can safely put that code to make it work only when tests are run with Jest?

@NetanelBasal
Copy link
Member

I would keep it simple and go with something that works.

@IlCallo
Copy link
Contributor Author

IlCallo commented Jun 11, 2019

It depends on your perception of "something that works", given that it's always a matter of tradeoffs:

  • Prototype-changing implementation theoretically works for every framework, but potentially have major performance impact;
  • jQuery1 implementation detects visibility only the direct child, eg. it won't understand that children of an hidden element are hidden themselves

I can also try to use the prototype code (which checks all parents of the given element for visibility hints) directly inside the matcher if you prefer, but the code will be uglier

@NetanelBasal
Copy link
Member

What about plain old JS?

@IlCallo
Copy link
Contributor Author

IlCallo commented Jun 11, 2019

I have nothing against plain JS (I actually prefer it over jQuery), but touching clean and working code (the one for Jasmine) is never a good idea for someone (me) which doesn't know well the codebase, expecially when there are no tests for the changed feature...

Anyway I did it with plain JS (noticing only at the end that a setup-jest file actually existed), I'll PR it soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants