-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdate.component.spec.ts
More file actions
90 lines (74 loc) · 2.8 KB
/
Copy pathdate.component.spec.ts
File metadata and controls
90 lines (74 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
89
90
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { format } from 'date-fns';
import { DateDisplayComponent } from './date.component';
describe('DateDisplayComponent', () => {
let component: DateDisplayComponent;
let fixture: ComponentFixture<DateDisplayComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [DateDisplayComponent],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(DateDisplayComponent);
component = fixture.componentInstance;
});
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
it('should format date value', () => {
const dateStr = '2023-04-29';
fixture.componentRef.setInput('value', dateStr);
component.ngOnInit();
fixture.detectChanges();
const expected = format(new Date(dateStr), 'P');
expect(component.formattedDate).toBe(expected);
});
it('should display dash for null', () => {
fixture.componentRef.setInput('value', null);
component.ngOnInit();
fixture.detectChanges();
expect(component.formattedDate).toBeUndefined();
});
it('should display "today" instead of relative time for same-day date', () => {
const recentDate = new Date(Date.now() - 1000 * 60); // 1 minute ago — same calendar day
fixture.componentRef.setInput('value', recentDate.toISOString());
fixture.componentRef.setInput('widgetStructure', {
widget_params: { formatDistanceWithinHours: 48 },
});
component.ngOnInit();
fixture.detectChanges();
expect(component.formattedDate).toBe('today');
});
it('should show relative date for non-today date within formatDistanceWithinHours', () => {
const yesterdayNoon = new Date();
yesterdayNoon.setDate(yesterdayNoon.getDate() - 1);
yesterdayNoon.setHours(12, 0, 0, 0);
fixture.componentRef.setInput('value', yesterdayNoon.toISOString());
fixture.componentRef.setInput('widgetStructure', {
widget_params: { formatDistanceWithinHours: 48 },
});
component.ngOnInit();
fixture.detectChanges();
expect(component.formattedDate).toContain('ago');
});
it('should expose exact date as tooltip via fullDate', () => {
const dateStr = '2023-04-29';
fixture.componentRef.setInput('value', dateStr);
component.ngOnInit();
fixture.detectChanges();
expect(component.fullDate).toBe(format(new Date(dateStr), 'PPP'));
});
it('should set fullDate tooltip even when displaying "today"', () => {
const recentDate = new Date(Date.now() - 1000 * 60);
fixture.componentRef.setInput('value', recentDate.toISOString());
fixture.componentRef.setInput('widgetStructure', {
widget_params: { formatDistanceWithinHours: 48 },
});
component.ngOnInit();
fixture.detectChanges();
expect(component.formattedDate).toBe('today');
expect(component.fullDate).toBe(format(recentDate, 'PPP'));
});
});