From 37442d25e76339fe0387653c63e9b5e964db8321 Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Thu, 6 Oct 2022 15:43:54 +0300 Subject: [PATCH] add localStorage tests --- src/scriptlets/trusted-click-element.js | 3 +- .../scriptlets/trusted-click-element.test.js | 53 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/scriptlets/trusted-click-element.js b/src/scriptlets/trusted-click-element.js index 38507f47..35a3f122 100644 --- a/src/scriptlets/trusted-click-element.js +++ b/src/scriptlets/trusted-click-element.js @@ -100,7 +100,8 @@ export function trustedClickElement(source, selectors, delay, extraMatch = '') { } if (localStorageMatch.length > 0) { - const localStorageMatched = cookieMatch.every((str) => window.localStorage.getItem(str)); + const localStorageMatched = localStorageMatch + .every((str) => window.localStorage.getItem(str)); if (!localStorageMatched) { return; } diff --git a/tests/scriptlets/trusted-click-element.test.js b/tests/scriptlets/trusted-click-element.test.js index 5f22d371..4c0abd38 100644 --- a/tests/scriptlets/trusted-click-element.test.js +++ b/tests/scriptlets/trusted-click-element.test.js @@ -214,3 +214,56 @@ test('extraMatch - single cookie match, not matched', (assert) => { }, 150); clearCookie(); }); + +test('extraMatch - single localStorage match, matched', (assert) => { + const itemName = 'item'; + window.localStorage.setItem(itemName, 'value'); + const EXTRA_MATCH_STR = `localStorage:${itemName}`; + + 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); + window.localStorage.clear(); +}); + +test('extraMatch - single localStorage match, not matched', (assert) => { + const itemName = 'item'; + const itemName2 = 'key'; + window.localStorage.setItem(itemName, 'value'); + const EXTRA_MATCH_STR = `localStorage:${itemName2}`; + + 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.notOk(clickable.getAttribute('clicked'), 'Element should not be clicked'); + assert.strictEqual(window.hit, undefined, 'hit should not fire'); + done(); + }, 150); + window.localStorage.clear(); +});