publicApiSpec.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. describe('Plugin initialization and component basic construction', function () {
  2. 'use strict';
  3. it('loads jquery plugin properly', function () {
  4. expect($('<div>').datetimepicker).toBeDefined();
  5. expect(typeof $('<div>').datetimepicker).toEqual('function');
  6. expect($('<div>').datetimepicker.defaults).toBeDefined();
  7. });
  8. it('creates the component with default options on an input element', function () {
  9. var dtpElement = $('<input>'),
  10. dtp;
  11. $(document).find('body').append(dtpElement);
  12. expect(function () {
  13. expect(dtpElement.datetimepicker()).toBe(dtpElement);
  14. }).not.toThrow();
  15. dtp = dtpElement.data('DateTimePicker');
  16. expect(dtpElement).not.toBe(null);
  17. });
  18. it('creates the component with default options merged with those provided on an input element', function () {
  19. var options = {locale: 'fr'},
  20. dtpElement = $('<input>'),
  21. dtp;
  22. $(document).find('body').append(dtpElement);
  23. expect(function () {
  24. expect(dtpElement.datetimepicker(options)).toBe(dtpElement);
  25. }).not.toThrow();
  26. dtp = dtpElement.data('DateTimePicker');
  27. expect(dtp).not.toBe(null);
  28. expect(dtp.options()).toEqual($.extend(true, {}, dtpElement.datetimepicker.defaults, options));
  29. });
  30. it('does not accept non-object or string types', function () {
  31. var dtpElement = $('<input>');
  32. $(document).find('body').append(dtpElement);
  33. expect(function () {
  34. dtpElement.datetimepicker(true);
  35. }).toThrow();
  36. });
  37. xit('calls destroy when Element that the component is attached is removed', function () {
  38. var dtpElement = $('<div>').attr('class', 'row').append($('<div>').attr('class', 'col-md-12').append($('<input>'))),
  39. dtp;
  40. $(document).find('body').append(dtpElement);
  41. dtpElement.datetimepicker();
  42. dtp = dtpElement.data('DateTimePicker');
  43. spyOn(dtp, 'destroy').and.callThrough();
  44. dtpElement.remove();
  45. expect(dtp.destroy).toHaveBeenCalled();
  46. });
  47. });
  48. describe('Public API method tests', function () {
  49. 'use strict';
  50. var dtp,
  51. dtpElement,
  52. dpChangeSpy,
  53. dpShowSpy,
  54. dpHideSpy,
  55. dpErrorSpy,
  56. dpClassifySpy;
  57. beforeEach(function () {
  58. dpChangeSpy = jasmine.createSpy('dp.change event Spy');
  59. dpShowSpy = jasmine.createSpy('dp.show event Spy');
  60. dpHideSpy = jasmine.createSpy('dp.hide event Spy');
  61. dpErrorSpy = jasmine.createSpy('dp.error event Spy');
  62. dpClassifySpy = jasmine.createSpy('dp.classify event Spy');
  63. dtpElement = $('<input>').attr('id', 'dtp');
  64. $(document).find('body').append($('<div>').attr('class', 'row').append($('<div>').attr('class', 'col-md-12').append(dtpElement)));
  65. $(document).find('body').on('dp.change', dpChangeSpy);
  66. $(document).find('body').on('dp.show', dpShowSpy);
  67. $(document).find('body').on('dp.hide', dpHideSpy);
  68. $(document).find('body').on('dp.error', dpErrorSpy);
  69. $(document).find('body').on('dp.classify', dpClassifySpy);
  70. dtpElement.datetimepicker();
  71. dtp = dtpElement.data('DateTimePicker');
  72. });
  73. afterEach(function () {
  74. dtp.destroy();
  75. dtpElement.remove();
  76. });
  77. describe('configuration option name match to public api function', function () {
  78. Object.getOwnPropertyNames($.fn.datetimepicker.defaults).forEach(function (key) {
  79. it('has function ' + key + '()', function () {
  80. expect(dtp[key]).toBeDefined();
  81. });
  82. });
  83. });
  84. describe('unknown functions', function () {
  85. it('are not allowed', function () {
  86. expect(function () {
  87. dtpElement.datetimepicker('abcdef');
  88. }).toThrow();
  89. });
  90. });
  91. describe('date() function', function () {
  92. describe('typechecking', function () {
  93. it('accepts a null', function () {
  94. expect(function () {
  95. dtp.date(null);
  96. }).not.toThrow();
  97. });
  98. it('accepts a string', function () {
  99. expect(function () {
  100. dtp.date('2013/05/24');
  101. }).not.toThrow();
  102. });
  103. it('accepts a Date object', function () {
  104. expect(function () {
  105. dtp.date(new Date());
  106. }).not.toThrow();
  107. });
  108. it('accepts a Moment object', function () {
  109. expect(function () {
  110. dtp.date(moment());
  111. }).not.toThrow();
  112. });
  113. it('does not accept undefined', function () {
  114. expect(function () {
  115. dtp.date(undefined);
  116. }).toThrow();
  117. });
  118. it('does not accept a number', function () {
  119. expect(function () {
  120. dtp.date(0);
  121. }).toThrow();
  122. });
  123. it('does not accept a generic Object', function () {
  124. expect(function () {
  125. dtp.date({});
  126. }).toThrow();
  127. });
  128. it('does not accept a boolean', function () {
  129. expect(function () {
  130. dtp.date(false);
  131. }).toThrow();
  132. });
  133. });
  134. describe('functionality', function () {
  135. it('has no date set upon construction', function () {
  136. expect(dtp.date()).toBe(null);
  137. });
  138. it('sets the date correctly', function () {
  139. var timestamp = moment();
  140. dtp.date(timestamp);
  141. expect(dtp.date().isSame(timestamp)).toBe(true);
  142. });
  143. });
  144. describe('access', function () {
  145. it('gets date', function () {
  146. expect(dtpElement.datetimepicker('date')).toBe(null);
  147. });
  148. it('sets date', function () {
  149. var timestamp = moment();
  150. expect(dtpElement.datetimepicker('date', timestamp)).toBe(dtpElement);
  151. expect(dtpElement.datetimepicker('date').isSame(timestamp)).toBe(true);
  152. });
  153. });
  154. });
  155. describe('format() function', function () {
  156. describe('typechecking', function () {
  157. it('accepts a false value', function () {
  158. expect(function () {
  159. dtp.format(false);
  160. }).not.toThrow();
  161. });
  162. it('accepts a string', function () {
  163. expect(function () {
  164. dtp.format('YYYY-MM-DD');
  165. }).not.toThrow();
  166. });
  167. it('does not accept undefined', function () {
  168. expect(function () {
  169. dtp.format(undefined);
  170. }).toThrow();
  171. });
  172. it('does not accept true', function () {
  173. expect(function () {
  174. dtp.format(true);
  175. }).toThrow();
  176. });
  177. it('does not accept a generic Object', function () {
  178. expect(function () {
  179. dtp.format({});
  180. }).toThrow();
  181. });
  182. });
  183. describe('functionality', function () {
  184. it('returns no format before format is set', function () {
  185. expect(dtp.format()).toBe(false);
  186. });
  187. it('sets the format correctly', function () {
  188. var format = 'YYYY-MM-DD';
  189. dtp.format(format);
  190. expect(dtp.format()).toBe(format);
  191. });
  192. });
  193. describe('access', function () {
  194. it('gets format', function () {
  195. expect(dtpElement.datetimepicker('format')).toBe(false);
  196. });
  197. it('sets format', function () {
  198. var format = 'YYYY-MM-DD';
  199. expect(dtpElement.datetimepicker('format', format)).toBe(dtpElement);
  200. expect(dtpElement.datetimepicker('format')).toBe(format);
  201. });
  202. });
  203. });
  204. describe('destroy() function', function () {
  205. describe('existence', function () {
  206. it('is defined', function () {
  207. expect(dtp.destroy).toBeDefined();
  208. });
  209. });
  210. describe('access', function () {
  211. it('returns jQuery object', function () {
  212. expect(dtpElement.datetimepicker('destroy')).toBe(dtpElement);
  213. });
  214. });
  215. });
  216. describe('toggle() function', function () {
  217. describe('existence', function () {
  218. it('is defined', function () {
  219. expect(dtp.toggle).toBeDefined();
  220. });
  221. });
  222. // describe('functionality', function () {
  223. // it('')
  224. // });
  225. describe('access', function () {
  226. it('returns jQuery object', function () {
  227. expect(dtpElement.datetimepicker('toggle')).toBe(dtpElement);
  228. });
  229. });
  230. });
  231. describe('show() function', function () {
  232. describe('existence', function () {
  233. it('is defined', function () {
  234. expect(dtp.show).toBeDefined();
  235. });
  236. });
  237. describe('functionality', function () {
  238. it('emits a show event when called while widget is hidden', function () {
  239. dtp.show();
  240. expect(dpShowSpy).toHaveBeenCalled();
  241. });
  242. it('does not emit a show event when called and widget is already showing', function () {
  243. dtp.hide();
  244. dtp.show();
  245. dpShowSpy.calls.reset();
  246. dtp.show();
  247. expect(dpShowSpy).not.toHaveBeenCalled();
  248. });
  249. it('calls the classify event for each day that is shown', function () {
  250. dtp.show();
  251. expect(dpClassifySpy.calls.count()).toEqual(42);
  252. });
  253. it('actually shows the widget', function () {
  254. dtp.show();
  255. expect($(document).find('body').find('.bootstrap-datetimepicker-widget').length).toEqual(1);
  256. });
  257. it('applies the styles appended in the classify event handler', function () {
  258. var handler = function (event) {
  259. if (event.date.get('weekday') === 4) {
  260. event.classNames.push('humpday');
  261. }
  262. event.classNames.push('injected');
  263. };
  264. $(document).find('body').on('dp.classify', handler);
  265. dtp.show();
  266. $(document).find('body').off('dp.classify', handler);
  267. expect($(document).find('body').find('.bootstrap-datetimepicker-widget td.day.injected').length).toEqual(42);
  268. expect($(document).find('body').find('.bootstrap-datetimepicker-widget td.day.humpday').length).toEqual(6);
  269. });
  270. });
  271. describe('access', function () {
  272. it('returns jQuery object', function () {
  273. expect(dtpElement.datetimepicker('show')).toBe(dtpElement);
  274. });
  275. });
  276. });
  277. describe('hide() function', function () {
  278. describe('existence', function () {
  279. it('is defined', function () {
  280. expect(dtp.hide).toBeDefined();
  281. });
  282. });
  283. describe('functionality', function () {
  284. it('emits a hide event when called while widget is shown', function () {
  285. dtp.show();
  286. dtp.hide();
  287. expect(dpHideSpy).toHaveBeenCalled();
  288. });
  289. it('does not emit a hide event when called while widget is hidden', function () {
  290. dtp.hide();
  291. expect(dpHideSpy).not.toHaveBeenCalled();
  292. });
  293. it('actually hides the widget', function () {
  294. dtp.show();
  295. dtp.hide();
  296. expect($(document).find('body').find('.bootstrap-datetimepicker-widget').length).toEqual(0);
  297. });
  298. });
  299. describe('access', function () {
  300. it('returns jQuery object', function () {
  301. expect(dtpElement.datetimepicker('hide')).toBe(dtpElement);
  302. });
  303. });
  304. });
  305. describe('disable() function', function () {
  306. describe('existence', function () {
  307. it('is defined', function () {
  308. expect(dtp.disable).toBeDefined();
  309. });
  310. });
  311. describe('access', function () {
  312. it('returns jQuery object', function () {
  313. expect(dtpElement.datetimepicker('disable')).toBe(dtpElement);
  314. });
  315. });
  316. });
  317. describe('enable() function', function () {
  318. describe('existence', function () {
  319. it('is defined', function () {
  320. expect(dtp.enable).toBeDefined();
  321. });
  322. });
  323. describe('access', function () {
  324. it('returns jQuery object', function () {
  325. expect(dtpElement.datetimepicker('enable')).toBe(dtpElement);
  326. });
  327. });
  328. });
  329. describe('options() function', function () {
  330. describe('existence', function () {
  331. it('is defined', function () {
  332. expect(dtp.options).toBeDefined();
  333. });
  334. });
  335. describe('access', function () {
  336. it('gets options', function () {
  337. expect(dtpElement.datetimepicker('options')).toEqual(dtpElement.datetimepicker.defaults);
  338. });
  339. it('sets options', function () {
  340. var options = {locale: 'fr'};
  341. expect(dtpElement.datetimepicker('options', options)).toBe(dtpElement);
  342. expect(dtpElement.datetimepicker('options')).toEqual($.extend(true, {}, dtpElement.datetimepicker.defaults, options));
  343. });
  344. });
  345. });
  346. describe('disabledDates() function', function () {
  347. describe('existence', function () {
  348. it('is defined', function () {
  349. expect(dtp.disabledDates).toBeDefined();
  350. });
  351. });
  352. describe('access', function () {
  353. it('gets disabled dates', function () {
  354. expect(dtpElement.datetimepicker('disabledDates')).toBe(false);
  355. });
  356. it('sets disabled dates', function () {
  357. var timestamps = [moment()];
  358. expect(dtpElement.datetimepicker('disabledDates', timestamps)).toBe(dtpElement);
  359. expect(dtpElement.datetimepicker('disabledDates')).not.toBe(false);
  360. });
  361. });
  362. });
  363. describe('enabledDates() function', function () {
  364. describe('existence', function () {
  365. it('is defined', function () {
  366. expect(dtp.enabledDates).toBeDefined();
  367. });
  368. });
  369. describe('access', function () {
  370. it('gets enabled dates', function () {
  371. expect(dtpElement.datetimepicker('enabledDates')).toBe(false);
  372. });
  373. it('sets enabled dates', function () {
  374. var timestamps = [moment()];
  375. expect(dtpElement.datetimepicker('enabledDates', timestamps)).toBe(dtpElement);
  376. expect(dtpElement.datetimepicker('enabledDates')).not.toBe(false);
  377. });
  378. });
  379. });
  380. describe('daysOfWeekDisabled() function', function () {
  381. describe('existence', function () {
  382. it('is defined', function () {
  383. expect(dtp.daysOfWeekDisabled).toBeDefined();
  384. });
  385. });
  386. describe('access', function () {
  387. xit('gets days of week disabled', function () {
  388. expect(dtpElement.datetimepicker('daysOfWeekDisabled')).toEqual([]);
  389. });
  390. it('sets days of week disabled', function () {
  391. var daysOfWeek = [0];
  392. expect(dtpElement.datetimepicker('daysOfWeekDisabled', daysOfWeek)).toBe(dtpElement);
  393. expect(dtpElement.datetimepicker('daysOfWeekDisabled')).toEqual(daysOfWeek);
  394. });
  395. });
  396. });
  397. describe('maxDate() function', function () {
  398. describe('existence', function () {
  399. it('is defined', function () {
  400. expect(dtp.maxDate).toBeDefined();
  401. });
  402. });
  403. describe('access', function () {
  404. it('gets max date', function () {
  405. expect(dtpElement.datetimepicker('maxDate')).toBe(false);
  406. });
  407. it('sets max date', function () {
  408. var timestamp = moment();
  409. expect(dtpElement.datetimepicker('maxDate', timestamp)).toBe(dtpElement);
  410. expect(dtpElement.datetimepicker('maxDate').isSame(timestamp)).toBe(true);
  411. });
  412. });
  413. });
  414. describe('minDate() function', function () {
  415. describe('existence', function () {
  416. it('is defined', function () {
  417. expect(dtp.minDate).toBeDefined();
  418. });
  419. });
  420. describe('access', function () {
  421. it('gets min date', function () {
  422. expect(dtpElement.datetimepicker('minDate')).toBe(false);
  423. });
  424. it('sets min date', function () {
  425. var timestamp = moment();
  426. expect(dtpElement.datetimepicker('minDate', timestamp)).toBe(dtpElement);
  427. expect(dtpElement.datetimepicker('minDate').isSame(timestamp)).toBe(true);
  428. });
  429. });
  430. });
  431. describe('defaultDate() function', function () {
  432. describe('existence', function () {
  433. it('is defined', function () {
  434. expect(dtp.defaultDate).toBeDefined();
  435. });
  436. });
  437. describe('functionality', function () {
  438. it('returns no defaultDate before defaultDate is set', function () {
  439. expect(dtp.defaultDate()).toBe(false);
  440. });
  441. it('sets the defaultDate correctly', function () {
  442. var timestamp = moment();
  443. dtp.defaultDate(timestamp);
  444. expect(dtp.defaultDate().isSame(timestamp)).toBe(true);
  445. expect(dtp.date().isSame(timestamp)).toBe(true);
  446. });
  447. it('triggers a change event upon setting a default date and input field is empty', function () {
  448. dtp.date(null);
  449. dtp.defaultDate(moment());
  450. expect(dpChangeSpy).toHaveBeenCalled();
  451. });
  452. it('does not override input value if it already has one', function () {
  453. var timestamp = moment();
  454. dtp.date(timestamp);
  455. dtp.defaultDate(moment().year(2000));
  456. expect(dtp.date().isSame(timestamp)).toBe(true);
  457. });
  458. });
  459. describe('access', function () {
  460. it('gets default date', function () {
  461. expect(dtpElement.datetimepicker('defaultDate')).toBe(false);
  462. });
  463. it('sets default date', function () {
  464. var timestamp = moment();
  465. expect(dtpElement.datetimepicker('defaultDate', timestamp)).toBe(dtpElement);
  466. expect(dtpElement.datetimepicker('defaultDate').isSame(timestamp)).toBe(true);
  467. });
  468. });
  469. });
  470. describe('locale() function', function () {
  471. describe('functionality', function () {
  472. it('it has the same locale as the global moment locale with default options', function () {
  473. expect(dtp.locale()).toBe(moment.locale());
  474. });
  475. it('it switches to a selected locale without affecting global moment locale', function () {
  476. dtp.locale('el');
  477. dtp.date(moment());
  478. expect(dtp.locale()).toBe('el');
  479. expect(dtp.date().locale()).toBe('el');
  480. expect(moment.locale()).toBe('en');
  481. });
  482. });
  483. describe('access', function () {
  484. it('gets locale', function () {
  485. expect(dtpElement.datetimepicker('locale')).toBe(moment.locale());
  486. });
  487. it('sets locale', function () {
  488. var locale = 'fr';
  489. expect(dtpElement.datetimepicker('locale', locale)).toBe(dtpElement);
  490. expect(dtpElement.datetimepicker('locale')).toBe(locale);
  491. });
  492. });
  493. });
  494. describe('useCurrent() function', function () {
  495. describe('existence', function () {
  496. it('is defined', function () {
  497. expect(dtp.useCurrent).toBeDefined();
  498. });
  499. });
  500. describe('check type and parameter validity', function () {
  501. it('accepts either a boolean value or string', function () {
  502. var useCurrentOptions = ['year', 'month', 'day', 'hour', 'minute'];
  503. expect(function () {
  504. dtp.useCurrent(false);
  505. }).not.toThrow();
  506. expect(function () {
  507. dtp.useCurrent(true);
  508. }).not.toThrow();
  509. useCurrentOptions.forEach(function (value) {
  510. expect(function () {
  511. dtp.useCurrent(value);
  512. }).not.toThrow();
  513. });
  514. expect(function () {
  515. dtp.useCurrent('test');
  516. }).toThrow();
  517. expect(function () {
  518. dtp.useCurrent({});
  519. }).toThrow();
  520. });
  521. });
  522. describe('functionality', function () {
  523. it('triggers a change event upon show() and input field is empty', function () {
  524. dtp.useCurrent(true);
  525. dtp.show();
  526. expect(dpChangeSpy).toHaveBeenCalled();
  527. });
  528. });
  529. describe('access', function () {
  530. it('gets use current', function () {
  531. expect(dtpElement.datetimepicker('useCurrent')).toBe(true);
  532. });
  533. it('sets use current', function () {
  534. var useCurrent = false;
  535. expect(dtpElement.datetimepicker('useCurrent', useCurrent)).toBe(dtpElement);
  536. expect(dtpElement.datetimepicker('useCurrent')).toBe(useCurrent);
  537. });
  538. });
  539. });
  540. describe('ignoreReadonly() function', function () {
  541. describe('existence', function () {
  542. it('is defined', function () {
  543. expect(dtp.ignoreReadonly).toBeDefined();
  544. });
  545. });
  546. describe('access', function () {
  547. it('gets ignore readonly', function () {
  548. expect(dtpElement.datetimepicker('ignoreReadonly')).toBe(false);
  549. });
  550. it('sets ignore readonly', function () {
  551. var ignoreReadonly = true;
  552. expect(dtpElement.datetimepicker('ignoreReadonly', ignoreReadonly)).toBe(dtpElement);
  553. expect(dtpElement.datetimepicker('ignoreReadonly')).toBe(ignoreReadonly);
  554. });
  555. });
  556. });
  557. describe('stepping() function', function () {
  558. describe('existence', function () {
  559. it('is defined', function () {
  560. expect(dtp.stepping).toBeDefined();
  561. });
  562. });
  563. describe('access', function () {
  564. it('gets stepping', function () {
  565. expect(dtpElement.datetimepicker('stepping')).toBe(1);
  566. });
  567. it('sets stepping', function () {
  568. var stepping = 2;
  569. expect(dtpElement.datetimepicker('stepping', stepping)).toBe(dtpElement);
  570. expect(dtpElement.datetimepicker('stepping')).toBe(stepping);
  571. });
  572. });
  573. });
  574. describe('collapse() function', function () {
  575. describe('existence', function () {
  576. it('is defined', function () {
  577. expect(dtp.collapse).toBeDefined();
  578. });
  579. });
  580. describe('access', function () {
  581. it('gets collapse', function () {
  582. expect(dtpElement.datetimepicker('collapse')).toBe(true);
  583. });
  584. it('sets collapse', function () {
  585. var collapse = false;
  586. expect(dtpElement.datetimepicker('collapse', collapse)).toBe(dtpElement);
  587. expect(dtpElement.datetimepicker('collapse')).toBe(collapse);
  588. });
  589. });
  590. });
  591. describe('icons() function', function () {
  592. describe('existence', function () {
  593. it('is defined', function () {
  594. expect(dtp.icons).toBeDefined();
  595. });
  596. });
  597. describe('access', function () {
  598. it('gets icons', function () {
  599. expect(dtpElement.datetimepicker('icons')).toEqual(dtpElement.datetimepicker.defaults.icons);
  600. });
  601. it('sets icons', function () {
  602. var icons = {time: 'fa fa-time'};
  603. expect(dtpElement.datetimepicker('icons', icons)).toBe(dtpElement);
  604. expect(dtpElement.datetimepicker('icons')).toEqual($.extend(true, {}, dtpElement.datetimepicker.defaults.icons, icons));
  605. });
  606. });
  607. });
  608. describe('useStrict() function', function () {
  609. describe('existence', function () {
  610. it('is defined', function () {
  611. expect(dtp.useStrict).toBeDefined();
  612. });
  613. });
  614. describe('access', function () {
  615. it('gets use strict', function () {
  616. expect(dtpElement.datetimepicker('useStrict')).toBe(false);
  617. });
  618. it('sets use strict', function () {
  619. var useStrict = true;
  620. expect(dtpElement.datetimepicker('useStrict', useStrict)).toBe(dtpElement);
  621. expect(dtpElement.datetimepicker('useStrict')).toBe(useStrict);
  622. });
  623. });
  624. });
  625. describe('sideBySide() function', function () {
  626. describe('existence', function () {
  627. it('is defined', function () {
  628. expect(dtp.sideBySide).toBeDefined();
  629. });
  630. });
  631. describe('access', function () {
  632. it('gets side-by-side', function () {
  633. expect(dtpElement.datetimepicker('sideBySide')).toBe(false);
  634. });
  635. it('sets side-by-side', function () {
  636. var sideBySide = true;
  637. expect(dtpElement.datetimepicker('sideBySide', sideBySide)).toBe(dtpElement);
  638. expect(dtpElement.datetimepicker('sideBySide')).toBe(sideBySide);
  639. });
  640. });
  641. });
  642. describe('viewMode() function', function () {
  643. describe('existence', function () {
  644. it('is defined', function () {
  645. expect(dtp.viewMode).toBeDefined();
  646. });
  647. });
  648. describe('access', function () {
  649. it('gets view mode', function () {
  650. expect(dtpElement.datetimepicker('viewMode')).toBe('days');
  651. });
  652. it('sets view mode', function () {
  653. var viewMode = 'years';
  654. expect(dtpElement.datetimepicker('viewMode', viewMode)).toBe(dtpElement);
  655. expect(dtpElement.datetimepicker('viewMode')).toBe(viewMode);
  656. });
  657. });
  658. });
  659. describe('widgetPositioning() function', function () {
  660. describe('existence', function () {
  661. it('is defined', function () {
  662. expect(dtp.widgetPositioning).toBeDefined();
  663. });
  664. });
  665. describe('access', function () {
  666. it('gets widget positioning', function () {
  667. expect(dtpElement.datetimepicker('widgetPositioning')).toEqual(dtpElement.datetimepicker.defaults.widgetPositioning);
  668. });
  669. it('sets widget positioning', function () {
  670. var widgetPositioning = {horizontal: 'left'};
  671. expect(dtpElement.datetimepicker('widgetPositioning', widgetPositioning)).toBe(dtpElement);
  672. expect(dtpElement.datetimepicker('widgetPositioning')).toEqual($.extend(true, {}, dtpElement.datetimepicker.defaults.widgetPositioning, widgetPositioning));
  673. });
  674. });
  675. });
  676. describe('calendarWeeks() function', function () {
  677. describe('existence', function () {
  678. it('is defined', function () {
  679. expect(dtp.calendarWeeks).toBeDefined();
  680. });
  681. });
  682. describe('access', function () {
  683. it('gets calendar weeks', function () {
  684. expect(dtpElement.datetimepicker('calendarWeeks')).toBe(false);
  685. });
  686. it('sets calendar weeks', function () {
  687. var calendarWeeks = true;
  688. expect(dtpElement.datetimepicker('calendarWeeks', calendarWeeks)).toBe(dtpElement);
  689. expect(dtpElement.datetimepicker('calendarWeeks')).toBe(calendarWeeks);
  690. });
  691. });
  692. });
  693. describe('showTodayButton() function', function () {
  694. describe('existence', function () {
  695. it('is defined', function () {
  696. expect(dtp.showTodayButton).toBeDefined();
  697. });
  698. });
  699. describe('access', function () {
  700. it('gets show today button', function () {
  701. expect(dtpElement.datetimepicker('showTodayButton')).toBe(false);
  702. });
  703. it('sets show today button', function () {
  704. var showTodayButton = true;
  705. expect(dtpElement.datetimepicker('showTodayButton', showTodayButton)).toBe(dtpElement);
  706. expect(dtpElement.datetimepicker('showTodayButton')).toBe(showTodayButton);
  707. });
  708. });
  709. });
  710. describe('showClear() function', function () {
  711. describe('existence', function () {
  712. it('is defined', function () {
  713. expect(dtp.showClear).toBeDefined();
  714. });
  715. });
  716. describe('access', function () {
  717. it('gets show clear', function () {
  718. expect(dtpElement.datetimepicker('showClear')).toBe(false);
  719. });
  720. it('sets show clear', function () {
  721. var showClear = true;
  722. expect(dtpElement.datetimepicker('showClear', showClear)).toBe(dtpElement);
  723. expect(dtpElement.datetimepicker('showClear')).toBe(showClear);
  724. });
  725. });
  726. });
  727. describe('dayViewHeaderFormat() function', function () {
  728. describe('typechecking', function () {
  729. it('does not accept a false value', function () {
  730. expect(function () {
  731. dtp.dayViewHeaderFormat(false);
  732. }).toThrow();
  733. });
  734. it('accepts a string', function () {
  735. expect(function () {
  736. dtp.dayViewHeaderFormat('YYYY-MM-DD');
  737. }).not.toThrow();
  738. });
  739. it('does not accept undefined', function () {
  740. expect(function () {
  741. dtp.dayViewHeaderFormat(undefined);
  742. }).toThrow();
  743. });
  744. it('does not accept true', function () {
  745. expect(function () {
  746. dtp.dayViewHeaderFormat(true);
  747. }).toThrow();
  748. });
  749. it('does not accept a generic Object', function () {
  750. expect(function () {
  751. dtp.dayViewHeaderFormat({});
  752. }).toThrow();
  753. });
  754. });
  755. describe('functionality', function () {
  756. it('expects dayViewHeaderFormat to be default of MMMM YYYY', function () {
  757. expect(dtp.dayViewHeaderFormat()).toBe('MMMM YYYY');
  758. });
  759. it('sets the dayViewHeaderFormat correctly', function () {
  760. dtp.dayViewHeaderFormat('MM YY');
  761. expect(dtp.dayViewHeaderFormat()).toBe('MM YY');
  762. });
  763. });
  764. describe('access', function () {
  765. it('gets day view header format', function () {
  766. expect(dtpElement.datetimepicker('dayViewHeaderFormat')).toBe('MMMM YYYY');
  767. });
  768. it('sets day view header format', function () {
  769. var dayViewHeaderFormat = 'MM YY';
  770. expect(dtpElement.datetimepicker('dayViewHeaderFormat', dayViewHeaderFormat)).toBe(dtpElement);
  771. expect(dtpElement.datetimepicker('dayViewHeaderFormat')).toBe(dayViewHeaderFormat);
  772. });
  773. });
  774. });
  775. describe('extraFormats() function', function () {
  776. describe('typechecking', function () {
  777. it('accepts a false value', function () {
  778. expect(function () {
  779. dtp.extraFormats(false);
  780. }).not.toThrow();
  781. });
  782. it('does not accept a string', function () {
  783. expect(function () {
  784. dtp.extraFormats('YYYY-MM-DD');
  785. }).toThrow();
  786. });
  787. it('does not accept undefined', function () {
  788. expect(function () {
  789. dtp.extraFormats(undefined);
  790. }).toThrow();
  791. });
  792. it('does not accept true', function () {
  793. expect(function () {
  794. dtp.extraFormats(true);
  795. }).toThrow();
  796. });
  797. it('accepts an Array', function () {
  798. expect(function () {
  799. dtp.extraFormats(['YYYY-MM-DD']);
  800. }).not.toThrow();
  801. });
  802. });
  803. describe('functionality', function () {
  804. it('returns no extraFormats before extraFormats is set', function () {
  805. expect(dtp.extraFormats()).toBe(false);
  806. });
  807. it('sets the extraFormats correctly', function () {
  808. dtp.extraFormats(['YYYY-MM-DD']);
  809. expect(dtp.extraFormats()[0]).toBe('YYYY-MM-DD');
  810. });
  811. });
  812. describe('access', function () {
  813. it('gets extra formats', function () {
  814. expect(dtpElement.datetimepicker('extraFormats')).toBe(false);
  815. });
  816. it('sets extra formats', function () {
  817. var extraFormats = ['YYYY-MM-DD'];
  818. expect(dtpElement.datetimepicker('extraFormats', extraFormats)).toBe(dtpElement);
  819. expect(dtpElement.datetimepicker('extraFormats')).toEqual(extraFormats);
  820. });
  821. });
  822. });
  823. describe('toolbarPlacement() function', function () {
  824. describe('existence', function () {
  825. it('is defined', function () {
  826. expect(dtp.toolbarPlacement).toBeDefined();
  827. });
  828. });
  829. describe('check type and parameter validity', function () {
  830. it('does not accept a false value', function () {
  831. expect(function () {
  832. dtp.dayViewHeaderFormat(false);
  833. }).toThrow();
  834. });
  835. it('does not accept a false value', function () {
  836. expect(function () {
  837. dtp.dayViewHeaderFormat(false);
  838. }).toThrow();
  839. });
  840. it('accepts a string', function () {
  841. var toolbarPlacementOptions = ['default', 'top', 'bottom'];
  842. toolbarPlacementOptions.forEach(function (value) {
  843. expect(function () {
  844. dtp.toolbarPlacement(value);
  845. }).not.toThrow();
  846. });
  847. expect(function () {
  848. dtp.toolbarPlacement('test');
  849. }).toThrow();
  850. expect(function () {
  851. dtp.toolbarPlacement({});
  852. }).toThrow();
  853. });
  854. });
  855. describe('access', function () {
  856. it('gets toolbar placement', function () {
  857. expect(dtpElement.datetimepicker('toolbarPlacement')).toBe('default');
  858. });
  859. it('sets toolbar placement', function () {
  860. var toolbarPlacement = 'top';
  861. expect(dtpElement.datetimepicker('toolbarPlacement', toolbarPlacement)).toBe(dtpElement);
  862. expect(dtpElement.datetimepicker('toolbarPlacement')).toBe(toolbarPlacement);
  863. });
  864. });
  865. });
  866. describe('widgetParent() function', function () {
  867. describe('typechecking', function () {
  868. it('accepts a null', function () {
  869. expect(function () {
  870. dtp.widgetParent(null);
  871. }).not.toThrow();
  872. });
  873. it('accepts a string', function () {
  874. expect(function () {
  875. dtp.widgetParent('testDiv');
  876. }).not.toThrow();
  877. });
  878. it('accepts a jquery object', function () {
  879. expect(function () {
  880. dtp.widgetParent($('#testDiv'));
  881. }).not.toThrow();
  882. });
  883. it('does not accept undefined', function () {
  884. expect(function () {
  885. dtp.widgetParent(undefined);
  886. }).toThrow();
  887. });
  888. it('does not accept a number', function () {
  889. expect(function () {
  890. dtp.widgetParent(0);
  891. }).toThrow();
  892. });
  893. it('does not accept a generic Object', function () {
  894. expect(function () {
  895. dtp.widgetParent({});
  896. }).toThrow();
  897. });
  898. it('does not accept a boolean', function () {
  899. expect(function () {
  900. dtp.widgetParent(false);
  901. }).toThrow();
  902. });
  903. });
  904. describe('access', function () {
  905. it('gets widget parent', function () {
  906. expect(dtpElement.datetimepicker('widgetParent')).toBe(null);
  907. });
  908. it('sets widget parent', function () {
  909. expect(dtpElement.datetimepicker('widgetParent', 'testDiv')).toBe(dtpElement);
  910. expect(dtpElement.datetimepicker('widgetParent')).not.toBe(null);
  911. });
  912. });
  913. });
  914. describe('keepOpen() function', function () {
  915. describe('existence', function () {
  916. it('is defined', function () {
  917. expect(dtp.keepOpen).toBeDefined();
  918. });
  919. });
  920. describe('access', function () {
  921. it('gets keep open', function () {
  922. expect(dtpElement.datetimepicker('keepOpen')).toBe(false);
  923. });
  924. it('sets keep open', function () {
  925. var keepOpen = true;
  926. expect(dtpElement.datetimepicker('keepOpen', keepOpen)).toBe(dtpElement);
  927. expect(dtpElement.datetimepicker('keepOpen')).toBe(keepOpen);
  928. });
  929. });
  930. });
  931. describe('inline() function', function () {
  932. describe('existence', function () {
  933. it('is defined', function () {
  934. expect(dtp.inline).toBeDefined();
  935. });
  936. });
  937. describe('access', function () {
  938. it('gets inline', function () {
  939. expect(dtpElement.datetimepicker('inline')).toBe(false);
  940. });
  941. it('sets inline', function () {
  942. var inline = true;
  943. expect(dtpElement.datetimepicker('inline', inline)).toBe(dtpElement);
  944. expect(dtpElement.datetimepicker('inline')).toBe(inline);
  945. });
  946. });
  947. });
  948. describe('clear() function', function () {
  949. describe('existence', function () {
  950. it('is defined', function () {
  951. expect(dtp.clear).toBeDefined();
  952. });
  953. });
  954. describe('access', function () {
  955. it('returns jQuery object', function () {
  956. expect(dtpElement.datetimepicker('clear')).toBe(dtpElement);
  957. });
  958. });
  959. });
  960. describe('keyBinds() function', function () {
  961. describe('existence', function () {
  962. it('is defined', function () {
  963. expect(dtp.keyBinds).toBeDefined();
  964. });
  965. });
  966. describe('access', function () {
  967. it('gets key binds', function () {
  968. expect(dtpElement.datetimepicker('keyBinds')).toEqual(dtpElement.datetimepicker.defaults.keyBinds);
  969. });
  970. it('sets key binds', function () {
  971. var keyBinds = {up: function () {}};
  972. expect(dtpElement.datetimepicker('keyBinds', keyBinds)).toBe(dtpElement);
  973. expect(dtpElement.datetimepicker('keyBinds')).toEqual(keyBinds);
  974. });
  975. });
  976. });
  977. describe('parseInputDate() function', function () {
  978. describe('existence', function () {
  979. it('is defined', function () {
  980. expect(dtp.parseInputDate).toBeDefined();
  981. });
  982. });
  983. describe('access', function () {
  984. it('gets parse input date', function () {
  985. expect(dtpElement.datetimepicker('parseInputDate')).toBe(undefined);
  986. });
  987. it('sets parse input date', function () {
  988. var parseInputDate = function () {};
  989. expect(dtpElement.datetimepicker('parseInputDate', parseInputDate)).toBe(dtpElement);
  990. expect(dtpElement.datetimepicker('parseInputDate')).toBe(parseInputDate);
  991. });
  992. });
  993. });
  994. describe('Time zone tests', function () {
  995. function makeFormatTest (format, displayTimeZone) {
  996. it('should not change the value that was set when using format ' + format, function () { // #1326
  997. var oldFormat = dtp.format(),
  998. oldTimeZone = dtp.timeZone(),
  999. now = moment().startOf('second');
  1000. dtp.timeZone(displayTimeZone);
  1001. dtp.format(format);
  1002. dtp.date(now);
  1003. dpChangeSpy.calls.reset();
  1004. dtp.show();
  1005. dtp.hide();
  1006. expect(dpChangeSpy).not.toHaveBeenCalled();
  1007. expect(dtp.date().format()).toEqual(now.tz(displayTimeZone).format());
  1008. dtp.format(oldFormat);
  1009. dtp.timeZone(oldTimeZone);
  1010. });
  1011. }
  1012. makeFormatTest('YYYY-MM-DD HH:mm:ss Z', 'UTC');
  1013. makeFormatTest('YYYY-MM-DD HH:mm:ss', 'UTC');
  1014. makeFormatTest('YYYY-MM-DD HH:mm:ss Z', 'America/New_York');
  1015. makeFormatTest('YYYY-MM-DD HH:mm:ss', 'America/New_York');
  1016. });
  1017. });