Skip to content

Commit

Permalink
improve helper
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 26, 2022
1 parent 47b9202 commit 6313c0e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers/prevent-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const isValidCallback = (callback) => {
* @returns {any} number as parsed delay or any input type if `delay` is not parsable
*/
export const parseRawDelay = (delay) => {
const parsedDelay = Math.floor(Number(delay));
const parsedDelay = Math.floor(parseInt(delay, 10));
return typeof parsedDelay === 'number' && !nativeIsNaN(parsedDelay) ? parsedDelay : delay;
};

Expand Down
32 changes: 32 additions & 0 deletions tests/scriptlets/prevent-setInterval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,3 +451,35 @@ test('match any callback + non-number, decimal and string delays', (assert) => {
const intervalTest5 = setInterval(fifth, '10');
testIntervals.push(intervalTest5);
});

test('match any callback, falsy non-numbers delays dont collide with 0 ', (assert) => {
const done = assert.async();
window.one = 'one';
window.two = 'two';
window.three = 'three';
// We need to run our assertion after all timeouts
setTimeout(() => {
assert.equal(window.one, 'one', 'property \'one\' should NOT be changed');
assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed');
assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed');

assert.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);

// run scriptlet code
const scriptletArgs = ['', '0'];
runScriptlet(name, scriptletArgs);

const first = () => { window.one = 'NEW ONE'; };
const intervalTest1 = setInterval(first, 0);
testIntervals.push(intervalTest1);

const second = () => { window.two = 'NEW TWO'; };
const intervalTest2 = setInterval(second, null);
testIntervals.push(intervalTest2);

const third = () => { window.three = 'NEW THREE'; };
const intervalTest3 = setInterval(third, undefined);
testIntervals.push(intervalTest3);
});
32 changes: 32 additions & 0 deletions tests/scriptlets/prevent-setTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,35 @@ test('match any callback + non-number, decimal and string delays', (assert) => {
const timeoutTest5 = setTimeout(fifth, '10');
testTimeouts.push(timeoutTest5);
});

test('match any callback, falsy non-numbers delays dont collide with 0 ', (assert) => {
const done = assert.async();
window.one = 'one';
window.two = 'two';
window.three = 'three';
// We need to run our assertion after all timeouts
nativeSetTimeout(() => {
assert.equal(window.one, 'one', 'property \'one\' should NOT be changed');
assert.equal(window.two, 'NEW TWO', 'property \'two\' should be changed');
assert.equal(window.three, 'NEW THREE', 'property \'three\' should be changed');

assert.strictEqual(window.hit, 'FIRED', 'hit fired');
done();
}, 100);

// run scriptlet code
const scriptletArgs = ['', '0'];
runScriptlet(name, scriptletArgs);

const first = () => { window.one = 'NEW ONE'; };
const timeoutTest1 = setTimeout(first, 0);
testTimeouts.push(timeoutTest1);

const second = () => { window.two = 'NEW TWO'; };
const timeoutTest2 = setTimeout(second, null);
testTimeouts.push(timeoutTest2);

const third = () => { window.three = 'NEW THREE'; };
const timeoutTest3 = setTimeout(third, undefined);
testTimeouts.push(timeoutTest3);
});

0 comments on commit 6313c0e

Please sign in to comment.