twoDigitToFullYear.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import {twoDigitToFullYear} from '../twoDigitToFullYear.js';
  2. const expect = require('unexpected');
  3. const sinon = require('sinon');
  4. describe('twoDigitToFullYear()', function () {
  5. it('converts 2-digit year to full year using 50 for the future years range by default', function () {
  6. let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
  7. expect(twoDigitToFullYear(0), 'to be', 2000);
  8. expect(twoDigitToFullYear(20), 'to be', 2020);
  9. expect(twoDigitToFullYear(70), 'to be', 2070);
  10. expect(twoDigitToFullYear(71), 'to be', 1971);
  11. clock.restore();
  12. clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
  13. expect(twoDigitToFullYear(0), 'to be', 2100);
  14. expect(twoDigitToFullYear(30), 'to be', 2130);
  15. expect(twoDigitToFullYear(31), 'to be', 2031);
  16. expect(twoDigitToFullYear(80), 'to be', 2080);
  17. clock.restore();
  18. });
  19. it('uses sepecified future year range if it is given', function () {
  20. let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
  21. expect(twoDigitToFullYear(40, 20), 'to be', 2040);
  22. expect(twoDigitToFullYear(40, 19), 'to be', 1940);
  23. expect(twoDigitToFullYear(95, 75), 'to be', 2095);
  24. expect(twoDigitToFullYear(95, 74), 'to be', 1995);
  25. clock.restore();
  26. clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
  27. expect(twoDigitToFullYear(0, 20), 'to be', 2100);
  28. expect(twoDigitToFullYear(0, 19), 'to be', 2000);
  29. expect(twoDigitToFullYear(55, 75), 'to be', 2155);
  30. expect(twoDigitToFullYear(55, 74), 'to be', 2055);
  31. clock.restore();
  32. });
  33. it('uses the century of baseDate for conversion if it is given', function () {
  34. const year2020 = new Date(2020, 0, 1);
  35. expect(twoDigitToFullYear(40, 20, year2020), 'to be', 2040);
  36. expect(twoDigitToFullYear(40, 19, year2020), 'to be', 1940);
  37. expect(twoDigitToFullYear(95, 75, year2020), 'to be', 2095);
  38. expect(twoDigitToFullYear(95, 74, year2020), 'to be', 1995);
  39. const year2280 = new Date(2280, 0, 1);
  40. expect(twoDigitToFullYear(0, 20, year2280), 'to be', 2300);
  41. expect(twoDigitToFullYear(0, 19, year2280), 'to be', 2200);
  42. expect(twoDigitToFullYear(55, 75, year2280), 'to be', 2355);
  43. expect(twoDigitToFullYear(55, 74, year2280), 'to be', 2255);
  44. });
  45. });