tests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* License: MIT.
  2. * Copyright (C) 2013, 2014, 2015, Uri Shaked.
  3. */
  4. /* global describe, inject, module, beforeEach, afterEach, it, expect, spyOn, jasmine */
  5. 'use strict';
  6. describe('module angularMoment', function () {
  7. var $rootScope, $compile, $window, $filter, moment, amTimeAgoConfig, originalTimeAgoConfig, angularMomentConfig,
  8. originalAngularMomentConfig, amMoment;
  9. beforeEach(module('angularMoment'));
  10. beforeEach(inject(function ($injector) {
  11. $rootScope = $injector.get('$rootScope');
  12. $compile = $injector.get('$compile');
  13. $window = $injector.get('$window');
  14. $filter = $injector.get('$filter');
  15. moment = $injector.get('moment');
  16. amMoment = $injector.get('amMoment');
  17. amTimeAgoConfig = $injector.get('amTimeAgoConfig');
  18. angularMomentConfig = $injector.get('angularMomentConfig');
  19. originalTimeAgoConfig = angular.copy(amTimeAgoConfig);
  20. originalAngularMomentConfig = angular.copy(angularMomentConfig);
  21. // Ensure the locale of moment.js is set to en by default
  22. (moment.locale || moment.lang)('en');
  23. // Add a sample timezone for tests
  24. moment.tz.add('Pacific/Tahiti|LMT TAHT|9W.g a0|01|-2joe1.I');
  25. }));
  26. afterEach(function () {
  27. // Restore original configuration after each test
  28. angular.copy(originalTimeAgoConfig, amTimeAgoConfig);
  29. angular.copy(originalAngularMomentConfig, angularMomentConfig);
  30. jasmine.clock().uninstall();
  31. });
  32. describe('am-time-ago directive', function () {
  33. it('should change the text of the element to "a few seconds ago" when given unix timestamp', function () {
  34. $rootScope.testDate = new Date().getTime() / 1000;
  35. var element = angular.element('<span am-time-ago="testDate" am-preprocess="unix"></span>');
  36. element = $compile(element)($rootScope);
  37. $rootScope.$digest();
  38. expect(element.text()).toBe('a few seconds ago');
  39. });
  40. it('should change the text of the element to "a few seconds ago" when given current time', function () {
  41. $rootScope.testDate = new Date();
  42. var element = angular.element('<span am-time-ago="testDate"></span>');
  43. element = $compile(element)($rootScope);
  44. $rootScope.$digest();
  45. expect(element.text()).toBe('a few seconds ago');
  46. });
  47. it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago', function () {
  48. $rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000);
  49. var element = angular.element('<div am-time-ago="testDate"></div>');
  50. element = $compile(element)($rootScope);
  51. $rootScope.$digest();
  52. expect(element.text()).toBe('3 minutes ago');
  53. });
  54. it('should change the text of the div to "2 hours ago" when given a date 2 hours ago', function () {
  55. $rootScope.testDate = new Date(new Date().getTime() - 2 * 60 * 60 * 1000);
  56. var element = angular.element('<div am-time-ago="testDate"></div>');
  57. element = $compile(element)($rootScope);
  58. $rootScope.$digest();
  59. expect(element.text()).toBe('2 hours ago');
  60. });
  61. it('should change the text of the div to "one year ago" when given a date one year ago', function () {
  62. var today = new Date();
  63. $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate());
  64. var element = angular.element('<div am-time-ago="testDate"></div>');
  65. element = $compile(element)($rootScope);
  66. $rootScope.$digest();
  67. expect(element.text()).toBe('a year ago');
  68. });
  69. it('should parse correctly numeric dates as milliseconds since the epoch', function () {
  70. $rootScope.testDate = new Date().getTime();
  71. var element = angular.element('<div am-time-ago="testDate"></div>');
  72. element = $compile(element)($rootScope);
  73. $rootScope.$digest();
  74. expect(element.text()).toBe('a few seconds ago');
  75. });
  76. it('should update the value if date changes on scope', function () {
  77. var today = new Date();
  78. $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime();
  79. var element = angular.element('<div am-time-ago="testDate"></div>');
  80. element = $compile(element)($rootScope);
  81. $rootScope.$digest();
  82. expect(element.text()).toBe('a year ago');
  83. $rootScope.testDate = new Date();
  84. $rootScope.$digest();
  85. expect(element.text()).toBe('a few seconds ago');
  86. });
  87. it('should update the span text as time passes', function (done) {
  88. $rootScope.testDate = new Date(new Date().getTime() - 44000);
  89. var element = angular.element('<div am-time-ago="testDate"></div>');
  90. element = $compile(element)($rootScope);
  91. $rootScope.$digest();
  92. expect(element.text()).toBe('a few seconds ago');
  93. var waitsInterval = setInterval(function () {
  94. // Wait until $rootScope.date is more than 45 seconds old
  95. if (new Date().getTime() - $rootScope.testDate.getTime() < 45000) {
  96. return;
  97. }
  98. clearInterval(waitsInterval);
  99. $rootScope.$digest();
  100. expect(element.text()).toBe('a minute ago');
  101. done();
  102. }, 50);
  103. });
  104. it('should schedule the update timer to one hour ahead for date in the far future (#73)', function () {
  105. $rootScope.testDate = new Date(new Date().getTime() + 86400000);
  106. jasmine.clock().install();
  107. spyOn($window, 'setTimeout');
  108. var element = angular.element('<div am-time-ago="testDate"></div>');
  109. element = $compile(element)($rootScope);
  110. $rootScope.$digest();
  111. expect($window.setTimeout).toHaveBeenCalledWith(jasmine.any(Function), 3600000);
  112. });
  113. describe('bindonce', function () {
  114. it('should change the text of the div to "3 minutes ago" when given a date 3 minutes ago with one time binding', function () {
  115. $rootScope.testDate = new Date(new Date().getTime() - 3 * 60 * 1000);
  116. var element = angular.element('<div am-time-ago="::testDate"></div>');
  117. element = $compile(element)($rootScope);
  118. $rootScope.$digest();
  119. expect(element.text()).toBe('3 minutes ago');
  120. });
  121. it('should parse correctly numeric dates as milliseconds since the epoch with one time binding', function () {
  122. $rootScope.testDate = new Date().getTime();
  123. var element = angular.element('<div am-time-ago="::testDate"></div>');
  124. element = $compile(element)($rootScope);
  125. $rootScope.$digest();
  126. expect(element.text()).toBe('a few seconds ago');
  127. });
  128. it('should not update the value if date changes on scope when using one time binding', function () {
  129. var today = new Date();
  130. $rootScope.testDate = new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()).getTime();
  131. var element = angular.element('<div am-time-ago="::testDate"></div>');
  132. element = $compile(element)($rootScope);
  133. $rootScope.$digest();
  134. expect(element.text()).toBe('a year ago');
  135. $rootScope.testDate = new Date();
  136. $rootScope.$digest();
  137. expect(element.text()).toBe('a year ago');
  138. });
  139. it('should not update the span text as time passes when using one time binding', function (done) {
  140. $rootScope.testDate = new Date(new Date().getTime() - 44000);
  141. var element = angular.element('<div am-time-ago="::testDate"></div>');
  142. element = $compile(element)($rootScope);
  143. $rootScope.$digest();
  144. expect(element.text()).toBe('a few seconds ago');
  145. var waitsInterval = setInterval(function () {
  146. // Wait until $rootScope.date is more than 45 seconds old
  147. if (new Date().getTime() - $rootScope.testDate.getTime() < 45000) {
  148. return;
  149. }
  150. clearInterval(waitsInterval);
  151. $rootScope.$digest();
  152. expect(element.text()).toBe('a few seconds ago');
  153. done();
  154. }, 50);
  155. });
  156. });
  157. it('should handle undefined data', function () {
  158. $rootScope.testDate = null;
  159. var element = angular.element('<div am-time-ago="testDate"></div>');
  160. element = $compile(element)($rootScope);
  161. var digest = function () {
  162. $rootScope.$digest();
  163. };
  164. expect(digest).not.toThrow();
  165. });
  166. it('should remove the element text and cancel the timer when an empty string is given (#15)', function () {
  167. $rootScope.testDate = new Date().getTime();
  168. var element = angular.element('<div am-time-ago="testDate"></div>');
  169. element = $compile(element)($rootScope);
  170. $rootScope.$digest();
  171. expect(element.text()).toBe('a few seconds ago');
  172. $rootScope.testDate = '';
  173. spyOn($window, 'clearTimeout').and.callThrough();
  174. $rootScope.$digest();
  175. expect($window.clearTimeout).toHaveBeenCalled();
  176. expect(element.text()).toBe('');
  177. });
  178. it('should not change the contents of the element until a date is given', function () {
  179. $rootScope.testDate = null;
  180. var element = angular.element('<div am-time-ago="testDate">Initial text</div>');
  181. element = $compile(element)($rootScope);
  182. $rootScope.$digest();
  183. expect(element.text()).toBe('Initial text');
  184. $rootScope.testDate = new Date().getTime();
  185. $rootScope.$digest();
  186. expect(element.text()).toBe('a few seconds ago');
  187. });
  188. it('should cancel the timer when the scope is destroyed', function () {
  189. var scope = $rootScope.$new();
  190. $rootScope.testDate = new Date();
  191. var element = angular.element('<span am-time-ago="testDate"></span>');
  192. element = $compile(element)(scope);
  193. $rootScope.$digest();
  194. spyOn($window, 'clearTimeout').and.callThrough();
  195. scope.$destroy();
  196. expect($window.clearTimeout).toHaveBeenCalled();
  197. });
  198. it('should generate a time string without suffix when configured to do so', function () {
  199. amTimeAgoConfig.withoutSuffix = true;
  200. $rootScope.testDate = new Date();
  201. var element = angular.element('<span am-time-ago="testDate"></span>');
  202. element = $compile(element)($rootScope);
  203. $rootScope.$digest();
  204. expect(element.text()).toBe('a few seconds');
  205. });
  206. it('should generate update the text following a locale change via amMoment.changeLocale() method', function () {
  207. $rootScope.testDate = new Date();
  208. var element = angular.element('<span am-time-ago="testDate"></span>');
  209. element = $compile(element)($rootScope);
  210. $rootScope.$digest();
  211. expect(element.text()).toBe('a few seconds ago');
  212. amMoment.changeLocale('fr');
  213. expect(element.text()).toBe('il y a quelques secondes');
  214. });
  215. it('should update the `datetime` attr if applied to a TIME element', function () {
  216. $rootScope.testDate = Date.UTC(2012, 8, 20, 15, 20, 12);
  217. var element = angular.element('<time am-time-ago="testDate"></span>');
  218. element = $compile(element)($rootScope);
  219. $rootScope.$digest();
  220. expect(element.attr('datetime')).toBe('2012-09-20T15:20:12.000Z');
  221. });
  222. describe('setting the element title', function() {
  223. it('should not set the title attribute of the element to the date by default', function () {
  224. $rootScope.testDate = new Date().getTime() / 1000;
  225. var element = angular.element('<span am-time-ago="testDate"></span>');
  226. element = $compile(element)($rootScope);
  227. $rootScope.$digest();
  228. expect(element.attr('title')).toBeUndefined();
  229. });
  230. it('should not change the title attribute of the element if the element already has a title', function () {
  231. amTimeAgoConfig.titleFormat = 'MMMM Do YYYY, h:mm:ss a';
  232. $rootScope.testDate = new Date().getTime() / 1000;
  233. var element = angular.element('<span am-time-ago="testDate" title="test"></span>');
  234. element = $compile(element)($rootScope);
  235. $rootScope.$digest();
  236. expect(element.attr('title')).toBe('test');
  237. });
  238. it('should set the title attribute of the element to the formatted date as per the config', function () {
  239. amTimeAgoConfig.titleFormat = 'MMMM Do YYYY, h:mm:ss a';
  240. $rootScope.testDate = new Date().getTime() / 1000;
  241. var element = angular.element('<span am-time-ago="testDate"></span>');
  242. element = $compile(element)($rootScope);
  243. $rootScope.$digest();
  244. var testDateWithCustomFormatting = moment($rootScope.testDate).format(amTimeAgoConfig.titleFormat);
  245. expect(element.attr('title')).toBe(testDateWithCustomFormatting);
  246. });
  247. });
  248. describe('am-without-suffix attribute', function () {
  249. it('should generate a time string without suffix when true', function () {
  250. $rootScope.testDate = new Date();
  251. var element = angular.element('<span am-time-ago="testDate" am-without-suffix="true"></span>');
  252. element = $compile(element)($rootScope);
  253. $rootScope.$digest();
  254. expect(element.text()).toBe('a few seconds');
  255. });
  256. it('should generate a time string with suffix when false', function () {
  257. amTimeAgoConfig.withoutSuffix = true;
  258. $rootScope.testDate = new Date();
  259. var element = angular.element('<span am-time-ago="testDate" am-without-suffix="false"></span>');
  260. element = $compile(element)($rootScope);
  261. $rootScope.$digest();
  262. expect(element.text()).toBe('a few seconds ago');
  263. });
  264. it('should support expressions', function () {
  265. $rootScope.testDate = new Date();
  266. $rootScope.withSuffix = false;
  267. var element = angular.element('<span am-time-ago="testDate" am-without-suffix="!withSuffix"></span>');
  268. element = $compile(element)($rootScope);
  269. $rootScope.$digest();
  270. expect(element.text()).toBe('a few seconds');
  271. $rootScope.withSuffix = true;
  272. $rootScope.$digest();
  273. expect(element.text()).toBe('a few seconds ago');
  274. });
  275. it('should ignore non-boolean values', function () {
  276. $rootScope.testDate = new Date();
  277. $rootScope.withoutSuffix = 'string';
  278. var element = angular.element('<span am-time-ago="testDate" am-without-suffix="withoutSuffix"></span>');
  279. element = $compile(element)($rootScope);
  280. $rootScope.$digest();
  281. expect(element.text()).toBe('a few seconds ago');
  282. });
  283. });
  284. describe('am-format attribute', function () {
  285. it('should support custom date format', function () {
  286. var today = new Date();
  287. $rootScope.testDate = today.getFullYear() + '#' + today.getDate() + '#' + today.getMonth();
  288. var element = angular.element('<span am-time-ago="testDate" am-format="YYYY#DD#MM"></span>');
  289. element = $compile(element)($rootScope);
  290. $rootScope.$digest();
  291. expect(element.text()).toBe('a month ago');
  292. });
  293. it('should support angular expressions in date format', function () {
  294. var today = new Date();
  295. $rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
  296. var element = angular.element('<span am-time-ago="testDate" am-format="{{dateFormat}}"></span>');
  297. element = $compile(element)($rootScope);
  298. $rootScope.$digest();
  299. $rootScope.dateFormat = 'MM@YYYY@DD';
  300. $rootScope.$digest();
  301. expect(element.text()).toBe('a month ago');
  302. });
  303. });
  304. describe('format config property', function () {
  305. it('should be used when no `am-format` attribute is found', function () {
  306. angularMomentConfig.format = 'MM@YYYY@DD';
  307. var today = new Date();
  308. $rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
  309. var element = angular.element('<span am-time-ago="testDate"></span>');
  310. element = $compile(element)($rootScope);
  311. $rootScope.$digest();
  312. expect(element.text()).toBe('a month ago');
  313. });
  314. it('should be overridable by `am-format` attribute', function () {
  315. angularMomentConfig.format = 'YYYY@MM@@DD';
  316. var today = new Date();
  317. $rootScope.testDate = today.getMonth() + '@' + today.getFullYear() + '@' + today.getDate();
  318. var element = angular.element('<span am-format="MM@YYYY@DD" am-time-ago="testDate"></span>');
  319. element = $compile(element)($rootScope);
  320. $rootScope.$digest();
  321. expect(element.text()).toBe('a month ago');
  322. });
  323. });
  324. describe('serverTime configuration', function () {
  325. it('should calculate time ago in respect to the configured server time', function () {
  326. amTimeAgoConfig.serverTime = Date.UTC(2014, 5, 12, 5, 22, 11);
  327. $rootScope.testDate = Date.UTC(2014, 5, 12, 9, 22, 11);
  328. var element = angular.element('<span am-time-ago="testDate"></span>');
  329. element = $compile(element)($rootScope);
  330. $rootScope.$digest();
  331. expect(element.text()).toBe('in 4 hours');
  332. });
  333. });
  334. });
  335. describe('amCalendar filter', function () {
  336. var amCalendar;
  337. beforeEach(function () {
  338. amCalendar = $filter('amCalendar');
  339. });
  340. it('should convert today date to calendar form', function () {
  341. var today = new Date();
  342. var testDate = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 13, 33, 33);
  343. expect(amCalendar(testDate)).toBe('Today at 1:33 PM');
  344. });
  345. it('should convert date in long past to calendar form', function () {
  346. expect(amCalendar(new Date(2012, 2, 25, 13, 14, 15))).toBe('03/25/2012');
  347. });
  348. it('should gracefully handle undefined values', function () {
  349. expect(amCalendar()).toBe('');
  350. });
  351. it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
  352. expect(amCalendar(new Date(2012, 0, 22, 4, 46, 54).getTime())).toBe('01/22/2012');
  353. });
  354. it('should respect the configured timezone', function () {
  355. angularMomentConfig.timezone = 'Pacific/Tahiti';
  356. expect(amCalendar(Date.UTC(2012, 0, 22, 4, 46, 54))).toBe('01/21/2012');
  357. });
  358. it('should apply the "utc" preprocessor when the string "utc" is given in the second argument', function () {
  359. expect(amCalendar(Date.UTC(2012, 0, 22, 0, 0, 0), 'utc')).toBe('01/22/2012');
  360. expect(amCalendar(Date.UTC(2012, 0, 22, 23, 59, 59), 'utc')).toBe('01/22/2012');
  361. });
  362. it('should apply the "unix" preprocessor if angularMomentConfig.preprocess is set to "unix" and no preprocessor is given', function () {
  363. angularMomentConfig.preprocess = 'unix';
  364. expect(amCalendar(100000)).toBe('01/02/1970');
  365. });
  366. it('should ignore the default preprocessor if we explicity give it null in the second argument', function () {
  367. angularMomentConfig.preprocess = 'unix';
  368. expect(amCalendar(100000, null)).toBe('01/01/1970');
  369. });
  370. it('should gracefully handle the case where timezone is given but moment-timezone is not loaded', function () {
  371. angularMomentConfig.timezone = 'Pacific/Tahiti';
  372. var originalMomentTz = moment.fn.tz;
  373. try {
  374. delete moment.fn.tz;
  375. expect(amCalendar(Date.UTC(2012, 0, 22, 4, 46, 54))).toBe('01/22/2012');
  376. } finally {
  377. moment.fn.tz = originalMomentTz;
  378. }
  379. });
  380. it('should return an empty string for invalid input', function () {
  381. expect(amCalendar('blah blah')).toBe('');
  382. });
  383. });
  384. describe('amDateFormat filter', function () {
  385. var amDateFormat;
  386. beforeEach(function () {
  387. amDateFormat = $filter('amDateFormat');
  388. });
  389. it('should support displaying format', function () {
  390. var today = new Date();
  391. var expectedResult = today.getDate() + '.' + (today.getMonth() + 1) + '.' + today.getFullYear();
  392. expect(amDateFormat(today, 'D.M.YYYY')).toBe(expectedResult);
  393. });
  394. it('should gracefully handle undefined values', function () {
  395. expect(amDateFormat(undefined, 'D.M.YYYY')).toBe('');
  396. });
  397. it('should accept a numeric unix timestamp (milliseconds since the epoch) as input', function () {
  398. var timestamp = new Date(2012, 0, 22, 12, 46, 54).getTime();
  399. expect(amDateFormat(timestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(12,46,54);01.22.2012');
  400. });
  401. it('should gracefully handle string unix timestamp as input', function () {
  402. var strTimestamp = String(new Date(2012, 0, 22, 12, 46, 54).getTime());
  403. expect(amDateFormat(strTimestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(12,46,54);01.22.2012');
  404. });
  405. it('should respect the configured timezone', function () {
  406. angularMomentConfig.timezone = 'Pacific/Tahiti';
  407. var timestamp = Date.UTC(2012, 0, 22, 12, 46, 54);
  408. expect(amDateFormat(timestamp, '(HH,mm,ss);MM.DD.YYYY')).toBe('(02,46,54);01.22.2012');
  409. });
  410. it('should return an empty string for invalid input', function () {
  411. expect(amDateFormat('blah blah', '(HH,mm,ss);MM.DD.YYYY')).toBe('');
  412. });
  413. });
  414. describe('amDurationFormat filter', function () {
  415. var amDurationFormat;
  416. beforeEach(function () {
  417. amDurationFormat = $filter('amDurationFormat');
  418. });
  419. it('should support return the given duration as text', function () {
  420. expect(amDurationFormat(1000, 'milliseconds')).toBe('a few seconds');
  421. });
  422. it('should support return a day given 24 hours', function () {
  423. expect(amDurationFormat(24, 'hours')).toBe('a day');
  424. });
  425. it('should add prefix the result with the word "in" if the third parameter (suffix) is true', function () {
  426. expect(amDurationFormat(1, 'minutes', true)).toBe('in a minute');
  427. });
  428. it('should add suffix the result with the word "ago" if the duration is negative and the third parameter is true', function () {
  429. expect(amDurationFormat(-1, 'minutes', true)).toBe('a minute ago');
  430. });
  431. it('should gracefully handle undefined values for duration', function () {
  432. expect(amDurationFormat(undefined, 'minutes')).toBe('');
  433. });
  434. });
  435. describe('amTimeAgo filter', function () {
  436. var amTimeAgo;
  437. beforeEach(function () {
  438. amTimeAgo = $filter('amTimeAgo');
  439. });
  440. it('should support return the time ago as text', function () {
  441. var date = new Date();
  442. expect(amTimeAgo(date)).toBe('a few seconds ago');
  443. });
  444. it('should remove suffix from the result if the third parameter (suffix) is true', function () {
  445. var date = new Date();
  446. expect(amTimeAgo(date, null, true)).toBe('a few seconds');
  447. });
  448. it('should gracefully handle undefined values', function () {
  449. expect(amTimeAgo()).toBe('');
  450. });
  451. it('should gracefully handle invalid input', function () {
  452. expect(amTimeAgo('noDate')).toBe('');
  453. });
  454. });
  455. describe('amMoment service', function () {
  456. describe('#changeLocale', function () {
  457. it('should return the current locale', function () {
  458. expect(amMoment.changeLocale()).toBe('en');
  459. });
  460. it('should broadcast an angularMoment:localeChanged event on the root scope if a locale is specified', function () {
  461. var eventBroadcasted = false;
  462. $rootScope.$on('amMoment:localeChanged', function () {
  463. eventBroadcasted = true;
  464. });
  465. amMoment.changeLocale('fr');
  466. expect(eventBroadcasted).toBe(true);
  467. });
  468. it('should not broadcast an angularMoment:localeChanged event on the root scope if no locale is specified', function () {
  469. var eventBroadcasted = false;
  470. $rootScope.$on('amMoment:localeChanged', function () {
  471. eventBroadcasted = true;
  472. });
  473. amMoment.changeLocale();
  474. expect(eventBroadcasted).toBe(false);
  475. });
  476. });
  477. describe('#preprocessDate', function () {
  478. it('should call a custom preprocessor that was registered on amMoment.preprocessors', function () {
  479. var testDate = new Date(2013, 0, 22, 12, 46, 54);
  480. var meeting = {
  481. name: 'Budget plan',
  482. date: testDate
  483. };
  484. amMoment.preprocessors.foobar = function (value) {
  485. return moment(value.date);
  486. };
  487. expect(amMoment.preprocessDate(meeting, 'foobar').valueOf()).toEqual(testDate.getTime());
  488. });
  489. it('should issue a warning if an unsupported preprocessor is used and fall-back to default processing', inject(function ($log) {
  490. var testDate = new Date(2014, 0, 22, 12, 46, 54);
  491. spyOn($log, 'warn');
  492. expect(amMoment.preprocessDate(testDate.getTime(), 'blabla').valueOf()).toEqual(testDate.getTime());
  493. expect($log.warn).toHaveBeenCalledWith('angular-moment: Ignoring unsupported value for preprocess: blabla');
  494. }));
  495. });
  496. });
  497. describe('amTimeAgoConfig constant', function () {
  498. it('should generate time with suffix by default', function () {
  499. expect(amTimeAgoConfig.withoutSuffix).toBe(false);
  500. });
  501. });
  502. describe('angularMomentConfig constant', function () {
  503. it('should have an empty timezone value by default', function () {
  504. expect(angularMomentConfig.timezone).toBe('');
  505. });
  506. it('should have an empty preprocess value by default', function () {
  507. expect(angularMomentConfig.preprocess).toBe(null);
  508. });
  509. });
  510. });