|
| 1 | +import { Time } from './time' |
| 2 | +describe('Performs time class unit test', () => { |
| 3 | + test('test add function(time + 1) should return correct value', () => { |
| 4 | + const time = new Time('2023-03-03') |
| 5 | + time.utc(true) |
| 6 | + time.add(1) |
| 7 | + |
| 8 | + expect(time.toDate().getTime().toString()).toStrictEqual('1677801600001') |
| 9 | + }) |
| 10 | + test('test add function(time + 1 min), should return correct value', () => { |
| 11 | + const time = new Time('2023-03-03') |
| 12 | + time.utc(true) |
| 13 | + const plusMin = time.add(1, 'minute') |
| 14 | + expect(plusMin.toDate().getTime().toString()).toStrictEqual('1677801660000') |
| 15 | + }) |
| 16 | + test('test validate time, should return wrong value ', () => { |
| 17 | + const time = new Time('wrong input') |
| 18 | + time.utc(true) |
| 19 | + expect(time.isValid).toStrictEqual(false) |
| 20 | + }) |
| 21 | + test('test validate time, should return correct value', () => { |
| 22 | + const time = new Time() |
| 23 | + time.utc(true) |
| 24 | + expect(time.isValid).toStrictEqual(true) |
| 25 | + }) |
| 26 | + test('test clone time, should return correct value', () => { |
| 27 | + const time1 = new Time() |
| 28 | + time1.utc(true) |
| 29 | + const clone = time1.clone() |
| 30 | + expect(time1.toDate()).toStrictEqual(clone.toDate()) |
| 31 | + }) |
| 32 | + test('test diff func, should return negative value', () => { |
| 33 | + const time1 = new Time('2023-03-03') |
| 34 | + const time2 = new Time('2023-03-04') |
| 35 | + expect(time1.diff(time2)).toStrictEqual(-86400000) |
| 36 | + }) |
| 37 | + |
| 38 | + test('test diff func, should return positive value', () => { |
| 39 | + const time1 = new Time('2023-03-04') |
| 40 | + const time2 = new Time('2023-03-03') |
| 41 | + expect(time1.diff(time2)).toStrictEqual(86400000) |
| 42 | + }) |
| 43 | + |
| 44 | + test('test timestamp, should return correct value', () => { |
| 45 | + const time = new Time('2023-03-03') |
| 46 | + time.utc(true) |
| 47 | + expect(time.timestamp).toStrictEqual(1677801600) |
| 48 | + }) |
| 49 | + |
| 50 | + test('test isSame, should return correct value', () => { |
| 51 | + const time1 = new Time('2023-03-03') |
| 52 | + const time2 = new Time('2023-03-03') |
| 53 | + expect(time2.isSame(time1.toDate())).toEqual(true) |
| 54 | + }) |
| 55 | + |
| 56 | + test('test isSam func, should return wrong value', () => { |
| 57 | + const time1 = new Time('2023-03-03') |
| 58 | + const time2 = new Time('2023-03-04') |
| 59 | + expect(time2.isSame(time1.toDate())).toEqual(false) |
| 60 | + }) |
| 61 | + |
| 62 | + test('test after func, should return correct value', () => { |
| 63 | + const time1 = new Time('2023-03-03') |
| 64 | + const time2 = new Time('2023-03-04') |
| 65 | + expect(time2.isAfter(time1.toDate())).toEqual(true) |
| 66 | + }) |
| 67 | + |
| 68 | + test('test after func, should return wrong value', () => { |
| 69 | + const time1 = new Time('2023-03-03') |
| 70 | + const time2 = new Time('2023-03-04') |
| 71 | + expect(time1.isAfter(time2.toDate())).toEqual(false) |
| 72 | + }) |
| 73 | + |
| 74 | + test('test before func, should return correct value', () => { |
| 75 | + const time1 = new Time('2023-03-03') |
| 76 | + const time2 = new Time('2023-03-04') |
| 77 | + expect(time1.isBefore(time2.toDate())).toEqual(true) |
| 78 | + }) |
| 79 | + |
| 80 | + test('test before func, should return wrong value', () => { |
| 81 | + const time1 = new Time('2023-03-03') |
| 82 | + const time2 = new Time('2023-03-04') |
| 83 | + expect(time2.isBefore(time1.toDate())).toEqual(false) |
| 84 | + }) |
| 85 | + |
| 86 | + test('test beforeOrSame func, should return correct value', () => { |
| 87 | + const time1 = new Time('2023-03-03') |
| 88 | + const time2 = new Time('2023-03-03') |
| 89 | + expect(time1.isSameOrBefore(time2.toDate())).toEqual(true) |
| 90 | + }) |
| 91 | + |
| 92 | + test('test beforeOrSame func, should return correct value', () => { |
| 93 | + const time1 = new Time('2023-03-03') |
| 94 | + const time2 = new Time('2023-03-04') |
| 95 | + expect(time1.isSameOrBefore(time2.toDate())).toEqual(true) |
| 96 | + }) |
| 97 | + |
| 98 | + test('test beforeOrSame func, should return wrong value', () => { |
| 99 | + const time1 = new Time('2023-03-03') |
| 100 | + const time2 = new Time('2023-03-04') |
| 101 | + expect(time2.isSameOrBefore(time1.toDate())).toEqual(false) |
| 102 | + }) |
| 103 | + |
| 104 | + test('test afterOrSame func, should return correct value', () => { |
| 105 | + const time1 = new Time('2023-03-03') |
| 106 | + const time2 = new Time('2023-03-03') |
| 107 | + expect(time2.isSameOrAfter(time1.toDate())).toEqual(true) |
| 108 | + }) |
| 109 | + |
| 110 | + test('test afterOrSame func, should return correct value', () => { |
| 111 | + const time1 = new Time('2023-03-03') |
| 112 | + const time2 = new Time('2023-03-04') |
| 113 | + expect(time2.isSameOrAfter(time1.toDate())).toEqual(true) |
| 114 | + }) |
| 115 | + |
| 116 | + test('test afterOrSame func, should return wrong value', () => { |
| 117 | + const time1 = new Time('2023-03-03') |
| 118 | + const time2 = new Time('2023-03-04') |
| 119 | + expect(time1.isSameOrAfter(time2.toDate())).toEqual(false) |
| 120 | + }) |
| 121 | + |
| 122 | + test('test between func, should return correct value', () => { |
| 123 | + const time1 = new Time('2023-03-02') |
| 124 | + const time2 = new Time('2023-03-04') |
| 125 | + const timeBetween = new Time('2023-03-03') |
| 126 | + expect(timeBetween.isBetween(time1.toDate(), time2.toDate())).toEqual(true) |
| 127 | + }) |
| 128 | + |
| 129 | + test('test between func, should return wrong value', () => { |
| 130 | + const time1 = new Time('2023-03-02') |
| 131 | + const time2 = new Time('2023-03-04') |
| 132 | + const timeBetween = new Time('2023-06-03') |
| 133 | + expect(timeBetween.isBetween(time1.toDate(), time2.toDate())).toEqual(false) |
| 134 | + }) |
| 135 | + |
| 136 | + test('test RFC3339, should return correct RFC3339', () => { |
| 137 | + const time = new Time('2023-03-03') |
| 138 | + time.utc(true) |
| 139 | + expect(time.RFC3339).toEqual('2023-03-03T00:00:00Z') |
| 140 | + }) |
| 141 | + |
| 142 | + test('test ISO, should return should correct ISO', () => { |
| 143 | + const time = new Time('2023-03-03') |
| 144 | + time.utc(true) |
| 145 | + expect(time.ISO).toEqual('2023-03-03T00:00:00.000Z') |
| 146 | + }) |
| 147 | + |
| 148 | + test('test subtract func, should return correct value', () => { |
| 149 | + const time = new Time('2023-03-03') |
| 150 | + time.utc(true) |
| 151 | + const subTime = time.subtract(1, 'day') |
| 152 | + expect(subTime.toDate().getTime()).toStrictEqual(1677715200000) |
| 153 | + }) |
| 154 | + |
| 155 | + test('test ms, should return correct value', () => { |
| 156 | + const time = new Time('2023-03-03') |
| 157 | + time.utc(true) |
| 158 | + expect(time.ms).toStrictEqual(1677801600000) |
| 159 | + }) |
| 160 | +}) |
0 commit comments