Skip to content

Commit

Permalink
fix(matchers): visibility matchers work with Jest (#113)
Browse files Browse the repository at this point in the history
fix(matchers): visibility matchers work with Jest
  • Loading branch information
NetanelBasal committed Jun 13, 2019
2 parents e1eaa5d + fd7c947 commit 843c5df
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions projects/spectator/src/lib/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,42 @@ export const toBeEmpty = comparator(el => {
});

/**
* The :hidden selector selects hidden elements.
* Hidden elements are elements that are:
* 1. Set to display:none
* Hidden elements are elements that have:
* 1. Display property set to "none"
* 2. Width and height set to 0
* 3. A hidden parent element (this also hides child elements)
* 4. Form elements with type="hidden"
* 4. Type equal to "hidden" (only for form elements)
* 5. A "hidden" attribute
*/
function isHidden(el) {
while (el) {
if (el === document) {
break;
}

if (!(el.offsetWidth || el.offsetHeight || el.getClientRects().length) || el.style.display === 'none' || el.style.visibility === 'hidden' || el.type === 'hidden' || el.hasAttribute('hidden')) {
return true;
}

el = el.parentNode;
}

return false;
}

/**
* Hidden elements are elements that have:
* 1. Display property set to "none"
* 2. Width and height set to 0
* 3. A hidden parent element (this also hides child elements)
* 4. Type equal to "hidden" (only for form elements)
* 5. A "hidden" attribute
*
* expect('div').toBeHidden();
*
* */
export const toBeHidden = comparator(el => {
const pass = $(el).is(':hidden');
const pass = isHidden(el);
const message = () => `Expected element${pass ? ' not' : ''} to be hidden`;
return { pass, message };
});
Expand All @@ -269,18 +293,19 @@ export const toBeSelected = comparator(el => {
});

/**
* The :visible selector selects hidden elements.
* Hidden elements are elements that are:
* 1. Set to display:none
* Hidden elements are elements that have:
* 1. Display property set to "none"
* 2. Width and height set to 0
* 3. A hidden parent element (this also hides child elements)
* 4. Form elements with type="hidden"
* 4. Type equal to "hidden" (only for form elements)
* 5. A "hidden" attribute
*
* expect('div').toBeVisible();
*
* */
export const toBeVisible = comparator(el => {
const pass = $(el).is(':visible');
const pass = !isHidden(el);

const message = () => `Expected element${pass ? ' not' : ''} to be visible`;
return { pass, message };
});
Expand Down

0 comments on commit 843c5df

Please sign in to comment.