timezone_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 timezoneUtils = require('utils/date/timezone');
  19. describe('timezoneUtils', function () {
  20. describe('#_groupTimezones', function () {
  21. var formattedTimezones = [
  22. {utcOffset: 1, groupByKey: '1', formattedOffset: '+01:00', value: 'a/Aa', region: 'a', city: 'Aa'},
  23. {utcOffset: 1, groupByKey: '1', formattedOffset: '+01:00', value: 'a/Bb', region: 'a', city: 'Bb'},
  24. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'a/Cc', region: 'a', city: 'Cc'},
  25. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'a/Dd', region: 'a', city: 'Dd'},
  26. {utcOffset: 1, groupByKey: '1', formattedOffset: '+01:00', value: 'b/Ee', region: 'b', city: 'Ee'},
  27. {utcOffset: 1, groupByKey: '1', formattedOffset: '+01:00', value: 'b/Ff', region: 'b', city: 'Ff'},
  28. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'b/Gg', region: 'b', city: 'Gg'},
  29. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'b/Hh', region: 'b', city: 'Hh'},
  30. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'b/II', region: 'b', city: 'II'},
  31. {utcOffset: 2, groupByKey: '2', formattedOffset: '+02:00', value: 'b', region: 'b', city: ''}
  32. ];
  33. before(function () {
  34. this.result = timezoneUtils._groupTimezones(formattedTimezones);
  35. });
  36. it('should group to 4 groups', function () {
  37. expect(this.result.length).to.equal(4);
  38. });
  39. it('UTCOffset should be [1,1,2,2]', function () {
  40. expect(this.result.mapProperty('utcOffset')).to.eql([1, 1, 2, 2]);
  41. });
  42. it('should map regions and cities correctly', function () {
  43. var expected = [
  44. '(UTC+01:00 UTC) a / Aa, Bb',
  45. '(UTC+01:00 UTC) b / Ee, Ff',
  46. '(UTC+02:00 UTC) a / Cc, Dd',
  47. '(UTC+02:00 UTC) b / Gg, Hh'
  48. ];
  49. var values = this.result.mapProperty('label');
  50. expect(values).to.eql(expected);
  51. expect(values.join('')).to.not.contain('II');
  52. expect(values.join('')).to.not.contain(', ,');
  53. });
  54. });
  55. describe('#_parseTimezones', function () {
  56. beforeEach(function () {
  57. sinon.stub(timezoneUtils, 'getAllTimezoneNames').returns([
  58. 'Europe/Helsinki',
  59. 'Asia/Magadan',
  60. 'America/Lima'
  61. ]);
  62. sinon.stub(timezoneUtils, '_groupTimezones', function (list) {
  63. return list;
  64. });
  65. this.result = timezoneUtils._parseTimezones();
  66. });
  67. afterEach(function () {
  68. timezoneUtils.getAllTimezoneNames.restore();
  69. timezoneUtils._groupTimezones.restore();
  70. });
  71. it('should sort by offset and name', function () {
  72. expect(this.result.mapProperty('value')).to.eql(['America/Lima', 'Europe/Helsinki', 'Asia/Magadan']);
  73. });
  74. it('should split regions and cities', function () {
  75. expect(this.result.mapProperty('region')).to.eql(['America', 'Europe', 'Asia']);
  76. expect(this.result.mapProperty('city')).to.eql(['Lima', 'Helsinki', 'Magadan']);
  77. });
  78. });
  79. describe('#getAllTimezoneNames', function () {
  80. before(function () {
  81. this.result = timezoneUtils.getAllTimezoneNames();
  82. });
  83. after(function () {
  84. this.result = undefined;
  85. });
  86. it('timezone names are parsed', function () {
  87. expect(this.result).to.have.length.above(0);
  88. });
  89. it('Etc/* are excluded', function () {
  90. this.result.forEach(function (tz) {
  91. expect(tz.indexOf('Etc/')).to.equal(-1);
  92. });
  93. });
  94. it('Abbreviations are excluded', function () {
  95. this.result.forEach(function (tz) {
  96. expect(tz).to.not.equal(tz.toUpperCase());
  97. });
  98. });
  99. });
  100. describe('#detectUserTimezone', function () {
  101. var getTimezoneOffset = Date.prototype.getTimezoneOffset;
  102. function mockTimezoneOffset(jan, jul) {
  103. Date.prototype.getTimezoneOffset = function () {
  104. var month = this.getMonth();
  105. if (month > 3 && month < 9) {
  106. return -jul;
  107. }
  108. return -jan;
  109. };
  110. }
  111. afterEach(function () {
  112. Date.prototype.getTimezoneOffset = getTimezoneOffset;
  113. });
  114. it('Detect UTC+1', function () {
  115. mockTimezoneOffset(0, 60);
  116. var tz = timezoneUtils.detectUserTimezone();
  117. expect(tz).to.contain('0-60|Atlantic');
  118. });
  119. it('Detect UTC+1 for Europe', function () {
  120. mockTimezoneOffset(0, 60);
  121. var tz = timezoneUtils.detectUserTimezone('Europe');
  122. expect(tz).to.contain('0-60|Europe');
  123. });
  124. it('Detect UTC-4', function () {
  125. mockTimezoneOffset(-300, -240);
  126. var tz = timezoneUtils.detectUserTimezone();
  127. expect(tz).to.contain('300240|America');
  128. });
  129. it('Detect UTC+3 for Asia', function () {
  130. mockTimezoneOffset(120, 180);
  131. var tz = timezoneUtils.detectUserTimezone();
  132. expect(tz).to.contain('-120-180|Asia');
  133. });
  134. it('Detect UTC-7', function () {
  135. mockTimezoneOffset(-480, -420);
  136. var tz = timezoneUtils.detectUserTimezone();
  137. expect(tz).to.contain('480420|America');
  138. });
  139. });
  140. });