Skip to content
This repository has been archived by the owner on Dec 24, 2023. It is now read-only.

Commit

Permalink
test: fix UT's
Browse files Browse the repository at this point in the history
  • Loading branch information
wswebcreation committed Mar 1, 2020
1 parent c306763 commit 571a852
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,99 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 1`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 2`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 3`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 4`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 5`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 6`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 7`] = `""`;

exports[`hideRemoveElements should be able to find and hide a single element based on a css selector 8`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide elements based on a css selector 1`] = `
NodeList [
<span
class="hide"
>
Hello
</span>,
<span
class="hide"
>
Hello
</span>,
<span
class="hide"
>
Hello
</span>,
<span
class="hide"
>
Hello
</span>,
]
`;

exports[`hideRemoveElements should be able to find and hide elements based on a css selector 2`] = `
NodeList [
<span
class="hide"
style="visibility: hidden;"
>
Hello
</span>,
<span
class="hide"
style="visibility: hidden;"
>
Hello
</span>,
<span
class="hide"
style="visibility: hidden;"
>
Hello
</span>,
<span
class="hide"
style="visibility: hidden;"
>
Hello
</span>,
]
`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 1`] = `""`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 2`] = `""`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 3`] = `""`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 4`] = `""`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 5`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 6`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 7`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide elements based on xpath 8`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide single element based on xpath 1`] = `""`;

exports[`hideRemoveElements should be able to find and hide single element based on xpath 2`] = `""`;

exports[`hideRemoveElements should be able to find and hide single element based on xpath 3`] = `"hidden"`;

exports[`hideRemoveElements should be able to find and hide single element based on xpath 4`] = `"hidden"`;

exports[`hideRemoveElements should be able to hide elements and put them back again 1`] = `""`;

exports[`hideRemoveElements should be able to hide elements and put them back again 2`] = `""`;
Expand Down
114 changes: 114 additions & 0 deletions lib/clientSideScripts/hideRemoveElements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,118 @@ describe('hideRemoveElements', () => {
expect((<HTMLElement>document.querySelector('#id-3')).style.display).toMatchSnapshot();
expect((<HTMLElement[]><unknown>document.querySelectorAll('.remove'))[2].style.display).toMatchSnapshot();
});

it('should be able to find and hide single element based on xpath', () => {
document.body.innerHTML =
'<div>' +
' <span id="id-1">Hello</span>' +
' <span id="id-2">Hello</span>' +
' <div>' +
' <span id="id-3">Hello</span>' +
' <span id="id-4">Hello</span>' +
' </div>' +
'</div>';

// Check not hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();

hideRemoveElements({
hide: [<any>{selector: '//span[@id=\'id-1\']'}, <any>{selector: '//span[@id=\'id-3\']'}],
remove: [],
},
true,
);

// Check hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();
});

it('should be able to find and hide elements based on xpath', () => {
document.body.innerHTML =
'<div>' +
' <span id="id-1">Hello</span>' +
' <span id="id-2">Hello</span>' +
' <div>' +
' <span id="id-3">Hello</span>' +
' <span id="id-4">Hello</span>' +
' </div>' +
'</div>';

// Check not hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-2')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-4')).style.visibility).toMatchSnapshot();

hideRemoveElements({
hide: [[<any>{selector: '//span'}]],
remove: [],
},
true,
);

// Check hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-2')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-4')).style.visibility).toMatchSnapshot();
});

it('should be able to find and hide a single element based on a css selector', () => {
document.body.innerHTML =
'<div>' +
' <span id="id-1">Hello</span>' +
' <span id="id-2">Hello</span>' +
' <div>' +
' <span id="id-3">Hello</span>' +
' <span class="hide">Hello</span>' +
' </div>' +
'</div>';

// Check not hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-2')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('.hide')).style.visibility).toMatchSnapshot();

hideRemoveElements({
hide: [<any>{selector: '.hide'}],
remove: [],
},
true,
);

// Check hidden
expect((<HTMLElement>document.querySelector('#id-1')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-2')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('#id-3')).style.visibility).toMatchSnapshot();
expect((<HTMLElement>document.querySelector('.hide')).style.visibility).toMatchSnapshot();
});

it('should be able to find and hide elements based on a css selector', () => {
document.body.innerHTML =
'<div>' +
' <span class="hide">Hello</span>' +
' <span class="hide">Hello</span>' +
' <div>' +
' <span class="hide">Hello</span>' +
' <span class="hide">Hello</span>' +
' </div>' +
'</div>';

// Check not hidden
expect((<HTMLElement[]><unknown>document.querySelectorAll('.hide'))).toMatchSnapshot();

hideRemoveElements({
hide: [[<any>{selector: '.hide'}, <any>{selector: '.hide'}]],
remove: [],
},
true,
);

// Check hidden
expect((<HTMLElement[]><unknown>document.querySelectorAll('.hide'))).toMatchSnapshot();
});
});
12 changes: 6 additions & 6 deletions lib/clientSideScripts/hideRemoveElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ export default function hideRemoveElements(
try {
if (singleElement) {
// @ts-ignore
return setPropertyToElement(document.querySelector(el.selector), prop, hideRemove);
setPropertyToElement(document.querySelector(el.selector), prop, hideRemove);
} else {
// @ts-ignore
document.querySelectorAll(el.selector).forEach(singleEl =>
setPropertyToElement(singleEl, prop, hideRemove)
);
}

// @ts-ignore
return document.querySelectorAll(el.selector).forEach(singleEl =>
setPropertyToElement(singleEl, prop, hideRemove)
);
} catch (e) {
// 99.99% sure that we have XPATH here
// @ts-ignore
Expand Down

0 comments on commit 571a852

Please sign in to comment.