time_helper_spec.rb 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'TimeHelperCache', time_zone: 'Europe/London' do
  4. context 'with frontend_relative_month' do
  5. before do
  6. freeze_time
  7. travel_to DateTime.parse(datestamp)
  8. end
  9. context "when it's the 1st day of the month" do
  10. let(:datestamp) { '2023-01-01T12:00:00.000Z' }
  11. it 'matches ECMAScript results' do
  12. expect([
  13. frontend_relative_month(Time.current, 1),
  14. frontend_relative_month(Time.current, 2),
  15. frontend_relative_month(Time.current, 3),
  16. frontend_relative_month(Time.current, 4),
  17. frontend_relative_month(Time.current, 5),
  18. frontend_relative_month(Time.current, 6),
  19. frontend_relative_month(Time.current, 7),
  20. frontend_relative_month(Time.current, 8),
  21. frontend_relative_month(Time.current, 9),
  22. frontend_relative_month(Time.current, 10),
  23. frontend_relative_month(Time.current, 11),
  24. frontend_relative_month(Time.current, 12),
  25. ]).to eq([
  26. '2023-02-01T12:00:00.000Z',
  27. '2023-03-01T12:00:00.000Z',
  28. '2023-04-01T11:00:00.000Z',
  29. '2023-05-01T11:00:00.000Z',
  30. '2023-06-01T11:00:00.000Z',
  31. '2023-07-01T11:00:00.000Z',
  32. '2023-08-01T11:00:00.000Z',
  33. '2023-09-01T11:00:00.000Z',
  34. '2023-10-01T11:00:00.000Z',
  35. '2023-11-01T12:00:00.000Z',
  36. '2023-12-01T12:00:00.000Z',
  37. '2024-01-01T12:00:00.000Z',
  38. ])
  39. end
  40. end
  41. context "when it's the 28th day of the month" do
  42. let(:datestamp) { '2023-01-28T12:00:00.000Z' }
  43. it 'matches ECMAScript result' do
  44. expect([
  45. frontend_relative_month(Time.current, 1),
  46. frontend_relative_month(Time.current, 2),
  47. frontend_relative_month(Time.current, 3),
  48. frontend_relative_month(Time.current, 4),
  49. frontend_relative_month(Time.current, 5),
  50. frontend_relative_month(Time.current, 6),
  51. frontend_relative_month(Time.current, 7),
  52. frontend_relative_month(Time.current, 8),
  53. frontend_relative_month(Time.current, 9),
  54. frontend_relative_month(Time.current, 10),
  55. frontend_relative_month(Time.current, 11),
  56. frontend_relative_month(Time.current, 12),
  57. ]).to eq([
  58. '2023-02-28T12:00:00.000Z',
  59. '2023-03-28T11:00:00.000Z',
  60. '2023-04-28T11:00:00.000Z',
  61. '2023-05-28T11:00:00.000Z',
  62. '2023-06-28T11:00:00.000Z',
  63. '2023-07-28T11:00:00.000Z',
  64. '2023-08-28T11:00:00.000Z',
  65. '2023-09-28T11:00:00.000Z',
  66. '2023-10-28T11:00:00.000Z',
  67. '2023-11-28T12:00:00.000Z',
  68. '2023-12-28T12:00:00.000Z',
  69. '2024-01-28T12:00:00.000Z',
  70. ])
  71. end
  72. end
  73. context "when it's the 29th day of the month" do
  74. let(:datestamp) { '2023-01-29T12:00:00.000Z' }
  75. it 'matches ECMAScript result' do
  76. expect([
  77. frontend_relative_month(Time.current, 1),
  78. frontend_relative_month(Time.current, 2),
  79. frontend_relative_month(Time.current, 3),
  80. frontend_relative_month(Time.current, 4),
  81. frontend_relative_month(Time.current, 5),
  82. frontend_relative_month(Time.current, 6),
  83. frontend_relative_month(Time.current, 7),
  84. frontend_relative_month(Time.current, 8),
  85. frontend_relative_month(Time.current, 9),
  86. frontend_relative_month(Time.current, 10),
  87. frontend_relative_month(Time.current, 11),
  88. frontend_relative_month(Time.current, 12),
  89. ]).to eq([
  90. '2023-03-01T12:00:00.000Z',
  91. '2023-03-29T11:00:00.000Z',
  92. '2023-04-29T11:00:00.000Z',
  93. '2023-05-29T11:00:00.000Z',
  94. '2023-06-29T11:00:00.000Z',
  95. '2023-07-29T11:00:00.000Z',
  96. '2023-08-29T11:00:00.000Z',
  97. '2023-09-29T11:00:00.000Z',
  98. '2023-10-29T12:00:00.000Z',
  99. '2023-11-29T12:00:00.000Z',
  100. '2023-12-29T12:00:00.000Z',
  101. '2024-01-29T12:00:00.000Z',
  102. ])
  103. end
  104. end
  105. context "when it's the 30th day of the month" do
  106. let(:datestamp) { '2023-01-30T12:00:00.000Z' }
  107. it 'matches ECMAScript result' do
  108. expect([
  109. frontend_relative_month(Time.current, 1),
  110. frontend_relative_month(Time.current, 2),
  111. frontend_relative_month(Time.current, 3),
  112. frontend_relative_month(Time.current, 4),
  113. frontend_relative_month(Time.current, 5),
  114. frontend_relative_month(Time.current, 6),
  115. frontend_relative_month(Time.current, 7),
  116. frontend_relative_month(Time.current, 8),
  117. frontend_relative_month(Time.current, 9),
  118. frontend_relative_month(Time.current, 10),
  119. frontend_relative_month(Time.current, 11),
  120. frontend_relative_month(Time.current, 12),
  121. ]).to eq([
  122. '2023-03-02T12:00:00.000Z',
  123. '2023-03-30T11:00:00.000Z',
  124. '2023-04-30T11:00:00.000Z',
  125. '2023-05-30T11:00:00.000Z',
  126. '2023-06-30T11:00:00.000Z',
  127. '2023-07-30T11:00:00.000Z',
  128. '2023-08-30T11:00:00.000Z',
  129. '2023-09-30T11:00:00.000Z',
  130. '2023-10-30T12:00:00.000Z',
  131. '2023-11-30T12:00:00.000Z',
  132. '2023-12-30T12:00:00.000Z',
  133. '2024-01-30T12:00:00.000Z',
  134. ])
  135. end
  136. end
  137. context "when it's the 31st day of the month" do
  138. let(:datestamp) { '2023-01-31T12:00:00.000Z' }
  139. it 'matches ECMAScript result' do
  140. expect([
  141. frontend_relative_month(Time.current, 1),
  142. frontend_relative_month(Time.current, 2),
  143. frontend_relative_month(Time.current, 3),
  144. frontend_relative_month(Time.current, 4),
  145. frontend_relative_month(Time.current, 5),
  146. frontend_relative_month(Time.current, 6),
  147. frontend_relative_month(Time.current, 7),
  148. frontend_relative_month(Time.current, 8),
  149. frontend_relative_month(Time.current, 9),
  150. frontend_relative_month(Time.current, 10),
  151. frontend_relative_month(Time.current, 11),
  152. frontend_relative_month(Time.current, 12),
  153. ]).to eq([
  154. '2023-03-03T12:00:00.000Z',
  155. '2023-03-31T11:00:00.000Z',
  156. '2023-05-01T11:00:00.000Z',
  157. '2023-05-31T11:00:00.000Z',
  158. '2023-07-01T11:00:00.000Z',
  159. '2023-07-31T11:00:00.000Z',
  160. '2023-08-31T11:00:00.000Z',
  161. '2023-10-01T11:00:00.000Z',
  162. '2023-10-31T12:00:00.000Z',
  163. '2023-12-01T12:00:00.000Z',
  164. '2023-12-31T12:00:00.000Z',
  165. '2024-01-31T12:00:00.000Z',
  166. ])
  167. end
  168. end
  169. end
  170. end