Skip to content

Commit

Permalink
add testcase for a special param value of set-attr
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Feb 2, 2024
1 parent 88c365f commit 24bbfb1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/helpers/attribute-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { hit } from './hit';
* @param value
* @returns
*/
const defaultAttributeSetter = (
export const defaultAttributeSetter = (
elem: Element,
attribute: string,
value: string,
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/set-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
nativeIsNaN,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
Expand Down Expand Up @@ -165,6 +166,7 @@ setAttr.injections = [
nativeIsNaN,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
Expand Down
2 changes: 2 additions & 0 deletions src/scriptlets/trusted-set-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
nativeIsNaN,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
Expand Down Expand Up @@ -114,6 +115,7 @@ trustedSetAttr.injections = [
nativeIsNaN,
// following helpers should be imported and injected
// because they are used by helpers above
defaultAttributeSetter,
logMessage,
throttle,
hit,
Expand Down
44 changes: 29 additions & 15 deletions tests/scriptlets/set-attr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { runScriptlet, clearGlobalProps } from '../helpers';
const { test, module } = QUnit;
const name = 'set-attr';

const afterEach = () => {
clearGlobalProps('hit', '__debug');
};

module(name, { afterEach });

const createHit = () => {
const beforeEach = () => {
window.__debug = () => {
window.hit = 'FIRED';
};
};

const afterEach = () => {
clearGlobalProps('hit', '__debug');
};

module(name, { beforeEach, afterEach });

const createElem = (className) => {
const elem = document.createElement('div');
if (className) {
Expand Down Expand Up @@ -48,7 +48,6 @@ test('Checking if alias name works', (assert) => {
});

test('selector + attr + eligible number', (assert) => {
createHit();
const attr = 'test-attr';
const value = '1234';
const matchClassName = 'testClass';
Expand Down Expand Up @@ -81,7 +80,6 @@ test('selector + attr + eligible number', (assert) => {
});

test('selector + attr + empty string', (assert) => {
createHit();
const attr = 'test-attr';
const value = '';
const matchClassName = 'testClass';
Expand All @@ -105,7 +103,6 @@ test('selector + attr + empty string', (assert) => {
});

test('selector + attr + empty string', (assert) => {
createHit();
const attr = 'test-attr';
const matchClassName = 'testClass';
const mismatchClassName = 'none';
Expand All @@ -129,7 +126,6 @@ test('selector + attr + empty string', (assert) => {
});

test('selector + attr + negative number', (assert) => {
createHit();
const attr = 'test-attr';
const value = '-100';
const matchClassName = 'testClass';
Expand All @@ -148,7 +144,6 @@ test('selector + attr + negative number', (assert) => {
});

test('selector + attr + too big of a number', (assert) => {
createHit();
const attr = 'test-attr';
const value = '33000';
const matchClassName = 'testClass';
Expand All @@ -167,7 +162,6 @@ test('selector + attr + too big of a number', (assert) => {
});

test('selector + attr + true', (assert) => {
createHit();
const attr = 'test-attr-true';
const value = 'true';
const matchClassName = 'testClassTrue';
Expand Down Expand Up @@ -200,7 +194,6 @@ test('selector + attr + true', (assert) => {
});

test('selector + attr + False', (assert) => {
createHit();
const attr = 'test-attr-False';
const value = 'False';
const matchClassName = 'testClassFalse';
Expand Down Expand Up @@ -233,7 +226,6 @@ test('selector + attr + False', (assert) => {
});

test('selector + attr + not allowed string', (assert) => {
createHit();
const attr = 'test-attr';
const value = 'trueNotAllowed';
const matchClassName = 'testClassNotAllowed';
Expand All @@ -250,3 +242,25 @@ test('selector + attr + not allowed string', (assert) => {
// Clean up test elements
matchElem.remove();
});

test('copying attribute value', (assert) => {
const attrName = 'test-attr';
const attrValue = 'test-value';
const specialName = 'special-attr';
const specialValue = `[${attrName}]`;
const matchClassName = 'testClass';

const targetElement = document.createElement('div');
targetElement.setAttribute(attrName, attrValue);
targetElement.classList.add(matchClassName);
document.body.appendChild(targetElement);

const scriptletArgs = [`.${matchClassName}`, specialName, specialValue];
runScriptlet(name, scriptletArgs);

assert.ok(targetElement.getAttribute(attrName) === attrValue, 'Original attribute is intact');
assert.ok(targetElement.getAttribute(specialName) === attrValue, 'New attr value is copied correctly');
assert.strictEqual(window.hit, 'FIRED');

targetElement.remove();
});

0 comments on commit 24bbfb1

Please sign in to comment.