await-timeout
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -23,4 +23,4 @@ /** | ||
// Static `.set()` helper | ||
Timeout.set = ms => new Timeout().set(ms); | ||
Timeout.set = (ms, msg) => new Timeout().set(ms, msg); | ||
export default Timeout; |
@@ -1,2 +0,2 @@ | ||
/*! await-timeout v0.1.3 */ | ||
/*! await-timeout v0.1.4 */ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
@@ -130,4 +130,4 @@ if(typeof exports === 'object' && typeof module === 'object') | ||
Timeout.set = function (ms) { | ||
return new Timeout().set(ms); | ||
Timeout.set = function (ms, msg) { | ||
return new Timeout().set(ms, msg); | ||
}; | ||
@@ -134,0 +134,0 @@ |
{ | ||
"name": "await-timeout", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A Promise-based API for setTimeout / clearTimeout", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -10,2 +10,2 @@ require('babel-register')({ | ||
global.Timeout = require(process.env.LIB_PATH || '../index'); | ||
global.wait = ms => new Promise(r => setTimeout(r, ms)); | ||
global.sleep = ms => new Promise(r => setTimeout(r, ms)); |
describe('Timeout.set', function () { | ||
beforeEach(function () { | ||
this.triggered = false; | ||
}); | ||
it('should be exported', function () { | ||
@@ -8,9 +12,16 @@ assert.ok(Timeout.set instanceof Function); | ||
it('should resolve after required', function () { | ||
const t = Date.now(); | ||
return Timeout.set(50) | ||
.then(() => { | ||
const duration = Date.now() - t; | ||
assert.ok(Math.abs(duration - 50) < 10); | ||
}); | ||
Timeout.set(50).then(() => this.triggered = true); | ||
return Promise.all([ | ||
sleep(45).then(() => assert.equal(this.triggered, false)), | ||
sleep(55).then(() => assert.equal(this.triggered, true)), | ||
]); | ||
}); | ||
it('should reject after delay if message is defined', function () { | ||
return Timeout.set(50, 'Timeout') | ||
.then( | ||
() => assert.fail('should throw'), | ||
e => assert.equal(e.message, 'Timeout') | ||
); | ||
}); | ||
}); |
describe('Timeout', function () { | ||
beforeEach(function () { | ||
this.timeout = new Timeout(); | ||
this.triggered = false; | ||
}); | ||
afterEach(function () { | ||
this.timeout.clear(); | ||
}); | ||
it('should export class', function () { | ||
@@ -11,22 +16,28 @@ assert.ok(Timeout instanceof Function); | ||
it('should resolve after required ms', function () { | ||
const t = Date.now(); | ||
return this.timeout.set(50) | ||
.then(() => { | ||
const duration = Date.now() - t; | ||
assert.ok(Math.abs(duration - 50) < 10); | ||
}); | ||
this.timeout.set(50).then(() => this.triggered = true); | ||
return Promise.all([ | ||
sleep(45).then(() => assert.equal(this.triggered, false)), | ||
sleep(55).then(() => assert.equal(this.triggered, true)), | ||
]); | ||
}); | ||
it('should reject after delay ms if message defined', function () { | ||
it('should reject after delay if message is defined', function () { | ||
return this.timeout.set(50, 'Timeout') | ||
.catch(e => assert.equal(e.message, 'Timeout')); | ||
.then( | ||
() => assert.fail('should throw'), | ||
e => assert.equal(e.message, 'Timeout') | ||
); | ||
}); | ||
it('should clear timeout', function () { | ||
return Promise.race([ | ||
this.timeout.set(50).then(() => 'triggered'), | ||
wait(20).then(() => this.timeout.clear()) | ||
]) | ||
.then(result => assert.equal(result, undefined)); | ||
this.timeout.set(50).then(() => this.triggered = true); | ||
sleep(20).then(() => this.timeout.clear()); | ||
return sleep(60).then(() => assert.equal(this.triggered, false)); | ||
}); | ||
it('should re-set timeout', function () { | ||
this.timeout.set(50).then(() => this.triggered = true); | ||
this.timeout.set(20).then(() => this.triggered = true); | ||
return sleep(30).then(() => assert.equal(this.triggered, true)); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
19512
270