Skip to content

Commit

Permalink
improve set-constant, add corresponding helpers and testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 26, 2022
1 parent 6191100 commit 659ec77
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
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

0 comments on commit 659ec77

Please sign in to comment.