From c72900eeef8a7a8e3c10beeb21c41f85961733e2 Mon Sep 17 00:00:00 2001 From: "ondrej.frei" Date: Fri, 29 Nov 2024 10:57:28 +0100 Subject: [PATCH] Add tests for DomActions.prehide() --- tests-wtr/dom-actions/prehide/README.md | 1 + .../dom-actions.prehide.delayed-element.html | 11 ++++++ .../dom-actions.prehide.delayed-element.ts | 34 +++++++++++++++++++ .../dom-actions.prehide.empty-selector.html | 12 +++++++ .../dom-actions.prehide.empty-selector.ts | 27 +++++++++++++++ ...m-actions.prehide.preexisting-element.html | 12 +++++++ ...dom-actions.prehide.preexisting-element.ts | 28 +++++++++++++++ 7 files changed, 125 insertions(+) create mode 100644 tests-wtr/dom-actions/prehide/README.md create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.html create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.ts create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.html create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.ts create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.html create mode 100644 tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.ts diff --git a/tests-wtr/dom-actions/prehide/README.md b/tests-wtr/dom-actions/prehide/README.md new file mode 100644 index 00000000..7bc50ff7 --- /dev/null +++ b/tests-wtr/dom-actions/prehide/README.md @@ -0,0 +1 @@ +Note: The test cases for `prehide()` have to be separated into dedicated files, as the generated HTML element `style#autoconsent-prehide` always has the same id and thus the test cases would interfere with one another. diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.html b/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.html new file mode 100644 index 00000000..768d312e --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.html @@ -0,0 +1,11 @@ + + + + + diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.ts b/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.ts new file mode 100644 index 00000000..599a9233 --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.delayed-element.ts @@ -0,0 +1,34 @@ +import { expect } from '@esm-bundle/chai'; +import { DomActions } from '../../../lib/dom-actions'; +import { instantiateDomActions } from '../utils'; + +// must be run from dom-actions.prehide.delayed-element.html +describe('prehide', () => { + let domActions: DomActions; + + beforeEach(() => { + // Given + domActions = instantiateDomActions(); + }); + + it('returns true even when element is only created after the call, and is immediately hidden', () => { + // Given + expect(document.querySelector('style#autoconsent-prehide')).to.be.null; // check that style element does not yet exist + + // When + const result = domActions.prehide('#nonexistent'); + + // Then + expect(result).to.be.true; + expect(document.querySelector('style#autoconsent-prehide')).not.to.be.null; // check that style element was created + + // we create element ex post and it will be hidden immediately + const createdDiv = document.createElement('div'); + createdDiv.innerText = 'Here is some content'; + createdDiv.id = 'nonexistent'; + + document.getElementsByTagName('body')[0].appendChild(createdDiv); + + expect(createdDiv.checkVisibility({ checkOpacity: true })).to.be.false; + }); +}); diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.html b/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.html new file mode 100644 index 00000000..ecfcb54b --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.html @@ -0,0 +1,12 @@ + + + +
Here is some visible content!
+ + diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.ts b/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.ts new file mode 100644 index 00000000..a4fb477c --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.empty-selector.ts @@ -0,0 +1,27 @@ +import { expect } from '@esm-bundle/chai'; +import { DomActions } from '../../../lib/dom-actions'; +import { instantiateDomActions } from '../utils'; + +// must be run from dom-actions.prehide.empty-selector.html +describe('prehide', () => { + let domActions: DomActions; + + beforeEach(() => { + // Given + domActions = instantiateDomActions(); + }); + + it('returns false when selector is empty string', () => { + // Given + expect(document.getElementById('visible').checkVisibility({ checkOpacity: true })).to.be.true; // establish baseline to prevent false positive + expect(document.querySelector('style#autoconsent-prehide')).to.be.null; // check that style element does not yet exist + + // When + const result = domActions.prehide(''); + + // Then + expect(result).to.be.false; + expect(document.getElementById('visible').checkVisibility({ checkOpacity: true })).to.be.true; + expect(document.querySelector('style#autoconsent-prehide')).not.to.be.null; // check that style element was created + }); +}); diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.html b/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.html new file mode 100644 index 00000000..403e6f94 --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.html @@ -0,0 +1,12 @@ + + + +
Here is some visible content!
+ + diff --git a/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.ts b/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.ts new file mode 100644 index 00000000..ca7599dd --- /dev/null +++ b/tests-wtr/dom-actions/prehide/dom-actions.prehide.preexisting-element.ts @@ -0,0 +1,28 @@ +import { expect } from '@esm-bundle/chai'; +import { instantiateDomActions } from '../utils'; +import { DomActions } from '../../../lib/dom-actions'; + +// must be run from dom-actions.prehide.preexisting-element.html +describe('prehide', () => { + let domActions: DomActions; + + beforeEach(() => { + // Given + domActions = instantiateDomActions(); + }); + + it('prehides preexisting element by selector', async () => { + // Given + expect(document.getElementById('visible').checkVisibility({ checkOpacity: true })).to.be.true; // establish baseline to prevent false positive + expect(document.querySelector('style#autoconsent-prehide')).to.be.null; // check that style element does not yet exist + + // When + const result = domActions.prehide('#visible'); + + // Then + expect(result).to.be.true; + + expect(document.getElementById('visible').checkVisibility({ checkOpacity: true })).to.be.false; + expect(document.querySelector('style#autoconsent-prehide')).not.to.be.null; // check that style element was created + }); +});