-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathDateFormat.spec.tsx
More file actions
152 lines (133 loc) · 4.15 KB
/
DateFormat.spec.tsx
File metadata and controls
152 lines (133 loc) · 4.15 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { RenderResult } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { DateFormat } from './index';
import { TimeFormatType } from '../../lib/dateFormat';
const renderComponent = ({
date,
type,
className,
prefix,
}: {
date: string | number | Date;
type: TimeFormatType;
className?: string;
prefix?: string;
}): RenderResult => {
return render(
<DateFormat
date={date}
type={type}
className={className}
prefix={prefix}
/>,
);
};
const date = new Date(2024, 6, 6, 12, 30, 30);
beforeEach(() => {
jest.useFakeTimers().setSystemTime(date);
});
afterEach(() => {
jest.useRealTimers();
});
describe('date format post type', () => {
it('should render post time format', async () => {
renderComponent({
date: '2021-08-01T00:00:00Z',
type: TimeFormatType.Post,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Aug 01, 2021');
});
it('should render post time format as now', async () => {
renderComponent({ date, type: TimeFormatType.Post });
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Now');
});
it('should render post time format as today', async () => {
renderComponent({
date: date.setHours(date.getHours() - 2),
type: TimeFormatType.Post,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Today');
});
it('should render post time format as yesterday', async () => {
renderComponent({
date: date.setDate(date.getDate() - 1),
type: TimeFormatType.Post,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Yesterday');
});
});
describe('date format comment type', () => {
it('should render comment time format', async () => {
renderComponent({
date: '2021-08-01T00:00:00Z',
type: TimeFormatType.Comment,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('3y');
});
it('should render comment time format as now', async () => {
renderComponent({
date,
type: TimeFormatType.Comment,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('now');
});
it('should render comment time format as minutes', async () => {
renderComponent({
date: date.setMinutes(date.getMinutes() - 2),
type: TimeFormatType.Comment,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('2m');
});
it('should render comment time format as hours', async () => {
renderComponent({
date: date.setHours(date.getHours() - 2),
type: TimeFormatType.Comment,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('2h');
});
});
describe('date format read history type', () => {
it('should render comment time format', async () => {
renderComponent({
date: '2021-08-01T00:00:00Z',
type: TimeFormatType.ReadHistory,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Sun, 1 Aug 2021');
});
it('should render comment time format as today', async () => {
renderComponent({
date: date.setMinutes(date.getMinutes() - 2),
type: TimeFormatType.ReadHistory,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Today');
});
it('should render comment time format as yesterday', async () => {
renderComponent({
date: date.setDate(date.getDate() - 1),
type: TimeFormatType.ReadHistory,
});
const element = screen.getByRole('time');
expect(element).toHaveTextContent('Yesterday');
});
});
describe('invalid date handling', () => {
it('should return null for undefined date', () => {
render(<DateFormat type={TimeFormatType.Post} />);
expect(screen.queryByRole('time')).not.toBeInTheDocument();
});
it('should return null for malformed date string', () => {
render(<DateFormat date="not-a-date" type={TimeFormatType.Post} />);
expect(screen.queryByRole('time')).not.toBeInTheDocument();
});
});