|
| 1 | +import {IntlRelativeTimePipe} from './relative-time.pipe'; |
| 2 | +import * as dayjs from 'dayjs' |
| 3 | +import {fakeAsync, TestBed, tick} from "@angular/core/testing"; |
| 4 | +import {INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS} from "./relative-time-pipe-default-options"; |
| 5 | +import {INTL_LOCALES} from "../locale"; |
| 6 | +import {ChangeDetectorRef} from "@angular/core"; |
| 7 | + |
| 8 | +describe('RelativeTimePipe', () => { |
| 9 | + let testUnit: IntlRelativeTimePipe; |
| 10 | + |
| 11 | + it('should create an instance', () => { |
| 12 | + testUnit = new IntlRelativeTimePipe(); |
| 13 | + expect(testUnit).toBeTruthy(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('parsing', () => { |
| 17 | + beforeEach(() => { |
| 18 | + testUnit = new IntlRelativeTimePipe('en-US'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should handle null values', () => { |
| 22 | + expect(testUnit.transform(null)).toEqual(null); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should handle undefined values', () => { |
| 26 | + expect(testUnit.transform(undefined)).toEqual(null); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should handle empty strings', () => { |
| 30 | + expect(testUnit.transform('')).toEqual(null); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should throw an error when an invalid string is passed', () => { |
| 34 | + expect(() => testUnit.transform('someInvalidDate')).toThrowError('someInvalidDate is not a valid date'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should throw an error when an invalid date is passed', () => { |
| 38 | + expect(() => testUnit.transform(new Date('invalid'))).toThrowError('Invalid Date is not a valid date'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should support string value', () => { |
| 42 | + expect(testUnit.transform(new Date().toISOString())).toEqual('in 0 minutes'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should support number value', () => { |
| 46 | + expect(testUnit.transform(new Date().getTime())).toEqual('in 0 minutes'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should support Date value', () => { |
| 50 | + expect(testUnit.transform(new Date())).toEqual('in 0 minutes'); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('years', () => { |
| 54 | + it('should transform a date one year in past', () => { |
| 55 | + const date = dayjs().subtract(1, 'year').subtract(1, 'second').toDate(); |
| 56 | + |
| 57 | + expect(testUnit.transform(date)).toEqual('1 year ago'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should transform a date almost 3 years in future', () => { |
| 61 | + const date = dayjs().add(365 * 3, 'days').subtract(1, 'second').toDate(); |
| 62 | + |
| 63 | + expect(testUnit.transform(date)).toEqual('in 2 years'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('months', () => { |
| 68 | + it('should transform a date 1 month in future', () => { |
| 69 | + const date = dayjs().add(31, 'days').add(1, 'second').toDate(); |
| 70 | + |
| 71 | + expect(testUnit.transform(date)).toEqual('in 1 month'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should transform a date almost 12 months in past', () => { |
| 75 | + const date = dayjs().subtract(30 * 12, 'days').add(1, 'second').toDate(); |
| 76 | + |
| 77 | + expect(testUnit.transform(date)).toEqual('11 months ago'); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('weeks', () => { |
| 82 | + it('should transform a date 1 week in future', () => { |
| 83 | + const date = dayjs().add(1, 'week').add(1, 'second').toDate(); |
| 84 | + |
| 85 | + expect(testUnit.transform(date)).toEqual('in 1 week'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should transform a date almost 4 weeks in past', () => { |
| 89 | + const date = dayjs().subtract(4, 'weeks').add(1, 'second').toDate(); |
| 90 | + |
| 91 | + expect(testUnit.transform(date)).toEqual('3 weeks ago'); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('days', () => { |
| 96 | + it('should transform a date 1 day in future', () => { |
| 97 | + const date = dayjs().add(1, 'day').add(1, 'second').toDate(); |
| 98 | + |
| 99 | + expect(testUnit.transform(date)).toEqual('in 1 day'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should transform a date almost 7 days in past', () => { |
| 103 | + const date = dayjs().subtract(7, 'days').add(1, 'second').toDate(); |
| 104 | + |
| 105 | + expect(testUnit.transform(date)).toEqual('6 days ago'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('hours', () => { |
| 110 | + it('should transform a date 1 hour in future', () => { |
| 111 | + const date = dayjs().add(1, 'hour').add(1, 'second').toDate(); |
| 112 | + |
| 113 | + expect(testUnit.transform(date)).toEqual('in 1 hour'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should transform a date almost 24 hours in past', () => { |
| 117 | + const date = dayjs().subtract(24, 'hours').add(1, 'second').toDate(); |
| 118 | + |
| 119 | + expect(testUnit.transform(date)).toEqual('23 hours ago'); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('minutes', () => { |
| 124 | + it('should transform a date 1 minute in future', () => { |
| 125 | + const date = dayjs().add(1, 'minute').add(1, 'second').toDate(); |
| 126 | + |
| 127 | + expect(testUnit.transform(date)).toEqual('in 1 minute'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should transform a date almost 59 minutes in past', () => { |
| 131 | + const date = dayjs().subtract(60, 'minutes').add(1, 'second').toDate(); |
| 132 | + |
| 133 | + expect(testUnit.transform(date)).toEqual('59 minutes ago'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should transform a date almost than 1 minute in past', () => { |
| 138 | + const date = dayjs().subtract(1, 'minute').add(1, 'second').toDate(); |
| 139 | + |
| 140 | + expect(testUnit.transform(date)).toEqual('in 0 minutes'); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('options', () => { |
| 145 | + beforeEach(() => { |
| 146 | + TestBed.configureTestingModule({ |
| 147 | + providers: [ |
| 148 | + IntlRelativeTimePipe, |
| 149 | + { |
| 150 | + provide: INTL_RELATIVE_TIME_PIPE_DEFAULT_OPTIONS, |
| 151 | + useValue: {numeric: 'auto', style: 'short'}, |
| 152 | + }, |
| 153 | + { |
| 154 | + provide: INTL_LOCALES, |
| 155 | + useValue: 'en-US', |
| 156 | + }, |
| 157 | + ], |
| 158 | + }); |
| 159 | + testUnit = TestBed.inject(IntlRelativeTimePipe); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should respect the default options', () => { |
| 163 | + expect(testUnit.transform(new Date())).toEqual('this minute') |
| 164 | + }); |
| 165 | + |
| 166 | + it('should give the passed options a higher priority', () => { |
| 167 | + expect(testUnit.transform(new Date(), {numeric: 'always'})).toEqual('in 0 min.'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should apply the locale from the passed options', () => { |
| 171 | + expect(testUnit.transform(new Date(), {locale: 'de-DE'})).toEqual('in dieser Minute'); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should fall back to the default locale', () => { |
| 176 | + TestBed.configureTestingModule({providers: [IntlRelativeTimePipe]}); |
| 177 | + |
| 178 | + const result1 = TestBed.inject(IntlRelativeTimePipe).transform(new Date()); |
| 179 | + const result2 = new IntlRelativeTimePipe(navigator.language).transform(new Date()); |
| 180 | + |
| 181 | + expect(result1).toEqual(result2); |
| 182 | + }); |
| 183 | + |
| 184 | + describe('timer', () => { |
| 185 | + const cdrMock = {markForCheck: jasmine.createSpy()} as unknown as ChangeDetectorRef; |
| 186 | + |
| 187 | + beforeEach(() => { |
| 188 | + testUnit = new IntlRelativeTimePipe(null, null, cdrMock) |
| 189 | + }); |
| 190 | + |
| 191 | + it('should mark for check once after 1 minute', fakeAsync(() => { |
| 192 | + testUnit.transform(0); |
| 193 | + tick(60000); |
| 194 | + |
| 195 | + expect(cdrMock.markForCheck).toHaveBeenCalledTimes(1); |
| 196 | + |
| 197 | + testUnit.ngOnDestroy(); |
| 198 | + })); |
| 199 | + |
| 200 | + it('should mark for check 10 times after 10 minutes', fakeAsync(() => { |
| 201 | + testUnit.transform(new Date()); |
| 202 | + tick(600000); |
| 203 | + |
| 204 | + expect(cdrMock.markForCheck).toHaveBeenCalledTimes(10); |
| 205 | + |
| 206 | + testUnit.ngOnDestroy(); |
| 207 | + })); |
| 208 | + |
| 209 | + afterEach(() => { |
| 210 | + (cdrMock.markForCheck as jasmine.Spy).calls.reset(); |
| 211 | + }); |
| 212 | + }); |
| 213 | +}); |
0 commit comments