Skip to content

Commit

Permalink
add localStorage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 6, 2022
1 parent ce08b2a commit 37442d2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/scriptlets/trusted-click-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
53 changes: 53 additions & 0 deletions tests/scriptlets/trusted-click-element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit 37442d2

Please sign in to comment.