|
1 | 1 | var isNumeric = require('fast-isnumeric'); |
| 2 | + |
2 | 3 | var Lib = require('@src/lib'); |
| 4 | +var calComponent = require('@src/components/calendars'); |
| 5 | + |
| 6 | +// use only the parts of world-calendars that we've imported for our tests |
| 7 | +var calendars = require('@src/components/calendars/calendars'); |
3 | 8 |
|
4 | 9 | describe('dates', function() { |
5 | 10 | 'use strict'; |
@@ -240,6 +245,7 @@ describe('dates', function() { |
240 | 245 | [ |
241 | 246 | [undefined, '1970-01-01'], |
242 | 247 | ['gregorian', '1970-01-01'], |
| 248 | + ['chinese', '1969-11-24'], |
243 | 249 | ['coptic', '1686-04-23'], |
244 | 250 | ['discworld', '1798-12-27'], |
245 | 251 | ['ethiopian', '1962-04-23'], |
@@ -284,6 +290,55 @@ describe('dates', function() { |
284 | 290 | expect(Lib.dateTime2ms(expected_lastinstant, calendar)).toBe(lastInstant, calendar); |
285 | 291 | }); |
286 | 292 | }); |
| 293 | + |
| 294 | + it('should contain canonical ticks sundays, ranges for all calendars', function() { |
| 295 | + var calList = Object.keys(calendars.calendars).filter(function(v) { |
| 296 | + return v !== 'gregorian'; |
| 297 | + }); |
| 298 | + |
| 299 | + var canonicalTick = calComponent.CANONICAL_TICK, |
| 300 | + canonicalSunday = calComponent.CANONICAL_SUNDAY, |
| 301 | + dfltRange = calComponent.DFLTRANGE; |
| 302 | + expect(Object.keys(canonicalTick).length).toBe(calList.length); |
| 303 | + expect(Object.keys(canonicalSunday).length).toBe(calList.length); |
| 304 | + expect(Object.keys(dfltRange).length).toBe(calList.length); |
| 305 | + |
| 306 | + calList.forEach(function(calendar) { |
| 307 | + expect(Lib.dateTime2ms(canonicalTick[calendar], calendar)).toBeDefined(calendar); |
| 308 | + var sunday = Lib.dateTime2ms(canonicalSunday[calendar], calendar); |
| 309 | + // convert back implicitly with gregorian calendar |
| 310 | + expect(Lib.formatDate(sunday, '%A')).toBe('Sunday', calendar); |
| 311 | + |
| 312 | + expect(Lib.dateTime2ms(dfltRange[calendar][0], calendar)).toBeDefined(calendar); |
| 313 | + expect(Lib.dateTime2ms(dfltRange[calendar][1], calendar)).toBeDefined(calendar); |
| 314 | + }); |
| 315 | + }); |
| 316 | + |
| 317 | + it('should handle Chinese intercalary months correctly', function() { |
| 318 | + var intercalaryDates = [ |
| 319 | + '1995-08i-01', |
| 320 | + '1995-08i-29', |
| 321 | + '1984-10i-15', |
| 322 | + '2023-02i-29' |
| 323 | + ]; |
| 324 | + intercalaryDates.forEach(function(v) { |
| 325 | + var ms = Lib.dateTime2ms(v, 'chinese'); |
| 326 | + expect(Lib.ms2DateTime(ms, 0, 'chinese')).toBe(v); |
| 327 | + |
| 328 | + // should also work without leading zeros |
| 329 | + var vShort = v.replace(/-0/g, '-'); |
| 330 | + expect(Lib.dateTime2ms(vShort, 'chinese')).toBe(ms, vShort); |
| 331 | + }); |
| 332 | + |
| 333 | + var badIntercalaryDates = [ |
| 334 | + '1995-07i-01', |
| 335 | + '1995-08i-30', |
| 336 | + '1995-09i-01' |
| 337 | + ]; |
| 338 | + badIntercalaryDates.forEach(function(v) { |
| 339 | + expect(Lib.dateTime2ms(v, 'chinese')).toBeUndefined(v); |
| 340 | + }); |
| 341 | + }); |
287 | 342 | }); |
288 | 343 |
|
289 | 344 | describe('cleanDate', function() { |
@@ -341,6 +396,51 @@ describe('dates', function() { |
341 | 396 | }); |
342 | 397 | }); |
343 | 398 |
|
| 399 | + describe('incrementMonth', function() { |
| 400 | + it('should include Chinese intercalary months', function() { |
| 401 | + var start = '1995-06-01'; |
| 402 | + var expected = [ |
| 403 | + '1995-07-01', |
| 404 | + '1995-08-01', |
| 405 | + '1995-08i-01', |
| 406 | + '1995-09-01', |
| 407 | + '1995-10-01', |
| 408 | + '1995-11-01', |
| 409 | + '1995-12-01', |
| 410 | + '1996-01-01' |
| 411 | + ]; |
| 412 | + var tick = Lib.dateTime2ms(start, 'chinese'); |
| 413 | + expected.forEach(function(v) { |
| 414 | + tick = Lib.incrementMonth(tick, 1, 'chinese'); |
| 415 | + expect(tick).toBe(Lib.dateTime2ms(v, 'chinese'), v); |
| 416 | + }); |
| 417 | + }); |
| 418 | + |
| 419 | + it('should increment years even over leap years', function() { |
| 420 | + var start = '1995-06-01'; |
| 421 | + var expected = [ |
| 422 | + '1996-06-01', |
| 423 | + '1997-06-01', |
| 424 | + '1998-06-01', |
| 425 | + '1999-06-01', |
| 426 | + '2000-06-01', |
| 427 | + '2001-06-01', |
| 428 | + '2002-06-01', |
| 429 | + '2003-06-01', |
| 430 | + '2004-06-01', |
| 431 | + '2005-06-01', |
| 432 | + '2006-06-01', |
| 433 | + '2007-06-01', |
| 434 | + '2008-06-01' |
| 435 | + ]; |
| 436 | + var tick = Lib.dateTime2ms(start, 'chinese'); |
| 437 | + expected.forEach(function(v) { |
| 438 | + tick = Lib.incrementMonth(tick, 12, 'chinese'); |
| 439 | + expect(tick).toBe(Lib.dateTime2ms(v, 'chinese'), v); |
| 440 | + }); |
| 441 | + }); |
| 442 | + }); |
| 443 | + |
344 | 444 | describe('isJSDate', function() { |
345 | 445 | it('should return true for any Date object but not the equivalent numbers', function() { |
346 | 446 | [ |
|
0 commit comments