formatTime.spec.js 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import { formatTime } from '@/utils/index.js'
  2. describe('Utils:formatTime', () => {
  3. const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
  4. const retrofit = 5 * 1000
  5. it('ten digits timestamp', () => {
  6. expect(formatTime((d / 1000).toFixed(0))).toBe('7月13日17时54分')
  7. })
  8. it('test now', () => {
  9. expect(formatTime(+new Date() - 1)).toBe('刚刚')
  10. })
  11. it('less two minute', () => {
  12. expect(formatTime(+new Date() - 60 * 2 * 1000 + retrofit)).toBe('2分钟前')
  13. })
  14. it('less two hour', () => {
  15. expect(formatTime(+new Date() - 60 * 60 * 2 * 1000 + retrofit)).toBe('2小时前')
  16. })
  17. it('less one day', () => {
  18. expect(formatTime(+new Date() - 60 * 60 * 24 * 1 * 1000)).toBe('1天前')
  19. })
  20. it('more than one day', () => {
  21. expect(formatTime(d)).toBe('7月13日17时54分')
  22. })
  23. it('format', () => {
  24. expect(formatTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54')
  25. expect(formatTime(d, '{y}-{m}-{d}')).toBe('2018-07-13')
  26. expect(formatTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54')
  27. })
  28. })