time-enforcer
Version:
A JavaScript implementation of the Ruby Timecop gem https://github.com/travisjeffery/timecop.
95 lines (83 loc) • 3.45 kB
JavaScript
/* globals describe, it, afterEach */
var TimeEnforcer = require('../index.js'),
expect = require('expect.js');
//originalDate = new Date();
describe('TimeEnforcer', function() {
afterEach(function() {
TimeEnforcer.restore();
});
describe('.freeze', function() {
it('should stop time indefinitely', function(done) {
TimeEnforcer.freeze(2011, 3, 4, 1, 2, 0, 0);
var start = new Date();
expect(start.getTime()).to.be(1301904120000);
setTimeout(function() {
var future = new Date();
expect(future.getTime()).to.be(1301904120000);
setTimeout(function() {
var farFuture = new Date();
expect(farFuture.getTime()).to.be(1301904120000);
done();
}, 500);
}, 1000);
TimeEnforcer.restore(); // unfreeze time in order to allow the timers to increment
});
it('should not freeze time outside of the scope function', function(done) {
TimeEnforcer.freeze(2011, 3, 4, 1, 2, 0, 0, function() {
var start = new Date();
expect(start.getTime()).to.be(1301904120000);
setTimeout(function() {
var future = new Date();
expect(future.getTime()).to.be(1301904120000);
setTimeout(function() {
var farFuture = new Date();
expect(farFuture.getTime()).to.be(1301904120000);
done();
}, 500);
}, 1000);
});
});
it('should set the current date to the date passed', function() {
TimeEnforcer.freeze(2011, 8, 5, 3, 24, 35, 100);
var current = new Date();
expect(current.getTime()).to.be(1315218275100);
expect(current.valueOf()).to.be(1315218275100);
expect(current.getFullYear()).to.be(2011);
expect(current.getMonth()).to.be(8);
expect(current.getDate()).to.be(5);
expect(current.getHours()).to.be(3);
expect(current.getMinutes()).to.be(24);
expect(current.getSeconds()).to.be(35);
expect(current.getMilliseconds()).to.be(100);
expect(current.getDay()).to.be(1);
//expect(current.toDateString()).to.be('Mon Sep 05 2011'); // needs language support
expect(current.toISOString()).to.match(/^2011-09-05T10:24:35.100Z$/);
expect(current.toJSON()).to.match(/^2011-09-05T10:24:35.100Z$/);
//expect(current.toString()).to.match(/^Mon Sep 05 2011 03:24:35 GMT-\d{4} \(.*\)$/); // needs language support
expect(current.toTimeString()).to.match(/^03:24:35 GMT(\+|\-)\d{4} \(.*\)$/);
});
it('should modify Date.now', function() {
TimeEnforcer.freeze(2011, 7, 4);
expect(Date.now()).to.be.within(1312441200000, 1312441201000);
});
it('should not modify Date.UTC', function() {
TimeEnforcer.freeze(2011, 7, 24);
expect(Date.UTC(2013, 5, 21, 2, 34)).to.be(1371782040000);
});
it('should not modify Date.parse', function() {
TimeEnforcer.freeze(2011, 7, 24);
expect(Date.parse('2013-06-21T02:34:00.100Z')).to.be(1371782040100);
});
it('should restore the date once outside of the scope', function() {
var startDate = new Date(), endDate, called = false;
TimeEnforcer.freeze(2010, 6, 24, function() {
called = true;
var date = new Date();
expect(date.getFullYear()).to.be(2010);
});
endDate = new Date();
expect(endDate.getDate()).to.be(startDate.getDate());
expect(called).to.be(true);
});
});
});