datetime_utility_spec.js 3.1 KB
Newer Older
1
import { timeIntervalInWords } from '~/lib/utils/datetime_utility';
2

3 4
(() => {
  describe('Date time utils', () => {
5 6 7 8 9 10 11 12 13 14 15 16 17 18
    describe('timeFor', () => {
      it('returns `past due` when in past', () => {
        const date = new Date();
        date.setFullYear(date.getFullYear() - 1);

        expect(
          gl.utils.timeFor(date),
        ).toBe('Past due');
      });

      it('returns remaining time when in the future', () => {
        const date = new Date();
        date.setFullYear(date.getFullYear() + 1);

19 20 21 22
        // Add a day to prevent a transient error. If date is even 1 second
        // short of a full year, timeFor will return '11 months remaining'
        date.setDate(date.getDate() + 1);

23 24 25 26 27 28
        expect(
          gl.utils.timeFor(date),
        ).toBe('1 year remaining');
      });
    });

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    describe('get day name', () => {
      it('should return Sunday', () => {
        const day = gl.utils.getDayName(new Date('07/17/2016'));
        expect(day).toBe('Sunday');
      });

      it('should return Monday', () => {
        const day = gl.utils.getDayName(new Date('07/18/2016'));
        expect(day).toBe('Monday');
      });

      it('should return Tuesday', () => {
        const day = gl.utils.getDayName(new Date('07/19/2016'));
        expect(day).toBe('Tuesday');
      });

      it('should return Wednesday', () => {
        const day = gl.utils.getDayName(new Date('07/20/2016'));
        expect(day).toBe('Wednesday');
      });

      it('should return Thursday', () => {
        const day = gl.utils.getDayName(new Date('07/21/2016'));
        expect(day).toBe('Thursday');
      });

      it('should return Friday', () => {
        const day = gl.utils.getDayName(new Date('07/22/2016'));
        expect(day).toBe('Friday');
      });

      it('should return Saturday', () => {
        const day = gl.utils.getDayName(new Date('07/23/2016'));
        expect(day).toBe('Saturday');
      });
    });

    describe('get day difference', () => {
      it('should return 7', () => {
        const firstDay = new Date('07/01/2016');
        const secondDay = new Date('07/08/2016');
        const difference = gl.utils.getDayDifference(firstDay, secondDay);
        expect(difference).toBe(7);
      });

      it('should return 31', () => {
        const firstDay = new Date('07/01/2016');
        const secondDay = new Date('08/01/2016');
        const difference = gl.utils.getDayDifference(firstDay, secondDay);
        expect(difference).toBe(31);
      });

      it('should return 365', () => {
        const firstDay = new Date('07/02/2015');
        const secondDay = new Date('07/01/2016');
        const difference = gl.utils.getDayDifference(firstDay, secondDay);
        expect(difference).toBe(365);
      });
    });
  });
89 90 91 92 93 94 95 96 97

  describe('timeIntervalInWords', () => {
    it('should return string with number of minutes and seconds', () => {
      expect(timeIntervalInWords(9.54)).toEqual('9 seconds');
      expect(timeIntervalInWords(1)).toEqual('1 second');
      expect(timeIntervalInWords(200)).toEqual('3 minutes 20 seconds');
      expect(timeIntervalInWords(6008)).toEqual('100 minutes 8 seconds');
    });
  });
98
})();