date_test.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var Ember = require('ember');
  19. var App = require('app');
  20. var validator = require('utils/validator');
  21. var date = require('utils/date');
  22. describe('date', function () {
  23. var correct_tests = Em.A([
  24. {t: 1349752195000, e: 'Tue, Oct 09, 2012 03:09', e2: 'Tue Oct 09 2012'},
  25. {t: 1367752195000, e: 'Sun, May 05, 2013 11:09', e2: 'Sun May 05 2013'},
  26. {t: 1369952195000, e: 'Thu, May 30, 2013 22:16', e2: 'Thu May 30 2013'},
  27. {t: 1369952195000, e: 'Thu, May 30, 2013 22:16:35', e2: 'Thu May 30 2013', showSeconds: true },
  28. {t: 1369952195000, e: 'Thu, May 30, 2013 22:16:35:000', e2: 'Thu May 30 2013', showSeconds: true, showMilliseconds: true }
  29. ]);
  30. var incorrect_tests = Em.A([
  31. {t: null},
  32. {t: ''},
  33. {t: false},
  34. {t: []},
  35. {t: {}},
  36. {t: undefined},
  37. {t: function(){}}
  38. ]);
  39. describe('#dateFormatZeroFirst()', function() {
  40. var tests = [
  41. {
  42. t: 2,
  43. e: '02',
  44. m: 'should convert to `02`'
  45. },
  46. {
  47. t: 10,
  48. e: '10',
  49. m: 'should convert to `10`'
  50. }
  51. ];
  52. tests.forEach(function(test) {
  53. it(test.m, function() {
  54. expect(date.dateFormatZeroFirst(test.t)).to.eql(test.e);
  55. });
  56. });
  57. });
  58. describe('#dateFormat', function() {
  59. describe('Correct timestamps', function(){
  60. correct_tests.forEach(function(test) {
  61. var testMessage = test.t + ' `showSeconds` ' + !!test.showSeconds + '`showMilliseconds` ' + !!test.showMilliseconds;
  62. it(testMessage, function() {
  63. expect(date.dateFormat(test.t, test.showSeconds, test.showMilliseconds)).to.equal(test.e);
  64. });
  65. });
  66. });
  67. describe('Incorrect timestamps', function() {
  68. incorrect_tests.forEach(function(test) {
  69. it(test.t, function() {
  70. expect(date.dateFormat(test.t)).to.equal(test.t);
  71. });
  72. });
  73. });
  74. });
  75. describe('#dateFormatShort', function() {
  76. describe('Correct timestamps', function() {
  77. correct_tests.forEach(function(test) {
  78. it(test.t, function() {
  79. expect(date.dateFormatShort(test.t)).to.equal(test.e2);
  80. });
  81. });
  82. });
  83. it('Today timestamp', function() {
  84. var now = new Date();
  85. var then = new Date(now.getFullYear(),now.getUTCMonth(),now.getUTCDate(),0,0,0);
  86. expect(date.dateFormatShort(then.getTime() + 10*3600*1000)).to.equal('Today 10:00:00');
  87. });
  88. describe('Incorrect timestamps', function() {
  89. incorrect_tests.forEach(function(test) {
  90. it(test.t, function() {
  91. expect(date.dateFormatShort(test.t)).to.equal(test.t);
  92. });
  93. });
  94. });
  95. });
  96. describe('#startTime()', function() {
  97. var today = new Date();
  98. var tests = [
  99. { t: 1349752195000, e: 'Tue Oct 09 2012 06:09' },
  100. { t: -10000000, e: 'Not started' },
  101. { t: today.getTime(), e: 'Today {0}:{1}'.format(date.dateFormatZeroFirst(today.getHours()), date.dateFormatZeroFirst(today.getMinutes())) },
  102. { t: today, e: ''}
  103. ];
  104. tests.forEach(function(test) {
  105. var testMessage = 'should conver {0} to {1}'.format(test.t, test.e);
  106. it(testMessage, function() {
  107. expect(date.startTime(test.t)).to.be.eql(test.e);
  108. });
  109. });
  110. });
  111. describe('#timingFormat', function() {
  112. var tests = Em.A([
  113. {i: '30', e:'30 ms'},
  114. {i: '300', e:'300 ms'},
  115. {i: '999', e:'999 ms'},
  116. {i: '1000', e:'1.00 secs'},
  117. {i: '3000', e:'3.00 secs'},
  118. {i: '35000', e:'35.00 secs'},
  119. {i: '350000', e:'350.00 secs'},
  120. {i: '999999', e:'1000.00 secs'},
  121. {i: '1000000', e:'16.67 mins'},
  122. {i: '3500000', e:'58.33 mins'},
  123. {i: '35000000', e:'9.72 hours'},
  124. {i: '350000000', e:'4.05 days'},
  125. {i: '3500000000', e:'40.51 days'},
  126. {i: '35000000000', e:'405.09 days'}
  127. ]);
  128. describe('Correct data', function(){
  129. tests.forEach(function(test) {
  130. it(test.t, function() {
  131. expect(date.timingFormat(test.i)).to.equal(test.e);
  132. });
  133. });
  134. });
  135. describe('Incorrect data', function(){
  136. incorrect_tests.forEach(function(test) {
  137. it(test.t, function() {
  138. expect(date.timingFormat(test.t)).to.equal(null);
  139. });
  140. });
  141. });
  142. });
  143. describe('#duration', function() {
  144. var tests = Em.A([
  145. {startTime: 1, endTime: 2, e: 1},
  146. {startTime: 0, endTime: 2000, e: 0},
  147. {startTime: 200, endTime: 0, e: 19800}
  148. ]);
  149. beforeEach(function() {
  150. sinon.stub(App, 'dateTime', function () { return 20000; });
  151. });
  152. tests.forEach(function(test) {
  153. it(test.startTime + ' ' + test.endTime, function() {
  154. expect(date.duration(test.startTime, test.endTime)).to.equal(test.e);
  155. });
  156. });
  157. afterEach(function() {
  158. App.dateTime.restore();
  159. });
  160. });
  161. describe('#durationSummary()', function() {
  162. var tests = [
  163. {
  164. startTimestamp: 1349752195000,
  165. endTimestamp: 1349752199000,
  166. e: '4.00 secs'
  167. },
  168. {
  169. startTimestamp: 1349752195000,
  170. endTimestamp: 1367752195000,
  171. e: '208.33 days'
  172. },
  173. {
  174. startTimestamp: -10000000,
  175. endTimestamp: 1367752195000,
  176. e: Em.I18n.t('common.na')
  177. },
  178. {
  179. startTimestamp: 1349752195000,
  180. endTimestamp: -1,
  181. stubbed: true,
  182. e: '0 secs'
  183. },
  184. {
  185. startTimestamp: 1000,
  186. endTimestamp: -1,
  187. stubbed: true,
  188. e: '19.00 secs'
  189. }
  190. ];
  191. beforeEach(function() {
  192. sinon.stub(App, 'dateTime', function () { return 20000; });
  193. });
  194. tests.forEach(function(test) {
  195. var testMessage = 'duration between {0} and {1} is {2}'.format(test.startTimestamp, test.endTimestamp, test.e) + (test.stubbed ? " App.dateTime() is stubbed" : "");
  196. it(testMessage, function() {
  197. expect(date.durationSummary(test.startTimestamp, test.endTimestamp)).to.be.eql(test.e);
  198. });
  199. });
  200. afterEach(function() {
  201. App.dateTime.restore();
  202. });
  203. });
  204. });