Skip to content

Commit

Permalink
improve tests & add emptyobject in chain case
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Mar 29, 2022
1 parent 5c18130 commit 6406257
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/helpers/get-property-in-chain.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isEmptyObject } from './object-utils';

/**
* @typedef Chain
* @property {Object} base
Expand Down Expand Up @@ -33,6 +35,11 @@ export function getPropertyInChain(base, chain) {
const nextBase = base[prop];
chain = chain.slice(pos + 1);

if ((base instanceof Object || typeof base === 'object') && isEmptyObject(base)) {
// for empty objects in chain
return { base, prop, chain };
}

if (nextBase === null) {
return { base, prop, chain };
}
Expand Down
8 changes: 7 additions & 1 deletion src/scriptlets/set-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
toRegExp,
matchStackTrace,
nativeIsNaN,
isEmptyObject,
} from '../helpers';

/* eslint-disable max-len */
Expand Down Expand Up @@ -240,6 +241,11 @@ export function setConstant(source, property, value, stack) {
return;
}

// Empty object prop in chain
if ((base instanceof Object || typeof base === 'object') && isEmptyObject(base)) {
trapProp(base, prop, true, inChainPropHandler);
}

// Defined prop in chain
const propValue = owner[prop];
if (propValue instanceof Object || (typeof propValue === 'object' && propValue !== null)) {
Expand All @@ -249,7 +255,6 @@ export function setConstant(source, property, value, stack) {
// Undefined prop in chain
trapProp(owner, prop, true, inChainPropHandler);
};

setChainPropAccess(window, property);
}

Expand Down Expand Up @@ -278,4 +283,5 @@ setConstant.injections = [
toRegExp,
matchStackTrace,
nativeIsNaN,
isEmptyObject,
];
37 changes: 32 additions & 5 deletions tests/scriptlets/set-constant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,39 @@ if (!isSupported) {
clearGlobalProps(illegalNumberProp);
});

test('keep other keys after setting a value', (assert) => {
window.testObj = {
testChain: null,
};
runScriptletFromTag('testObj.testChain.testProp', 'true');
window.testObj.testChain = {
testProp: false,
otherProp: 'someValue',
testMethod: () => { },
};
assert.strictEqual(window.testObj.testChain.testProp, true, 'target prop set');
assert.strictEqual(window.testObj.testChain.otherProp, 'someValue', 'target prop set');
assert.strictEqual(typeof window.testObj.testChain.testMethod, 'function', 'target prop set');
clearGlobalProps('testObj');
});

test('set value on null prop', (assert) => {
// end prop is null
window.test = null;
runScriptletFromTag('test', '15');
assert.strictEqual(window.test, 15, 'null end prop changed');
clearGlobalProps('test');
window.nullProp = null;
runScriptletFromTag('nullProp', '15');
assert.strictEqual(window.nullProp, 15, 'null end prop changed');
clearGlobalProps('nullProp');
});

test('set value through chain with empty object', (assert) => {
window.emptyObj = {};
runScriptletFromTag('emptyObj.a.prop', 'true');
window.emptyObj.a = {};
assert.strictEqual(window.emptyObj.a.prop, true, 'target prop set');
clearGlobalProps('emptyObj');
});

test('set value through chain with null', (assert) => {
// null prop in chain
window.nullChain = {
nullProp: null,
Expand All @@ -170,7 +196,8 @@ if (!isSupported) {
window.nullChain.nullProp = {
endProp: false,
};
assert.strictEqual(window.nullChain.nullProp.endProp, true, 'nsdfsdxfhgsd');
assert.strictEqual(window.nullChain.nullProp.endProp, true, 'chain with null trapped');
clearGlobalProps('nullChain');
});

test('sets values to the chained properties', (assert) => {
Expand Down

0 comments on commit 6406257

Please sign in to comment.