ticket_macro.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. QUnit.module("ticket macro pending time check", hooks => {
  2. hooks.beforeEach( () => {
  3. this.offset = new Date(0).getTimezoneOffset()
  4. this.clock = sinon.useFakeTimers()
  5. })
  6. hooks.afterEach(() => {
  7. this.clock.restore()
  8. })
  9. var calculate_travel_on_ticket_diff = (rules, attribute = 'pending_time') => {
  10. var new_date = travel_on_ticket_date(rules, attribute)
  11. return new Date(new_date) - new Date()
  12. }
  13. var travel_on_ticket_date = (rules, attribute = 'pending_time') => {
  14. var ticket = new App.Ticket()
  15. macro = {}
  16. macro["ticket."+ attribute] = rules
  17. App.Ticket.macro({
  18. ticket: ticket,
  19. macro: macro
  20. })
  21. return ticket[attribute]
  22. }
  23. QUnit.test("5 days", assert => {
  24. var rules = {
  25. operator: "relative",
  26. range: "day",
  27. value: 5
  28. }
  29. assert.equal(calculate_travel_on_ticket_diff(rules), 60 * 60 * 24 * 5 * 1000)
  30. })
  31. QUnit.test("5 minutes", assert => {
  32. var rules = {
  33. operator: "relative",
  34. range: "minute",
  35. value: 3
  36. }
  37. assert.equal(calculate_travel_on_ticket_diff(rules), 60 * 3 * 1000)
  38. });
  39. QUnit.test("10 hours", assert => {
  40. var rules = {
  41. operator: "relative",
  42. range: "hour",
  43. value: 10
  44. }
  45. assert.equal(calculate_travel_on_ticket_diff(rules), 60 * 60 * 10 * 1000)
  46. });
  47. QUnit.test("10 months", assert => {
  48. var rules = {
  49. operator: "relative",
  50. range: "month",
  51. value: 10
  52. }
  53. var new_date = new Date(travel_on_ticket_date(rules))
  54. var target_date = new Date("1970-11-01T00:00:00.000Z")
  55. assert.equal(new_date.getTime(), target_date.getTime())
  56. });
  57. QUnit.test("10 years", assert => {
  58. var rules = {
  59. operator: "relative",
  60. range: "year",
  61. value: 1
  62. }
  63. var new_date = new Date(travel_on_ticket_date(rules))
  64. var target_date = new Date("1971-01-01T00:00:00.000Z")
  65. assert.equal(new_date.getTime(), target_date.getTime())
  66. });
  67. QUnit.test("5 minutes on a different attribute", assert => {
  68. App.Ticket.configure_attributes.push({
  69. name: 'custom_date', display: 'Custom date', tag: 'datetime'
  70. })
  71. var rules = {
  72. operator: "relative",
  73. range: "minute",
  74. value: 3
  75. }
  76. assert.equal(calculate_travel_on_ticket_diff(rules, 'custom_date'), 60 * 3 * 1000)
  77. });
  78. })