pretty_date.js 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. let freeze_at = new Date(0)
  2. freeze_at.setFullYear(1999)
  3. freeze_at.setMonth(3)
  4. freeze_at.setDate(22)
  5. freeze_at.setHours(21)
  6. QUnit.test('App.PrettyDate#humanTime as timestamp', assert => {
  7. clock = sinon.useFakeTimers({now: freeze_at})
  8. let type = 'timestamp'
  9. let offset = 60 * 60 * 24 * 7 * 1000
  10. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '04/15/1999 9:00 pm', 'before')
  11. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), '04/29/1999 9:00 pm', 'after')
  12. offset = 50 * 1000
  13. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'just now', 'close before')
  14. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'just now', 'close after')
  15. clock.restore()
  16. });
  17. QUnit.test('App.PrettyDate#humanTime as absolute', assert => {
  18. clock = sinon.useFakeTimers({now: freeze_at})
  19. let type = 'absolute'
  20. let offset = 60 * 60 * 24 * 3 * 1000
  21. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'Monday 21:00', 'less than a week before')
  22. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'Sunday 21:00', 'less than a week after')
  23. offset = 60 * 30 * 1000
  24. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'Thursday 20:30', 'less than an hour before')
  25. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'Thursday 21:30', 'less than an hour after')
  26. offset = 60 * 60 * 24 * 90 * 1000
  27. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'Friday 22. Jan 20:00', 'in same year and before')
  28. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'Wednesday 21. Jul 21:00', 'in same year and after')
  29. offset = 60 * 60 * 24 * 300 * 1000
  30. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'Friday 06/26/1998 9:00 pm', 'long before')
  31. offset = 60 * 60 * 24 * 900 * 1000
  32. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'Monday 10/08/2001 9:00 pm', 'long after')
  33. offset = 50 * 1000
  34. assert.equal(App.PrettyDate.humanTime(new Date(), false, true, type), 'just now', 'close before')
  35. assert.equal(App.PrettyDate.humanTime(new Date(), false, true, type), 'just now', 'close after')
  36. clock.restore()
  37. });
  38. QUnit.test('App.PrettyDate#humanTime as relative', assert => {
  39. clock = sinon.useFakeTimers({now: freeze_at})
  40. let type = 'relative'
  41. let offset = 60 * 5 * 1000
  42. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '5 minutes ago', 'minutes before')
  43. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 5 minutes', 'minutes after')
  44. offset = 60 * 60 * 4 * 1000 + 60 * 5 * 1000
  45. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '4 hours 5 minutes ago', 'hours before')
  46. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 4 hours 5 minutes', 'hours after')
  47. offset = 60 * 60 * 4 * 1000
  48. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '4 hours ago', 'exactly hours before')
  49. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 4 hours', 'exactly hours after')
  50. offset = 60 * 60 * 24 * 3 * 1000 + 60 * 60 * 4 * 1000 + 60 * 5 * 1000
  51. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '3 days 4 hours ago', 'days before')
  52. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 3 days 4 hours', 'days after')
  53. offset = 60 * 60 * 24 * 3 * 1000
  54. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '3 days ago', 'days before')
  55. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 3 days', 'days after')
  56. offset = 60 * 60 * 24 * 14 * 1000 + 60 * 60 * 24 * 3 * 1000 + 60 * 60 * 4 * 1000 + 60 * 5 * 1000
  57. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), '04/05/1999', 'over a week before')
  58. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'in 17 days', 'over a week after')
  59. offset = 50 * 1000
  60. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() - offset), false, true, type), 'just now', 'close before')
  61. assert.equal(App.PrettyDate.humanTime(new Date(freeze_at.getTime() + offset), false, true, type), 'just now', 'close after')
  62. clock.restore()
  63. });