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

Fix visible to work with elements in the Shadow DOM #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions chai-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@

chai.Assertion.addProperty('displayed', function() {
var el = flag(this, 'object'),
actual = el.getRootNode({ composed: true }) === document ? window.getComputedStyle(el).display : el.style.display
isAttached = el.getRootNode ? el.getRootNode({ composed: true }) === document : document.body.contains(el),
actual = isAttached ? window.getComputedStyle(el).display : el.style.display

this.assert(
actual !== 'none'
Expand All @@ -362,7 +363,8 @@

chai.Assertion.addProperty('visible', function() {
var el = flag(this, 'object'),
actual = document.body.contains(el) ? window.getComputedStyle(el).visibility : el.style.visibility
isAttached = el.getRootNode ? el.getRootNode({ composed: true }) === document : document.body.contains(el),
actual = isAttached ? window.getComputedStyle(el).visibility : el.style.visibility

this.assert(
actual !== 'hidden' && actual !== 'collapse'
Expand Down
35 changes: 22 additions & 13 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe('DOM assertions', function() {
})
})

class CustomElement extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
var template = document.createElement('template')
template.innerHTML = '<style>div { display: none; visibility: hidden; }</style>'
this.shadowRoot.appendChild(template.content.cloneNode(true))
this.shadowRoot.appendChild(document.createElement('div'))
}
}
window.customElements.define('custom-element', CustomElement)

describe('attr', function() {
beforeEach(function() {
subject = parse('<div name="foo"></div>')
Expand Down Expand Up @@ -837,23 +849,13 @@ describe('DOM assertions', function() {
})

describe('displayed', function() {
class CustomElement extends HTMLElement {
constructor() {
super()
this.attachShadow({ mode: 'open' })
this.shadowRoot.appendChild(document.createElement('div'))
}
}

var ce = document.createElement('custom-element'),
div = document.createElement('div'),
notDisplayedViaStyle = parse('<div style="display: none"></div>'),
notDisplayedViaCSS = parse('<div class="hidden"></div>'),
inlineDiv = parse('<div style="display: inline-block"></div>')

before(function() {
window.customElements.define('custom-element', CustomElement)

document.styleSheets[1].insertRule('.hidden { display: none; }', 0);
document.body.appendChild(notDisplayedViaCSS)
document.body.appendChild(div)
Expand Down Expand Up @@ -899,13 +901,14 @@ describe('DOM assertions', function() {
div.should.be.displayed.and.exist.and.be.ok
})

it('passes when the element is in the shadow DOM', function() {
ce.shadowRoot.querySelector('div').should.be.displayed
it('passes negated when the element is in the shadow DOM', function() {
ce.shadowRoot.querySelector('div').should.not.be.displayed
})
})

describe('visible', function() {
var div = document.createElement('div'),
var ce = document.createElement('custom-element'),
div = document.createElement('div'),
hiddenViaStyle = parse('<div style="visibility: hidden"></div>'),
collapsedViaStyle = parse('<div style="visibility: collapse"></div>'),
hiddenViaCSS = parse('<div class="invisible"></div>'),
Expand All @@ -917,11 +920,13 @@ describe('DOM assertions', function() {
document.body.appendChild(hiddenViaCSS)
document.body.appendChild(collapsedViaCSS)
document.body.appendChild(div)
document.body.appendChild(ce)
})
after(function() {
document.body.removeChild(hiddenViaCSS)
document.body.removeChild(collapsedViaCSS)
document.body.removeChild(div)
document.body.removeChild(ce)
})

it('passes when visible (any visibility value but hidden or collapse)', function() {
Expand Down Expand Up @@ -964,6 +969,10 @@ describe('DOM assertions', function() {
it('should be chainable', function() {
div.should.be.visible.and.exist.and.be.ok
})

it('passes negated when the element is in the shadow DOM', function() {
ce.shadowRoot.querySelector('div').should.not.be.visible
})
})

describe('tagName', function() {
Expand Down