| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,6 +67,32 @@ const TIMERS_DEFAULT_INTERVAL = { | |||
| 67 | 67 | setImmediate: -1, | |
| 68 | 68 | }; | |
| 69 | 69 | ||
| 70 | + class Timeout { | ||
| 71 | + constructor(opts) { | ||
| 72 | + this.id = opts.id; | ||
| 73 | + this.callback = opts.callback; | ||
| 74 | + this.runAt = opts.runAt; | ||
| 75 | + this.interval = opts.interval; | ||
| 76 | + this.args = opts.args; | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + hasRef() { | ||
| 80 | + return true; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + ref() { | ||
| 84 | + return this; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + unref() { | ||
| 88 | + return this; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + refresh() { | ||
| 92 | + return this; | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 70 | 96 | class MockTimers { | |
| 71 | 97 | #realSetTimeout; | |
| 72 | 98 | #realClearTimeout; | |
@@ -260,14 +286,16 @@ class MockTimers { | |||
| 260 | 286 | ||
| 261 | 287 | #createTimer(isInterval, callback, delay, ...args) { | |
| 262 | 288 | const timerId = this.#currentTimer++; | |
| 263 | - const timer = { | ||
| 289 | + const opts = { | ||
| 264 | 290 | __proto__: null, | |
| 265 | 291 | id: timerId, | |
| 266 | 292 | callback, | |
| 267 | 293 | runAt: this.#now + delay, | |
| 268 | 294 | interval: isInterval ? delay : undefined, | |
| 269 | 295 | args, | |
| 270 | 296 | }; | |
| 297 | + | ||
| 298 | + const timer = new Timeout(opts); | ||
| 271 | 299 | this.#executionQueue.insert(timer); | |
| 272 | 300 | return timer; | |
| 273 | 301 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -844,4 +844,38 @@ describe('Mock Timers Test Suite', () => { | |||
| 844 | 844 | clearTimeout(id); | |
| 845 | 845 | }); | |
| 846 | 846 | }); | |
| 847 | + | ||
| 848 | + describe('Api should have same public properties as original', () => { | ||
| 849 | + it('should have hasRef', (t) => { | ||
| 850 | + t.mock.timers.enable(); | ||
| 851 | + const timer = setTimeout(); | ||
| 852 | + assert.strictEqual(typeof timer.hasRef, 'function'); | ||
| 853 | + assert.strictEqual(timer.hasRef(), true); | ||
| 854 | + clearTimeout(timer); | ||
| 855 | + }); | ||
| 856 | + | ||
| 857 | + it('should have ref', (t) => { | ||
| 858 | + t.mock.timers.enable(); | ||
| 859 | + const timer = setTimeout(); | ||
| 860 | + assert.ok(typeof timer.ref === 'function'); | ||
| 861 | + assert.deepStrictEqual(timer.ref(), timer); | ||
| 862 | + clearTimeout(timer); | ||
| 863 | + }); | ||
| 864 | + | ||
| 865 | + it('should have unref', (t) => { | ||
| 866 | + t.mock.timers.enable(); | ||
| 867 | + const timer = setTimeout(); | ||
| 868 | + assert.ok(typeof timer.unref === 'function'); | ||
| 869 | + assert.deepStrictEqual(timer.unref(), timer); | ||
| 870 | + clearTimeout(timer); | ||
| 871 | + }); | ||
| 872 | + | ||
| 873 | + it('should have refresh', (t) => { | ||
| 874 | + t.mock.timers.enable(); | ||
| 875 | + const timer = setTimeout(); | ||
| 876 | + assert.ok(typeof timer.refresh === 'function'); | ||
| 877 | + assert.deepStrictEqual(timer.refresh(), timer); | ||
| 878 | + clearTimeout(timer); | ||
| 879 | + }); | ||
| 880 | + }); | ||
| 847 | 881 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments