Skip to content

Commit

Permalink
AG-13199 improve set-constant: noopCallbackFunc, noopThrow
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-13199 to release/v1.8

Squashed commit of the following:

commit 8a95806
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 27 12:53:22 2022 +0300

    tweak trusted-set-cookie test

commit f7e2f08
Merge: d681b99 22e41f8
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 27 12:28:44 2022 +0300

    merge release

commit d681b99
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Dec 27 12:10:17 2022 +0300

    improve changelog

commit d1206f0
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Dec 26 20:43:06 2022 +0300

    fix changelog

commit 34a6b6c
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Dec 26 19:20:57 2022 +0300

    update changelog

commit 659ec77
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Dec 26 18:44:17 2022 +0300

    improve set-constant, add corresponding helpers and testcases
  • Loading branch information
stanislav-atr committed Dec 27, 2022
1 parent 22e41f8 commit 419d4a0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased 1.8.x

### Added
- `noopThrow` and `noopCallbackFunc` prop values for `set-constant` scriptlet

### Changed

- add decimal delay matching for `prevent-setInterval` and `prevent-setTimeout` [#247](https://github.com/AdguardTeam/Scriptlets/issues/247)
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/noop-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
export const noopFunc = () => { };

/**
* Function return noopFunc
* @returns {Function}
*/
export const noopCallbackFunc = () => noopFunc;

/**
* Function returns null
* @return {null} null
Expand Down Expand Up @@ -47,6 +53,14 @@ export const noopArray = () => [];
*/
export const noopObject = () => ({});

/**
* Function throws an error
* @throws
*/
export const noopThrow = () => {
throw new Error();
};

/**
* Function returns Promise.reject()
*/
Expand Down
10 changes: 10 additions & 0 deletions src/scriptlets/set-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
logMessage,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
noopThrow,
noopPromiseReject,
noopPromiseResolve,
getPropertyInChain,
Expand Down Expand Up @@ -53,8 +55,10 @@ import {
* - `emptyObj` - empty object
* - `emptyArr` - empty array
* - `noopFunc` - function with empty body
* - `noopCallbackFunc` - function returning noopFunc
* - `trueFunc` - function returning true
* - `falseFunc` - function returning false
* - `noopThrow` - function throwing an error
* - `noopPromiseResolve` - function returning Promise object that is resolved with an empty response
* - `noopPromiseReject` - function returning Promise.reject()
* - `''` - empty string
Expand Down Expand Up @@ -112,10 +116,14 @@ export function setConstant(source, property, value, stack) {
constantValue = emptyObj;
} else if (value === 'noopFunc') {
constantValue = noopFunc;
} else if (value === 'noopCallbackFunc') {
constantValue = noopCallbackFunc;
} else if (value === 'trueFunc') {
constantValue = trueFunc;
} else if (value === 'falseFunc') {
constantValue = falseFunc;
} else if (value === 'noopThrow') {
constantValue = noopThrow;
} else if (value === 'noopPromiseResolve') {
constantValue = noopPromiseResolve;
} else if (value === 'noopPromiseReject') {
Expand Down Expand Up @@ -283,8 +291,10 @@ setConstant.injections = [
noopArray,
noopObject,
noopFunc,
noopCallbackFunc,
trueFunc,
falseFunc,
noopThrow,
noopPromiseReject,
noopPromiseResolve,
getPropertyInChain,
Expand Down
18 changes: 17 additions & 1 deletion tests/helpers/noop.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { noopPromiseResolve } from '../../src/helpers';
import {
noopPromiseResolve,
noopCallbackFunc,
noopFunc,
noopThrow,
} from '../../src/helpers';

const { test, module } = QUnit;
const name = 'scriptlets-redirects helpers';
Expand All @@ -22,3 +27,14 @@ test('Test noopPromiseResolve for valid response props', async (assert) => {
assert.ok(Array.isArray(arrBody) && !arrBody.length);
assert.strictEqual(responseWithType.type, TEST_TYPE);
});

test('noopCallbackFunc returns noopFunc', async (assert) => {
const func = noopCallbackFunc();
assert.ok(typeof func === 'function', 'returns function');
assert.strictEqual(func.toString(), noopFunc.toString(), 'returns empty function');
assert.strictEqual(func(), undefined, 'function returns undefined');
});

test('noopThrow throws an error', async (assert) => {
assert.throws(() => noopThrow(), 'noopThrow throws an error');
});
14 changes: 14 additions & 0 deletions tests/scriptlets/set-constant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ if (!isSupported) {
assert.strictEqual(window[noopFuncProp](), undefined);
clearGlobalProps(noopFuncProp);

// setting constant to noopCallbackFunc;
const noopCallbackFuncProp = 'noopCallbackFunc';
runScriptletFromTag(noopCallbackFuncProp, 'noopCallbackFunc');
const noopFuncCb = window[noopCallbackFuncProp]();
assert.ok(typeof noopFuncCb === 'function', 'returns function');
assert.strictEqual(noopFuncCb(), undefined, 'function returns undefined');
clearGlobalProps(noopCallbackFuncProp);

// setting constant to noopCallbackFunc;
const noopThrowProp = 'noopThrow';
runScriptletFromTag(noopThrowProp, 'noopThrow');
assert.throws(() => window[noopThrowProp]()(), 'noopThrowProp throws an error');
clearGlobalProps(noopThrowProp);

// setting constant to trueFunc;
const trueFuncProp = 'trueFuncProp';
runScriptletFromTag(trueFuncProp, 'trueFunc');
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/trusted-set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test('Set cookie with current time value', (assert) => {

// Some time will pass between calling scriptlet
// and qunit running assertion
const tolerance = 20;
const tolerance = 100;
const cookieValue = parseCookieString(document.cookie)[cName];
const currentTime = new Date().getTime();
const timeDiff = currentTime - cookieValue;
Expand Down

0 comments on commit 419d4a0

Please sign in to comment.