timezone_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. var result = timezoneUtils.getAllTimezoneNames();
  81. it('timezone names are parsed', function () {
  82. expect(this.result).to.have.length.above(0);
  83. });
  84. describe('Etc/* are excluded', function () {
  85. result.forEach(function (tz) {
  86. it(tz, function () {
  87. expect(tz.indexOf('Etc/')).to.equal(-1);
  88. });
  89. });
  90. });
  91. describe('Abbreviations are excluded', function () {
  92. result.forEach(function (tz) {
  93. it(tz, function () {
  94. expect(tz).to.not.equal(tz.toUpperCase());
  95. });
  96. });
  97. });
  98. });
  99. describe('#detectUserTimezone', function () {
  100. var getTimezoneOffset = Date.prototype.getTimezoneOffset;
  101. function mockTimezoneOffset(jan, jul) {
  102. Date.prototype.getTimezoneOffset = function () {
  103. var month = this.getMonth();
  104. if (month > 3 && month < 9) {
  105. return -jul;
  106. }
  107. return -jan;
  108. };
  109. }
  110. afterEach(function () {
  111. Date.prototype.getTimezoneOffset = getTimezoneOffset;
  112. });
  113. it('Detect UTC+1', function () {
  114. mockTimezoneOffset(0, 60);
  115. var tz = timezoneUtils.detectUserTimezone();
  116. expect(tz).to.contain('0-60|Atlantic');
  117. });
  118. it('Detect UTC+1 for Europe', function () {
  119. mockTimezoneOffset(0, 60);
  120. var tz = timezoneUtils.detectUserTimezone('Europe');
  121. expect(tz).to.contain('0-60|Europe');
  122. });
  123. it('Detect UTC-4', function () {
  124. mockTimezoneOffset(-300, -240);
  125. var tz = timezoneUtils.detectUserTimezone();
  126. expect(tz).to.contain('300240|America');
  127. });
  128. it('Detect UTC+3 for Asia', function () {
  129. mockTimezoneOffset(120, 180);
  130. var tz = timezoneUtils.detectUserTimezone();
  131. expect(tz).to.contain('-120-180|Asia');
  132. });
  133. it('Detect UTC-7', function () {
  134. mockTimezoneOffset(-480, -420);
  135. var tz = timezoneUtils.detectUserTimezone();
  136. expect(tz).to.contain('480420|America');
  137. });
  138. });
  139. });