UNPKG

time-enforcer

Version:

A JavaScript implementation of the Ruby Timecop gem https://github.com/travisjeffery/timecop.

63 lines (53 loc) 2.4 kB
/* globals describe, it, afterEach */ var TimeEnforcer = require('../index.js'), expect = require('expect.js'); //originalDate = new Date(); describe('TimeEnforcer', function() { afterEach(function() { TimeEnforcer.restore(); }); describe('.travel', function() { it('should set the current date to the date passed', function() { TimeEnforcer.travel(2011, 8, 5, 3, 24, 35, 100); var current = new Date(); expect(current.getTime()).to.be.within(1315218275100, 1315218275101); expect(current.valueOf()).to.be.within(1315218275100, 1315218275101); 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.within(100, 101); expect(current.getDay()).to.be(1); //expect(current.toDateString()).to.be('Mon Sep 05 2011'); // need accounting for language variations expect(current.toISOString()).to.match(/^2011-09-05T10:24:35.10\dZ$/); expect(current.toJSON()).to.match(/^2011-09-05T10:24:35.10\dZ$/); //expect(current.toString()).to.match(/^Mon Sep 05 2011 03:24:35 GMT-\d{4}( \(.*\))?$/); // need accounting for language variations expect(current.toTimeString()).to.match(/^03:24:35 GMT(\+|\-)\d{4}( \(.*\))?$/); }); it('should modify Date.now', function() { TimeEnforcer.travel(2011, 7, 4); expect(Date.now()).to.be.within(1312441200000, 1312441201000); }); it('should not modify Date.UTC', function() { TimeEnforcer.travel(2011, 7, 24); expect(Date.UTC(2013, 5, 21, 2, 34)).to.be(1371782040000); }); it('should not modify Date.parse', function() { TimeEnforcer.travel(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.travel(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); }); }); });