parseTime.spec.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { parseTime } from '@/utils/index.js'
  2. describe('Utils:parseTime', () => {
  3. const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01"
  4. it('timestamp', () => {
  5. expect(parseTime(d)).toBe('2018-07-13 17:54:01')
  6. })
  7. it('timestamp string', () => {
  8. expect(parseTime((d + ''))).toBe('2018-07-13 17:54:01')
  9. })
  10. it('ten digits timestamp', () => {
  11. expect(parseTime((d / 1000).toFixed(0))).toBe('2018-07-13 17:54:01')
  12. })
  13. it('new Date', () => {
  14. expect(parseTime(new Date(d))).toBe('2018-07-13 17:54:01')
  15. })
  16. it('format', () => {
  17. expect(parseTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54')
  18. expect(parseTime(d, '{y}-{m}-{d}')).toBe('2018-07-13')
  19. expect(parseTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54')
  20. })
  21. it('get the day of the week', () => {
  22. expect(parseTime(d, '{a}')).toBe('五') // 星期五
  23. })
  24. it('get the day of the week', () => {
  25. expect(parseTime(+d + 1000 * 60 * 60 * 24 * 2, '{a}')).toBe('日') // 星期日
  26. })
  27. it('empty argument', () => {
  28. expect(parseTime()).toBeNull()
  29. })
  30. it('null', () => {
  31. expect(parseTime(null)).toBeNull()
  32. })
  33. })