wwf
3 天以前 a430284aa21e3ae1f0d5654e55b2ad2852519cc2
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
import { renderHook } from '@testing-library/react'
import useTimestamp from './use-timestamp'
 
jest.mock('@/context/app-context', () => ({
  useAppContext: jest.fn(() => ({
    userProfile: {
      id: '8b18e24b-1ac8-4262-aa5c-e9aa95c76846',
      name: 'test',
      avatar: null,
      avatar_url: null,
      email: 'test@dify.ai',
      is_password_set: false,
      interface_language: 'zh-Hans',
      interface_theme: 'light',
      timezone: 'Asia/Shanghai',
      last_login_at: 1744188761,
      last_login_ip: '127.0.0.1',
      created_at: 1728444483,
    },
  })),
}))
 
describe('useTimestamp', () => {
  describe('formatTime', () => {
    it('should format unix timestamp correctly', () => {
      const { result } = renderHook(() => useTimestamp())
      const timestamp = 1704132000
 
      expect(result.current.formatTime(timestamp, 'YYYY-MM-DD HH:mm:ss'))
        .toBe('2024-01-02 02:00:00')
    })
 
    it('should format with different patterns', () => {
      const { result } = renderHook(() => useTimestamp())
      const timestamp = 1704132000
 
      expect(result.current.formatTime(timestamp, 'MM/DD/YYYY'))
        .toBe('01/02/2024')
 
      expect(result.current.formatTime(timestamp, 'HH:mm'))
        .toBe('02:00')
    })
  })
 
  describe('formatDate', () => {
    it('should format date string correctly', () => {
      const { result } = renderHook(() => useTimestamp())
      const dateString = '2024-01-01T12:00:00Z'
 
      expect(result.current.formatDate(dateString, 'YYYY-MM-DD HH:mm:ss'))
        .toBe('2024-01-01 20:00:00')
    })
 
    it('should format with different patterns', () => {
      const { result } = renderHook(() => useTimestamp())
      const dateString = '2024-01-01T12:00:00Z'
 
      expect(result.current.formatDate(dateString, 'MM/DD/YYYY'))
        .toBe('01/01/2024')
 
      expect(result.current.formatDate(dateString, 'HH:mm'))
        .toBe('20:00')
    })
  })
})