Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Added a test for _dateObservable, fixed a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
petethepig committed Dec 21, 2015
1 parent 9e2d158 commit 0f7170d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/scripts/daterangepicker/config.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = =>
Expand Down
84 changes: 84 additions & 0 deletions test/config.coffee
Original file line number Diff line number Diff line change
@@ -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')
2 changes: 1 addition & 1 deletion website/styles/tests.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
margin: 0 -5px;
}

#mocha .suite {
#mocha #mocha-report>.suite {
margin-left: 0;
}

0 comments on commit 0f7170d

Please sign in to comment.