Skip to content

Commit

Permalink
add hasRef (#419)
Browse files Browse the repository at this point in the history
* add hasRef attribute

* add test to the hasRef feature

* Run Prettier

* Cleanup and add conditional for non-Node environments

Co-authored-by: Carl-Erik Kopseng <carlerik@gmail.com>
  • Loading branch information
soferdani and fatso83 committed Feb 3, 2022
1 parent 87fd84e commit 5263dae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/fake-timers-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,12 +585,18 @@ function withGlobal(_global) {

if (addTimerReturnsObject) {
const res = {
refed: true,
ref: function () {
this.refed = true;
return res;
},
unref: function () {
this.refed = false;
return res;
},
hasRef: function () {
return this.refed;
},
refresh: function () {
clearTimeout(timer.id);
const args = [timer.func, timer.delay].concat(timer.args);
Expand Down
55 changes: 54 additions & 1 deletion test/fake-timers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe("issue #59", function () {
clock.uninstall();
});
});

describe("issue #73", function () {
it("should install with date object", function () {
const date = new Date("2015-09-25");
Expand Down Expand Up @@ -4834,6 +4833,7 @@ describe("#368 - timeout.refresh setTimeout arguments", function () {
this.skip();
}
});

it("should forward arguments passed to setTimeout", function () {
const clock = FakeTimers.install();
const stub = sinon.stub();
Expand Down Expand Up @@ -5234,3 +5234,56 @@ describe("loop limit stack trace", function () {
});
});
});

describe("Node Timer: ref(), unref(),hasRef()", function () {
let clock;

before(function () {
if (!addTimerReturnsObject) {
this.skip();
}
clock = FakeTimers.install();
});

afterEach(function () {
clock.uninstall();
});

it("should return the ref status as true after initiation", function () {
const stub = sinon.stub();
const refStatusForTimeout = clock.setTimeout(stub, 0).hasRef();
const refStatusForInterval = clock.setInterval(stub, 0).hasRef();
assert.isTrue(refStatusForTimeout);
assert.isTrue(refStatusForInterval);
clock.uninstall();
});

it("should return the ref status as false after using unref", function () {
const stub = sinon.stub();
const refStatusForTimeout = clock.setTimeout(stub, 0).unref().hasRef();
const refStatusForInterval = clock
.setInterval(stub, 0)
.unref()
.hasRef();
assert.isFalse(refStatusForInterval);
assert.isFalse(refStatusForTimeout);
clock.uninstall();
});

it("should return the ref status as true after using unref and then ref ", function () {
const stub = sinon.stub();
const refStatusForTimeout = clock
.setTimeout(stub, 0)
.unref()
.ref()
.hasRef();
const refStatusForInterval = clock
.setInterval(stub, 0)
.unref()
.ref()
.hasRef();
assert.isTrue(refStatusForInterval);
assert.isTrue(refStatusForTimeout);
clock.uninstall();
});
});

0 comments on commit 5263dae

Please sign in to comment.