date_test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. ]);
  28. var incorrect_tests = Em.A([
  29. {t: null},
  30. {t: ''},
  31. {t: false},
  32. {t: []},
  33. {t: {}},
  34. {t: undefined},
  35. {t: function(){}}
  36. ]);
  37. describe('#dateFormatZeroFirst()', function() {
  38. var tests = [
  39. {
  40. t: 2,
  41. e: '02',
  42. m: 'should convert to `02`'
  43. },
  44. {
  45. t: 10,
  46. e: '10',
  47. m: 'should convert to `10`'
  48. }
  49. ];
  50. tests.forEach(function(test) {
  51. it(test.m, function() {
  52. expect(date.dateFormatZeroFirst(test.t)).to.eql(test.e);
  53. });
  54. });
  55. });
  56. describe('#startTime()', function() {
  57. var today = new Date();
  58. var tests = [
  59. { t: 1349752195000, e: 'Tue Oct 09 2012 06:09' },
  60. { t: -10000000, e: 'Not started' },
  61. { t: today.getTime(), e: 'Today {0}:{1}'.format(date.dateFormatZeroFirst(today.getHours()), date.dateFormatZeroFirst(today.getMinutes())) },
  62. { t: today, e: ''}
  63. ];
  64. tests.forEach(function(test) {
  65. var testMessage = 'should convert {0} to {1}'.format(test.t, test.e);
  66. it(testMessage, function() {
  67. expect(date.startTime(test.t)).to.be.eql(test.e);
  68. });
  69. });
  70. });
  71. describe('#timingFormat', function() {
  72. var tests = Em.A([
  73. {i: '30', e:'30 ms'},
  74. {i: '300', e:'300 ms'},
  75. {i: '999', e:'999 ms'},
  76. {i: '1000', e:'1.00 secs'},
  77. {i: '3000', e:'3.00 secs'},
  78. {i: '35000', e:'35.00 secs'},
  79. {i: '350000', e:'350.00 secs'},
  80. {i: '999999', e:'1000.00 secs'},
  81. {i: '1000000', e:'16.67 mins'},
  82. {i: '3500000', e:'58.33 mins'},
  83. {i: '35000000', e:'9.72 hours'},
  84. {i: '350000000', e:'4.05 days'},
  85. {i: '3500000000', e:'40.51 days'},
  86. {i: '35000000000', e:'405.09 days'}
  87. ]);
  88. describe('Correct data', function(){
  89. tests.forEach(function(test) {
  90. it(test.t, function() {
  91. expect(date.timingFormat(test.i)).to.equal(test.e);
  92. });
  93. });
  94. });
  95. describe('Incorrect data', function(){
  96. incorrect_tests.forEach(function(test) {
  97. it(test.t, function() {
  98. expect(date.timingFormat(test.t)).to.equal(null);
  99. });
  100. });
  101. });
  102. });
  103. describe('#duration', function() {
  104. var tests = Em.A([
  105. {startTime: 1, endTime: 2, e: 1},
  106. {startTime: 0, endTime: 2000, e: 0},
  107. {startTime: 200, endTime: 0, e: 19800}
  108. ]);
  109. beforeEach(function() {
  110. sinon.stub(App, 'dateTime', function () { return 20000; });
  111. });
  112. tests.forEach(function(test) {
  113. it(test.startTime + ' ' + test.endTime, function() {
  114. expect(date.duration(test.startTime, test.endTime)).to.equal(test.e);
  115. });
  116. });
  117. afterEach(function() {
  118. App.dateTime.restore();
  119. });
  120. });
  121. describe('#durationSummary()', function() {
  122. var tests = [
  123. {
  124. startTimestamp: 1349752195000,
  125. endTimestamp: 1349752199000,
  126. e: '4.00 secs'
  127. },
  128. {
  129. startTimestamp: 1349752195000,
  130. endTimestamp: 1367752195000,
  131. e: '208.33 days'
  132. },
  133. {
  134. startTimestamp: -10000000,
  135. endTimestamp: 1367752195000,
  136. e: Em.I18n.t('common.na')
  137. },
  138. {
  139. startTimestamp: 1349752195000,
  140. endTimestamp: -1,
  141. stubbed: true,
  142. e: '0 secs'
  143. },
  144. {
  145. startTimestamp: 1000,
  146. endTimestamp: -1,
  147. stubbed: true,
  148. e: '19.00 secs'
  149. }
  150. ];
  151. beforeEach(function() {
  152. sinon.stub(App, 'dateTime', function () { return 20000; });
  153. });
  154. tests.forEach(function(test) {
  155. var testMessage = 'duration between {0} and {1} is {2}'.format(test.startTimestamp, test.endTimestamp, test.e) + (test.stubbed ? " App.dateTime() is stubbed" : "");
  156. it(testMessage, function() {
  157. expect(date.durationSummary(test.startTimestamp, test.endTimestamp)).to.be.eql(test.e);
  158. });
  159. });
  160. afterEach(function() {
  161. App.dateTime.restore();
  162. });
  163. });
  164. });