| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 33c5f7f commit a8c773a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,12 +79,15 @@ const TIMERS_DEFAULT_INTERVAL = { | |||
| 79 | 79 | }; | |
| 80 | 80 | ||
| 81 | 81 | class Timeout { | |
| 82 | + #clear; | ||
| 83 | + | ||
| 82 | 84 | constructor(opts) { | |
| 83 | 85 | this.id = opts.id; | |
| 84 | 86 | this.callback = opts.callback; | |
| 85 | 87 | this.runAt = opts.runAt; | |
| 86 | 88 | this.interval = opts.interval; | |
| 87 | 89 | this.args = opts.args; | |
| 90 | + this.#clear = opts.clear; | ||
| 88 | 91 | } | |
| 89 | 92 | ||
| 90 | 93 | hasRef() { | |
@@ -102,6 +105,15 @@ class Timeout { | |||
| 102 | 105 | refresh() { | |
| 103 | 106 | return this; | |
| 104 | 107 | } | |
| 108 | + | ||
| 109 | + close() { | ||
| 110 | + this.#clear(this); | ||
| 111 | + return this; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + [SymbolDispose]() { | ||
| 115 | + this.#clear(this); | ||
| 116 | + } | ||
| 105 | 117 | } | |
| 106 | 118 | ||
| 107 | 119 | class MockTimers { | |
@@ -329,6 +341,7 @@ class MockTimers { | |||
| 329 | 341 | runAt: this.#now + delay, | |
| 330 | 342 | interval: isInterval ? delay : undefined, | |
| 331 | 343 | args, | |
| 344 | + clear: this.#clearTimeout, | ||
| 332 | 345 | }; | |
| 333 | 346 | ||
| 334 | 347 | const timer = new Timeout(opts); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -266,6 +266,32 @@ describe('Mock Timers Test Suite', () => { | |||
| 266 | 266 | assert.deepStrictEqual(fn.mock.calls[0].arguments, args); | |
| 267 | 267 | }); | |
| 268 | 268 | ||
| 269 | + it('should expose Timeout.prototype[Symbol.dispose]', (t) => { | ||
| 270 | + t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
| 271 | + const fn = t.mock.fn(); | ||
| 272 | + const timeout = globalThis.setTimeout(fn, 2000); | ||
| 273 | + | ||
| 274 | + assert.strictEqual(typeof timeout[Symbol.dispose], 'function'); | ||
| 275 | + | ||
| 276 | + timeout[Symbol.dispose](); | ||
| 277 | + t.mock.timers.tick(2000); | ||
| 278 | + | ||
| 279 | + assert.strictEqual(fn.mock.callCount(), 0); | ||
| 280 | + }); | ||
| 281 | + | ||
| 282 | + it('should expose Timeout.prototype.close()', (t) => { | ||
| 283 | + t.mock.timers.enable({ apis: ['setTimeout'] }); | ||
| 284 | + const fn = t.mock.fn(); | ||
| 285 | + const timeout = globalThis.setTimeout(fn, 2000); | ||
| 286 | + | ||
| 287 | + assert.strictEqual(typeof timeout.close, 'function'); | ||
| 288 | + assert.strictEqual(timeout.close(), timeout); | ||
| 289 | + | ||
| 290 | + t.mock.timers.tick(2000); | ||
| 291 | + | ||
| 292 | + assert.strictEqual(fn.mock.callCount(), 0); | ||
| 293 | + }); | ||
| 294 | + | ||
| 269 | 295 | it('should keep setTimeout working if timers are disabled', (t, done) => { | |
| 270 | 296 | const now = Date.now(); | |
| 271 | 297 | const timeout = 2; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments