date_test.js 4.9 KB

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