|
| 1 | +import {apply, isPolyfilled, isSupported, checkVisibility} from '../lib/element-checkvisibility.js' |
| 2 | + |
| 3 | +describe('checkVisibility', () => { |
| 4 | + it('has standard isSupported, isPolyfilled, apply API', () => { |
| 5 | + expect(isSupported).to.be.a('function') |
| 6 | + expect(isPolyfilled).to.be.a('function') |
| 7 | + expect(apply).to.be.a('function') |
| 8 | + expect(isSupported()).to.be.a('boolean') |
| 9 | + expect(isPolyfilled()).to.equal(false) |
| 10 | + }) |
| 11 | + |
| 12 | + it('checks visibility of elements', async () => { |
| 13 | + // These tests originate from |
| 14 | + // https://github.com/web-platform-tests/wpt/blob/master/css/cssom-view/checkVisibility.html |
| 15 | + const el = document.createElement('div') |
| 16 | + // eslint-disable-next-line github/no-inner-html |
| 17 | + el.innerHTML = ` |
| 18 | + <div id=visibilityhidden style="visibility:hidden">hello</div> |
| 19 | + <div style="content-visibility:hidden"> |
| 20 | + <div id=cvhidden>hello</div> |
| 21 | + </div> |
| 22 | + <div style="content-visibility:auto"> |
| 23 | + <div id=cvauto>hello</div> |
| 24 | + </div> |
| 25 | + <div id=displaynone style="display:none">hello</div> |
| 26 | + <div style="display:none" class="shadow-host-with-slot"> |
| 27 | + <div id="slottedindisplaynone" slot="slot">slotted</div> |
| 28 | + </div> |
| 29 | + <div id=displaycontents style="display:contents"> |
| 30 | + <div id=displaycontentschild>hello</div> |
| 31 | + </div> |
| 32 | + <div id=opacityzero style="opacity:0">hello</div> |
| 33 | + <div style="opacity:0" class="shadow-host-with-slot"> |
| 34 | + <div id="slottedinopacityzero" slot="slot">slotted</div> |
| 35 | + </div> |
| 36 | + <div style="content-visibility:hidden"> |
| 37 | + <div id=cvhiddenchildwithupdate></div> |
| 38 | + </div> |
| 39 | + <div style="content-visibility:hidden" id=cvhiddenwithupdate></div> |
| 40 | + <div style="content-visibility:hidden" class="shadow-host-with-slot"> |
| 41 | + <div id="slottedincvhidden" slot="slot">slotted</div> |
| 42 | + </div> |
| 43 | + <div style="height:10000px">spacer</div> |
| 44 | + <div style="content-visibility:auto"> |
| 45 | + <div id=cvautooffscreen>hello</div> |
| 46 | + </div> |
| 47 | + <div id=cvautocontainer> |
| 48 | + <div id=cvautochild></div> |
| 49 | + </div> |
| 50 | + <div style="content-visibility:auto"> |
| 51 | + <div style="content-visibility:auto"> |
| 52 | + <div id=nestedcvautochild></div> |
| 53 | + </div> |
| 54 | + ` |
| 55 | + document.body.append(el) |
| 56 | + for (const host of document.querySelectorAll('.shadow-host-with-slot')) { |
| 57 | + const shadowRoot = host.attachShadow({mode: 'open'}) |
| 58 | + const slot = document.createElement('slot') |
| 59 | + slot.name = 'slot' |
| 60 | + shadowRoot.appendChild(slot) |
| 61 | + } |
| 62 | + expect(checkVisibility.call(document.getElementById('visibilityhidden'), {checkVisibilityCSS: true})).to.equal( |
| 63 | + false, |
| 64 | + ) |
| 65 | + expect(checkVisibility.call(document.getElementById('visibilityhidden'), {checkVisibilityCSS: false})).to.equal( |
| 66 | + true, |
| 67 | + ) |
| 68 | + expect(checkVisibility.call(document.getElementById('cvhidden'))).to.equal(false) |
| 69 | + expect(checkVisibility.call(document.getElementById('slottedincvhidden'))).to.equal(false) |
| 70 | + expect(checkVisibility.call(document.getElementById('cvauto'))).to.equal(true) |
| 71 | + expect(checkVisibility.call(document.getElementById('cvautooffscreen'))).to.equal(true) |
| 72 | + expect(checkVisibility.call(document.getElementById('displaynone'))).to.equal(false) |
| 73 | + expect(checkVisibility.call(document.getElementById('slottedindisplaynone'))).to.equal(false) |
| 74 | + expect(checkVisibility.call(document.getElementById('displaycontents'))).to.equal(false) |
| 75 | + expect(checkVisibility.call(document.getElementById('displaycontentschild'))).to.equal(true) |
| 76 | + expect(checkVisibility.call(document.getElementById('opacityzero'), {checkOpacity: true})).to.equal(false) |
| 77 | + expect(checkVisibility.call(document.getElementById('opacityzero'), {checkOpacity: false})).to.equal(true) |
| 78 | + expect(checkVisibility.call(document.getElementById('slottedinopacityzero'), {checkOpacity: true})).to.equal(false) |
| 79 | + expect(checkVisibility.call(document.getElementById('slottedinopacityzero'), {checkOpacity: false})).to.equal(true) |
| 80 | + const cvautocontainer = document.getElementById('cvautocontainer') |
| 81 | + const cvautochild = document.getElementById('cvautochild') |
| 82 | + cvautocontainer.style.contentVisibility = 'auto' |
| 83 | + cvautochild.style.visibility = 'hidden' |
| 84 | + expect(checkVisibility.call(cvautochild, {checkVisibilityCSS: true})).to.equal(false) |
| 85 | + cvautochild.style.visibility = 'visible' |
| 86 | + expect(checkVisibility.call(cvautochild, {checkVisibilityCSS: true})).to.equal(true) |
| 87 | + expect(checkVisibility.call(document.getElementById('nestedcvautochild'))).to.equal(true) |
| 88 | + const cvhiddenchildwithupdate = document.getElementById('cvhiddenchildwithupdate') |
| 89 | + cvhiddenchildwithupdate.getBoundingClientRect() |
| 90 | + expect(checkVisibility.call(cvhiddenchildwithupdate)).to.equal(false) |
| 91 | + const cvhiddenwithupdate = document.getElementById('cvhiddenwithupdate') |
| 92 | + cvhiddenwithupdate.getBoundingClientRect() |
| 93 | + expect(checkVisibility.call(cvhiddenwithupdate)).to.equal(true) |
| 94 | + }) |
| 95 | +}) |
0 commit comments