Skip to content

Commit

Permalink
add regexp to extraMatch parsing with a testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 13, 2022
1 parent 99fc8fc commit 3477858
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/scriptlets/trusted-click-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
const THROTTLE_DELAY_MS = 20;
const COOKIE_MATCH_MARKER = 'cookie:';
const LOCAL_STORAGE_MATCH_MARKER = 'localStorage:';
const COMMON_DELIMITER = ',';
const SELECTORS_DELIMITER = ',';
const COOKIE_STRING_DELIMITER = ';';
const EXTRA_MATCH_DELIMITER = /(,\s*){1}(?=cookie:|localStorage:)/;

let parsedDelay;
if (delay) {
Expand All @@ -92,7 +93,7 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
if (extraMatch) {
// Get all match marker:value pairs from argument
const parsedExtraMatch = extraMatch
.split(COMMON_DELIMITER)
.split(EXTRA_MATCH_DELIMITER)
.map((matchStr) => matchStr.trim());

// Filter match pairs by marker
Expand Down Expand Up @@ -163,7 +164,7 @@ export function trustedClickElement(source, selectors, extraMatch = '', delay =
* - prevent selectors from being queried multiple times
*/
let selectorsSequence = selectors
.split(COMMON_DELIMITER)
.split(SELECTORS_DELIMITER)
.map((selector) => selector.trim());

const createElementObj = (element) => {
Expand Down
38 changes: 38 additions & 0 deletions tests/scriptlets/trusted-click-element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,41 @@ test('extraMatch - single localStorage match, not matched', (assert) => {
}, 150);
window.localStorage.clear();
});

test('extraMatch - complex string+regex cookie input & whitespaces & comma in regex, matched', (assert) => {
const cookieKey1 = 'first';
const cookieVal1 = 'true';
const cookieData1 = prepareCookie(cookieKey1, cookieVal1);
const cookieKey2 = 'sec';
const cookieVal2 = '1-1';
const cookieData2 = prepareCookie(cookieKey2, cookieVal2);
const cookieKey3 = 'third';
const cookieVal3 = 'true';
const cookieData3 = prepareCookie(cookieKey3, cookieVal3);

document.cookie = cookieData1;
document.cookie = cookieData2;
document.cookie = cookieData3;

const EXTRA_MATCH_STR = 'cookie:/firs/=true,cookie:sec=/(1-1){1,2}/, cookie:third=true';

const ELEM_COUNT = 1;
// Check elements for being clicked and hit func execution
const ASSERTIONS = ELEM_COUNT + 1;
assert.expect(ASSERTIONS);
const done = assert.async();

const selectorsString = `#${PANEL_ID} > #${CLICKABLE_NAME}${ELEM_COUNT}`;

runScriptlet(name, [selectorsString, EXTRA_MATCH_STR]);
const panel = createPanel();
const clickable = createClickable(1);
panel.appendChild(clickable);

setTimeout(() => {
assert.ok(clickable.getAttribute('clicked'), 'Element should be clicked');
assert.strictEqual(window.hit, 'FIRED', 'hit func executed');
done();
}, 150);
clearCookie(cookieKey1);
});

0 comments on commit 3477858

Please sign in to comment.