index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const should = require('should');
  2. const MockDate = require('../lib/mockdate');
  3. describe('MockDate', function() {
  4. const mockDate = '1/1/2000';
  5. const currentYear = new Date().getFullYear();
  6. const nativeToString = Date.toString();
  7. beforeEach(function () {
  8. MockDate.set(new Date(mockDate));
  9. });
  10. afterEach(function () {
  11. MockDate.reset();
  12. });
  13. it('should check date constructor name', function() {
  14. should.equal(Date.name, 'Date');
  15. });
  16. it('should throw for bad date', function() {
  17. should.throws(function () {
  18. MockDate.set('40/40/2000');
  19. }, 'mockdate: The time set is an invalid date: 40/40/2000');
  20. should.throws(function () {
  21. MockDate.set(NaN);
  22. }, 'mockdate: The time set is an invalid date: NaN');
  23. });
  24. it('should override new Date()', function() {
  25. should.equal(new Date().toString(), new Date(mockDate).toString());
  26. should.equal(new Date().getFullYear(), 2000);
  27. });
  28. it('should override Date.now()', function() {
  29. should.equal(Date.now(), new Date(mockDate).valueOf());
  30. });
  31. it('should override Date.parse()', function() {
  32. should.equal('807926400000', Date.parse('Wed, 09 Aug 1995 00:00:00 GMT'));
  33. });
  34. it('should allow mock dates to show up as real dates using instanceof', function() {
  35. should.ok(new Date() instanceof Date);
  36. });
  37. it('should have the same toString as the native Date object does', function() {
  38. should.equal(Date.toString(), nativeToString);
  39. });
  40. it('should be able to create a specific date from a timestamp', function() {
  41. var date = new Date(807926400000);
  42. should.equal('Wed, 09 Aug 1995 00:00:00 GMT', date.toUTCString());
  43. });
  44. it('should be able to create a specific date from year, month', function() {
  45. var locDate = new Date(1995, 7);
  46. var utcMs = locDate.valueOf()-locDate.getTimezoneOffset()*60*1000;
  47. var utcDate = new Date(utcMs);
  48. should.equal('Tue, 01 Aug 1995 00:00:00 GMT', utcDate.toUTCString());
  49. });
  50. it('should be able to create a specific date from year, month, date', function() {
  51. var locDate = new Date(1995, 7, 9);
  52. var utcMs = locDate.valueOf()-locDate.getTimezoneOffset()*60*1000;
  53. var utcDate = new Date(utcMs);
  54. should.equal('Wed, 09 Aug 1995 00:00:00 GMT', utcDate.toUTCString());
  55. });
  56. it('should respect a date of 0', function() {
  57. var locDate = new Date(1995, 7, 0);
  58. should.equal(locDate.getDate(), 31);
  59. });
  60. it('should be able to create a date correctly from the epoch', function() {
  61. MockDate.set(0);
  62. should.equal('Thu, 01 Jan 1970 00:00:00 GMT', new Date().toUTCString());
  63. });
  64. it('should revert correctly', function() {
  65. MockDate.reset();
  66. should.equal(new Date().getFullYear(), currentYear);
  67. should.ok(Date.toString().indexOf('native'));
  68. });
  69. });