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

Add isVisible() and isNotVisible() assertions #54

Closed
wants to merge 1 commit into from

Conversation

patocallaghan
Copy link
Contributor

@patocallaghan patocallaghan commented Feb 22, 2018

Fixes #52

Adds isVisible and isNotVisible assertion helpers.

Visibility is modelled off of jQuery's :visible logic in jQuery 3.0 https://github.com/jquery/jquery/blob/4a2bcc27f9c3ee24b3effac0fbe1285d1ee23cc5/src/css/hiddenVisibleSelectors.js#L11-L13

From https://api.jquery.com/hidden-selector/, an element is considered "hidden" if it follows any of the following criteria

They have a CSS display value of none.
They are form elements with type="hidden".
Their width and height are explicitly set to 0.
An ancestor element is hidden, so the element is not shown on the page.
Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout.
elements will be considered :hidden if they don't have any layout boxes. For example, br elements and inline elements with no content will not be selected by the :hidden selector.

As mentioned in the issue you may decide not to merge this. If you do, I can then update the README information.

@@ -16,5 +16,12 @@ test('qunit-dom assertions are available', function(assert) {
assert.dom('#title').exists();
assert.dom('#subtitle').doesNotExist();
assert.dom('#title').hasText('Welcome to Ember');

// isVisible/isNotVisible tests
assert.dom('#title').isVisible();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add the isVisible/isNotVisible tests here as the jest tests always seems to consider elements as hidden. Although I may have been doing something incorrect?

@patocallaghan
Copy link
Contributor Author

Just fixed some failing tests, they are now ✅

// https://github.com/jquery/jquery/blob/4a2bcc27f9c3ee24b3effac0fbe1285d1ee23cc5/src/css/hiddenVisibleSelectors.js#L11-L13

export default function visible(el) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could make this check a little more explicit? assuming I understand the logic correctly, maybe something like this:

return el.offsetWidth !== 0 || el.offsetHeight !== 0 || el.getClientRects().length !== 0

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good change, though the above is literally what jQuery uses (though that isn't any endorsement for using that code).

@@ -462,6 +463,58 @@ export default class DOMAssertions {
this.hasNoValue(message);
}

/**
* Assert an [HTMLElement][] matching the `selector` is visible.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add to the documentation what makes an element "visible"? for example if the browser is scrolled to a different part of the page does that mean that elements are not visible then?

@Turbo87 Turbo87 changed the title Add isVisible and isNotVisible assertion helpers Add isVisible() and isNotVisible() assertions Mar 11, 2018
@scalvert
Copy link
Collaborator

I tried to push some of these fixes to @patocallaghan's fork, but no dice. Guess we'll have to wait.

@scalvert
Copy link
Collaborator

scalvert commented Mar 17, 2018

So one thing that needs to be addressed here is that this code will fail if an element is not rendered yet, which may still be considered not visible.

In jQuery, you always get an element back. So, the visible check works well because if jQuery doesn't find the element, it returns a default jQuery object which can be evaluated.

With using querySelector, if an element is not found (not rendered yet) it will return null. Ember's test-helpers use find, which under the covers use querySelector. This means that when passing in an element that was the result of find we could end up passing null in. This will currently error in the above.

To fix is-visible, we should first test for the element's existence:

return el !== null && (el.offsetWidth !== 0 || el.offsetHeight !== 0 || el.getClientRects().length !== 0);

To fix is-not-visible:

return el === null || !(el.offsetWidth !== 0 || el.offsetHeight !== 0 || el.getClientRects().length !== 0);

@patocallaghan
Copy link
Contributor Author

Apologies I hope to get to addressing the comments and finishing this in the next few days

@scalvert
Copy link
Collaborator

Let me know what i can do to help.

@patocallaghan
Copy link
Contributor Author

Closing this PR as #67 supersedes it

@patocallaghan patocallaghan deleted the patoc/visible branch March 26, 2018 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants