diff --git a/src/scripts/daterangepicker/config.coffee b/src/scripts/daterangepicker/config.coffee index 3a9823c..a8fcf3d 100644 --- a/src/scripts/daterangepicker/config.coffee +++ b/src/scripts/daterangepicker/config.coffee @@ -113,16 +113,18 @@ class Config computed.mode = mode || 'inclusive' - computed.fit = (val) => - val = MomentUtil.tz(val, @timeZone()) + fitMin = (val) => if minBoundary min = minBoundary() switch minBoundary.mode when 'extended' min = min.clone().startOf(@period()) when 'exclusive' - min = min.clone().endOf(@period()).subtract(1, 'millisecond') + min = min.clone().endOf(@period()).add(1, 'millisecond') val = moment.max(min, val) + val + + fitMax = (val) => if maxBoundary max = maxBoundary() switch maxBoundary.mode @@ -133,6 +135,10 @@ class Config val = moment.min(max, val) val + computed.fit = (val) => + val = MomentUtil.tz(val, @timeZone()) + fitMax(fitMin(val)) + computed(val) computed.clone = => diff --git a/test/config.coffee b/test/config.coffee new file mode 100644 index 0000000..6369113 --- /dev/null +++ b/test/config.coffee @@ -0,0 +1,84 @@ +describe 'Config', -> + Config = $.fn.daterangepicker.Config + fmt = 'YYYY-MM-DD' + describe 'dateObservable', -> + describe '#fit()', -> + describe 'minDate', -> + describe 'inclusive mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2015-05-14'), 'inclusive'] + maxDate: [moment.utc('2016-01-01'), 'inclusive'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-05-25') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-06-15') + + describe 'exclusive mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2015-05-14'), 'exclusive'] + maxDate: [moment.utc('2016-01-01'), 'inclusive'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-06-01') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-06-01') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-06-01') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-06-01') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-06-15') + + describe 'extended mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2015-05-14'), 'extended'] + maxDate: [moment.utc('2016-01-01'), 'inclusive'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-05-01') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-05-05') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-05-25') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-06-15') + + describe 'maxDate', -> + describe 'inclusive mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2014-01-01'), 'inclusive'] + maxDate: [moment.utc('2015-05-14'), 'inclusive'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-04-15') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-05-05') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-05-14') + + describe 'exclusive mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2014-01-01'), 'inclusive'] + maxDate: [moment.utc('2015-05-14'), 'exclusive'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-04-15') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-04-30') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-04-30') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-04-30') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-04-30') + + describe 'extended mode', -> + it 'should work', () -> + c = new Config({ + period: 'month' + minDate: [moment.utc('2014-01-01'), 'inclusive'] + maxDate: [moment.utc('2015-05-14'), 'extended'] + }) + assert.equal(c.startDate.fit('2015-04-15').format(fmt), '2015-04-15') + assert.equal(c.startDate.fit('2015-05-05').format(fmt), '2015-05-05') + assert.equal(c.startDate.fit('2015-05-14').format(fmt), '2015-05-14') + assert.equal(c.startDate.fit('2015-05-25').format(fmt), '2015-05-25') + assert.equal(c.startDate.fit('2015-06-15').format(fmt), '2015-05-31') diff --git a/website/styles/tests.scss b/website/styles/tests.scss index 9e997ce..187d097 100644 --- a/website/styles/tests.scss +++ b/website/styles/tests.scss @@ -8,6 +8,6 @@ margin: 0 -5px; } -#mocha .suite { +#mocha #mocha-report>.suite { margin-left: 0; }