dashboard.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* global wp, sinon, JSON */
  2. var communityEventsData, dateI18n, pagenow;
  3. jQuery( document ).ready( function () {
  4. var getFormattedDate = wp.communityEvents.getFormattedDate,
  5. getTimeZone = wp.communityEvents.getTimeZone,
  6. getTimeZoneAbbreviation = wp.communityEvents.getTimeZoneAbbreviation,
  7. populateDynamicEventFields = wp.communityEvents.populateDynamicEventFields,
  8. startDate = 1600185600 * 1000, // Tue Sep 15 9:00:00 AM PDT 2020
  9. HOUR_IN_MS = 60 * 60 * 1000,
  10. DAY_IN_MS = HOUR_IN_MS * 24,
  11. WEEK_IN_MS = DAY_IN_MS * 7;
  12. QUnit.module( 'dashboard', function( hooks ) {
  13. hooks.beforeEach( function() {
  14. this.oldDateI18n = dateI18n;
  15. this.oldPagenow = pagenow;
  16. dateI18n = wp.date.dateI18n;
  17. pagenow = 'dashboard';
  18. communityEventsData = {
  19. time_format: 'g:i a',
  20. l10n: {
  21. date_formats: {
  22. single_day_event: 'l, M j, Y',
  23. multiple_day_event: '%1$s %2$dā€“%3$d, %4$d',
  24. multiple_month_event: '%1$s %2$d ā€“ %3$s %4$d, %5$d'
  25. }
  26. }
  27. };
  28. } );
  29. hooks.afterEach( function() {
  30. dateI18n = this.oldDateI18n;
  31. pagenow = this.oldPagenow;
  32. } );
  33. QUnit.module( 'communityEvents.populateDynamicEventFields', function() {
  34. QUnit.test( 'dynamic fields should be added', function( assert ) {
  35. var timeFormat = communityEventsData.time_format;
  36. var getFormattedDateStub = sinon.stub( wp.communityEvents, 'getFormattedDate' ),
  37. getTimeZoneStub = sinon.stub( wp.communityEvents, 'getTimeZone' ),
  38. getTimeZoneAbbreviationStub = sinon.stub( wp.communityEvents, 'getTimeZoneAbbreviation' );
  39. getFormattedDateStub.returns( 'Tuesday, Sep 15, 2020' );
  40. getTimeZoneStub.returns( 'America/Chicago' );
  41. getTimeZoneAbbreviationStub.returns( 'CDT' );
  42. var rawEvents = [
  43. {
  44. start_unix_timestamp: 1600185600,
  45. end_unix_timestamp: 1600189200
  46. },
  47. {
  48. start_unix_timestamp: 1602232400,
  49. end_unix_timestamp: 1602236000
  50. }
  51. ];
  52. var expected = JSON.parse( JSON.stringify( rawEvents ) );
  53. expected[0].user_formatted_date = 'Tuesday, Sep 15, 2020';
  54. expected[0].user_formatted_time = '11:00 am';
  55. expected[0].timeZoneAbbreviation = 'CDT';
  56. expected[1].user_formatted_date = 'Tuesday, Sep 15, 2020'; // This is expected to be the same as item 0, because of the stub.
  57. expected[1].user_formatted_time = '3:33 am';
  58. expected[1].timeZoneAbbreviation = 'CDT';
  59. var actual = populateDynamicEventFields( rawEvents, timeFormat );
  60. assert.strictEqual(
  61. JSON.stringify( actual ),
  62. JSON.stringify( expected )
  63. );
  64. getFormattedDateStub.restore();
  65. getTimeZoneStub.restore();
  66. getTimeZoneAbbreviationStub.restore();
  67. } );
  68. } );
  69. QUnit.module( 'communityEvents.getFormattedDate', function() {
  70. QUnit.test( 'single month event should use corresponding format', function( assert ) {
  71. var actual = getFormattedDate(
  72. startDate,
  73. startDate + HOUR_IN_MS,
  74. 'America/Vancouver',
  75. communityEventsData.l10n.date_formats
  76. );
  77. assert.strictEqual( actual, 'Tuesday, Sep 15, 2020' );
  78. } );
  79. QUnit.test( 'multiple day event should use corresponding format', function( assert ) {
  80. var actual = getFormattedDate(
  81. startDate,
  82. startDate + ( 2 * DAY_IN_MS ),
  83. 'America/Vancouver',
  84. communityEventsData.l10n.date_formats
  85. );
  86. assert.strictEqual( actual, 'September 15ā€“17, 2020' );
  87. } );
  88. QUnit.test( 'multiple month event should use corresponding format', function( assert ) {
  89. var actual = getFormattedDate(
  90. startDate,
  91. startDate + ( 3 * WEEK_IN_MS ),
  92. 'America/Vancouver',
  93. communityEventsData.l10n.date_formats
  94. );
  95. assert.strictEqual( actual, 'September 15 ā€“ October 6, 2020' );
  96. } );
  97. QUnit.test( 'undefined end date should be treated as a single-day event', function( assert ) {
  98. var actual = getFormattedDate(
  99. startDate,
  100. undefined,
  101. 'America/Vancouver',
  102. communityEventsData.l10n.date_formats
  103. );
  104. assert.strictEqual( actual, 'Tuesday, Sep 15, 2020' );
  105. } );
  106. QUnit.test( 'empty end date should be treated as a single-day event', function( assert ) {
  107. var actual = getFormattedDate(
  108. startDate,
  109. '',
  110. 'America/Vancouver',
  111. communityEventsData.l10n.date_formats
  112. );
  113. assert.strictEqual( actual, 'Tuesday, Sep 15, 2020' );
  114. } );
  115. } );
  116. QUnit.module( 'communityEvents.getTimeZone', function() {
  117. QUnit.test( 'modern browsers should return a time zone name', function( assert ) {
  118. // Simulate a modern browser.
  119. var stub = sinon.stub( Intl.DateTimeFormat.prototype, 'resolvedOptions' );
  120. stub.returns( { timeZone: 'America/Chicago' } );
  121. var actual = getTimeZone( startDate );
  122. stub.restore();
  123. assert.strictEqual( actual, 'America/Chicago' );
  124. } );
  125. QUnit.test( 'older browsers should fallback to a raw UTC offset', function( assert ) {
  126. // Simulate IE11.
  127. var resolvedOptionsStub = sinon.stub( Intl.DateTimeFormat.prototype, 'resolvedOptions' );
  128. var getTimezoneOffsetStub = sinon.stub( Date.prototype, 'getTimezoneOffset' );
  129. resolvedOptionsStub.returns( { timeZone: undefined } );
  130. getTimezoneOffsetStub.returns( 300 );
  131. var actual = getTimeZone( startDate );
  132. assert.strictEqual( actual, -300, 'negative offset' ); // Intentionally opposite, see `getTimeZone()`.
  133. getTimezoneOffsetStub.returns( 0 );
  134. actual = getTimeZone( startDate );
  135. assert.strictEqual( actual, 0, 'no offset' );
  136. getTimezoneOffsetStub.returns( -300 );
  137. actual = getTimeZone( startDate );
  138. assert.strictEqual( actual, 300, 'positive offset' ); // Intentionally opposite, see `getTimeZone()`.
  139. resolvedOptionsStub.restore();
  140. getTimezoneOffsetStub.restore();
  141. } );
  142. } );
  143. QUnit.module( 'communityEvents.getTimeZoneAbbreviation', function() {
  144. QUnit.test( 'modern browsers should return a time zone abbreviation', function( assert ) {
  145. // Modern browsers append a short time zone code to the time string.
  146. var stub = sinon.stub( Date.prototype, 'toLocaleTimeString' );
  147. stub.returns( '4:00:00 PM CDT' );
  148. var actual = getTimeZoneAbbreviation( startDate );
  149. stub.restore();
  150. assert.strictEqual( actual, 'CDT' );
  151. } );
  152. QUnit.test( 'older browsers should fallback to a formatted UTC offset', function( assert ) {
  153. var toLocaleTimeStringStub = sinon.stub( Date.prototype, 'toLocaleTimeString' );
  154. var getTimezoneOffsetStub = sinon.stub( Date.prototype, 'getTimezoneOffset' );
  155. // IE 11 doesn't add the abbreviation like modern browsers do.
  156. toLocaleTimeStringStub.returns( '4:00:00 PM' );
  157. getTimezoneOffsetStub.returns( 300 );
  158. var actual = getTimeZoneAbbreviation( startDate );
  159. assert.strictEqual( actual, 'GMT-5', 'negative offset' ); // Intentionally opposite, see `getTimeZone()`.
  160. getTimezoneOffsetStub.returns( 0 );
  161. actual = getTimeZoneAbbreviation( startDate );
  162. assert.strictEqual( actual, 'GMT+0', 'no offset' );
  163. getTimezoneOffsetStub.returns( -300 );
  164. actual = getTimeZoneAbbreviation( startDate );
  165. assert.strictEqual( actual, 'GMT+5', 'positive offset' ); // Intentionally opposite, see `getTimeZone()`.
  166. toLocaleTimeStringStub.restore();
  167. getTimezoneOffsetStub.restore();
  168. } );
  169. } );
  170. } );
  171. } );