Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hasRef #419

Merged
merged 4 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});