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

(DomActions) WTR tests for elementSelector() #495

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions tests-wtr/dom-actions/dom-actions.click.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { expect } from '@esm-bundle/chai';
import {DomActions} from "../../lib/dom-actions";
import {instantiateDomActions} from "./utils";

function instantiateDomActions() {
// @ts-expect-error we don't need to add a full AutoConsent instance, DomActions only needs config.logs from it
return new DomActions({config: {logs: {rulesteps: false, lifecycle: false, evals: false, errors: false, messages: false}}})
}

// must be run from dom-actions.test.html
// must be run from dom-actions.click.html
describe('click', () => {
let clickCounter1: number
let clickCounter2: number
Expand Down Expand Up @@ -100,7 +95,6 @@ describe('click', () => {
document.body.appendChild(shadowDiv)
const shadow = shadowDiv.attachShadow({ mode: 'open' })
const shadowButton = document.createElement('button')
shadowButton.innerText = '0'
muodov marked this conversation as resolved.
Show resolved Hide resolved
shadow.appendChild(shadowButton)
shadowButton.addEventListener('click', () => {
clickCounterShadowRoot++
Expand Down
30 changes: 30 additions & 0 deletions tests-wtr/dom-actions/dom-actions.element-selector.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<body>
<script type="module">
import {runTests} from '@web/test-runner-mocha';

runTests(async () => {
await import('./dom-actions.element-selector')
});
</script>

<div class="outer">
<div class="inner">
<p id="innermost">Innermost</p>
</div>
</div>
<div class="inner">
<p id="not-innermost">This should not be matched if searching by .outer first</p>
</div>

<div>
<button id="button1">Accept all</button>
<button id="button2">Reject all</button>
</div>

<div>
<button id="button3">Accept all</button>
</div>

</body>
</html>
91 changes: 91 additions & 0 deletions tests-wtr/dom-actions/dom-actions.element-selector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {expect} from '@esm-bundle/chai';
import {instantiateDomActions} from "./utils";

// must be run from dom-actions.element-selector.html
describe('elementSelector', () => {
muodov marked this conversation as resolved.
Show resolved Hide resolved
const specialCasesThatShouldReturnEmptyArray = ['aria/', 'text/', 'pierce/']

specialCasesThatShouldReturnEmptyArray.forEach((selector) => {
it(`should return empty array if string selector starts with "${selector}"`, () => {
// Given
const domActions = instantiateDomActions();

// When
const elements = domActions.elementSelector(selector + 'something');

// Then
expect(elements).to.be.empty
})
})
it('should return elements by xpath if string selector starts with "xpath/"', () => {
// Given
const domActions = instantiateDomActions();

// When
const elements = domActions.elementSelector('xpath///button[contains(., \'Accept all\')]');

// Then
expect(elements).to.deep.eq([
document.getElementById('button1'),
document.getElementById('button3'),
])
})
it('should return elements by string selector in querySelectorAll', () => {
// Given
const domActions = instantiateDomActions();

// When
const elements = domActions.elementSelector('button');

// Then
expect(elements).to.deep.eq([
document.getElementById('button1'),
document.getElementById('button2'),
document.getElementById('button3'),
])
})
it('should return empty array if any selector from array does not produce a match', () => {
// Given
const domActions = instantiateDomActions();

// When
const elements = domActions.elementSelector(['.outer', '.outer', '.inner'])

// Then
expect(elements).to.deep.eq([])
})
it('should return results if all selectors from array produce a match in one another', () => {
// Given
const domActions = instantiateDomActions();

// When
const elements = domActions.elementSelector(['.outer', '.inner', 'p'])

// Then
expect(elements).to.deep.eq([
document.getElementById('innermost')
])
})
it('should return elements by querySelectorAll from shadow root if available when shadow root selected by previous selector in array', () => {
// Given
const domActions = instantiateDomActions();

const shadowDiv = document.createElement('div')
shadowDiv.id = 'shadow'
document.body.appendChild(shadowDiv)
const shadow = shadowDiv.attachShadow({mode: 'open'})
const shadowButton1 = document.createElement('button')
shadow.appendChild(shadowButton1)
const shadowButton2 = document.createElement('button')
shadow.appendChild(shadowButton2)

// When
const elements = domActions.elementSelector(['#shadow', 'button'])

// Then
expect(elements).to.deep.eq([
shadowButton1,
shadowButton2,
])
})
})
6 changes: 6 additions & 0 deletions tests-wtr/dom-actions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DomActions } from "../../lib/dom-actions";

export const instantiateDomActions = () => {
// @ts-expect-error we don't need to add a full AutoConsent instance, DomActions only needs config.logs from it
return new DomActions({config: {logs: {rulesteps: false, lifecycle: false, evals: false, errors: false, messages: false}}})
}
Loading