<NME> DateTimePicker-test.js
<BEF> import React from 'react';
import Globalize from 'globalize';
import { mount, shallow } from 'enzyme';

import DateTimePicker from '../src/DateTimePicker'
import TimeList from '../src/TimeList'
import Calendar from '../src/Calendar'

let ControlledDateTimePicker = DateTimePicker.ControlledComponent;

describe('DateTimePicker', () => {

  it('should set initial values', () => {
    var date = new Date();

    expect(
      mount(<DateTimePicker defaultValue={date} format="MM-dd-yyyy"/>)
        .find('.rw-input')
        .getDOMNode()
        .value
    )
    .to.equal(Globalize.format(date, 'MM-dd-yyyy'))
  })

  it('should start closed', () => {

    let inst = shallow(<ControlledDateTimePicker />)

    expect(inst.prop('open')).to.not.equal(true)

    expect(inst.find('Popup').first().prop('open')).to.not.equal(true)

    inst.assertNone('.rw-open')
    inst.assertSingle(`DateTimePickerInput[aria-expanded=false]`)
    let wrapper = mount(<DatePicker onToggle={onOpen} />)

    wrapper.find('button.rw-picker-btn').simulate('click')


    let wrapper = shallow(<ControlledDateTimePicker onToggle={onOpen} />)

    wrapper.find('Button')
      .first()
      .simulate('click')

    wrapper.find('Button')
      .last()
      .simulate('click');

    expect(onOpen.calledTwice).to.equal(true)
  })

  it('should change when selecting a date', () => {
    let change = sinon.spy()

    shallow(
      <ControlledDateTimePicker
        open='date'
        onChange={change}
        onToggle={()=>{}}
      />
    )
    .assertSingle(Calendar)
    .simulate('change', new Date());

    expect(change.calledOnce).to.equal(true)
  })

  it('should change when selecting a time', () => {
    let change = sinon.spy()
      , select = sinon.spy()

    shallow(
      <ControlledDateTimePicker
        open='date'
        onChange={change}
        onSelect={select}
        onToggle={()=>{}}
      />
    )
    .assertSingle(TimeList)
    .simulate('select', { date: new Date() });

    expect(select.calledOnce).to.equal(true)
    expect(change.calledAfter(select)).to.equal(true)

  })

  it('should change when selecting a time', () => {
    let change = sinon.spy()
      , select = sinon.spy();

    shallow(
      <ControlledDateTimePicker
        open='date'
        onChange={change}
        onSelect={select}
        onToggle={()=>{}}
      />
    )
    .assertSingle(TimeList)
    .simulate('select', { date: new Date() });

    expect(select.calledOnce).to.equal(true)
    expect(change.calledAfter(select)).to.equal(true)
    let kp = sinon.spy(),
      kd = sinon.spy(),
      ku = sinon.spy()
  it('should set id on list', () => {
    expect(
      mount(<DateTimePicker />)
        .find('ul').getDOMNode()
        .hasAttribute('id')
    ).to.equal(true);
  })

  it('should not show time button when not selected', () => {
    var spy = sinon.spy()

    mount(
      <DateTimePicker time={false} date={false} onToggle={spy }/>
    )
    .tap(_ => _.assertNone('.rw-btn-time'))
    .tap(_ => _.assertNone('.rw-btn-calendar'))
    .simulate('keyDown', { altKey: true })
    .simulate('keyDown', { altKey: true })

    expect(spy.callCount).to.equal(0)
  })


  it('should simulate focus/blur events', (done) => {
    let blur = sinon.spy()
    let focus = sinon.spy()

  })

  it('should do nothing when readonly', () => {
    let spy = sinon.spy()
    let wrapper = mount(
      <DatePicker defaultValue={new Date()} readOnly onToggle={spy} />,
    )

    let input = wrapper.find('input.rw-input').getDOMNode()

    expect(input.hasAttribute('readonly')).to.equal(true)

    wrapper.find('.rw-i-calendar').simulate('click').update()

    expect(spy.called).to.equal(false)
  })
  })

  it('should simulate key events', () => {
    let kp = sinon.spy()
      , kd = sinon.spy()
      , ku = sinon.spy()

    mount(
      <DateTimePicker onKeyPress={kp} onKeyUp={ku} onKeyDown={kd}/>
    )
    .find('.rw-input')
    .simulate('keyPress')
    .simulate('keyDown')
    .simulate('keyUp')

    expect(kp.calledOnce).to.equal(true)
    expect(kd.calledOnce).to.equal(true)
    expect(ku.calledOnce).to.equal(true)
  })

  it('should do nothing when disabled', (done) => {
    let wrapper = mount(
      <DateTimePicker defaultValue={new Date()} disabled />
    )

    let input = wrapper.find('.rw-input').getDOMNode();

    expect(input.hasAttribute('disabled')).to.equal(true);

    wrapper.find('.rw-i-calendar').simulate('click')

    }, 0)
  })

  it('should do nothing when readonly', (done) => {
    let wrapper = mount(
      <DateTimePicker defaultValue={new Date()} readOnly />
    )

    let input = wrapper.find('.rw-input').getDOMNode();

    expect(input.hasAttribute('readonly')).to.equal(true);

    wrapper.find('.rw-i-calendar').simulate('click')

  it('should change values on key down', () => {
    let change = sinon.spy()

    let wrapper = mount(
      <DateTimePicker onChange={change} />
    )

    let options = wrapper.find('li').map(n => n.getDOMNode())


    wrapper.simulate('keyDown', { key: 'Home' })

    expect(options[0].className)
      .to.match(/\brw-state-focus\b/)

    wrapper.simulate('keyDown', { key: 'End' })

    expect(options[options.length - 1].className)
      .to.match(/\brw-state-focus\b/)

    wrapper.simulate('keyDown', { key: 'ArrowUp' })

    expect(options[options.length - 2].className)
      .to.match(/\brw-state-focus\b/)

    wrapper.simulate('keyDown', { key: 'ArrowDown' })

    expect(options[options.length - 1].className)
      .to.match(/\brw-state-focus\b/)
  })


  describe('TimeList', () => {

    it('should render max correctly', ()=>{
      let date = new Date(2014, 0, 16, 9, 30)
      let inst = mount(
        <TimeList
          value={new Date(2014, 0, 16, 8)}
          max={date}
          preserveDate
        />
      )

      let dates = inst.state('dates');
      let time = dates[dates.length - 1]

      expect(time.date.getHours()).to.eql(9)

      inst.setProps({ value: new Date(2014, 0, 15, 8) })

      dates = inst.state('dates');
      time = dates[dates.length - 1]

      expect(time.date.getHours()).to.eql(23)
      expect(time.date.getSeconds()).to.eql(0)
    })

    it('should render min correctly', ()=>{
      let date = new Date(2014, 0, 16, 9, 30)

      let inst = mount(
        <TimeList value={new Date(2014, 0, 16, 12)} min={date} preserveDate />
      )

      let time = inst.state('dates')[0];

      expect(time.date.getHours()).to.eql(9)
      expect(time.date.getMinutes()).to.eql(30)
      expect(time.date.getSeconds()).to.eql(0)

      inst = mount(<TimeList value={new Date(2014, 0, 18, 8)} min={date} preserveDate />)
      time = inst.state('dates')[0];

      expect(time.date.getHours()).to.eql(0)
      expect(time.date.getMinutes()).to.eql(0)
      expect(time.date.getSeconds()).to.eql(0)
    })


    it('should set the step property', ()=>{
      let dates = mount(<DateTimePicker step={60} />)
        .find(TimeList)
        .instance().state.dates
      expect(dates[2].date.getHours()).to.equal(4)
    })
  })

})

<MSG> fix defaultView and simplify RTL logic

closes #711

<DFF> @@ -1,34 +1,35 @@
-import React from 'react';
-import Globalize from 'globalize';
-import { mount, shallow } from 'enzyme';
+import React from 'react'
+import Globalize from 'globalize'
+import { mount, shallow } from 'enzyme'
 
 import DateTimePicker from '../src/DateTimePicker'
 import TimeList from '../src/TimeList'
 import Calendar from '../src/Calendar'
 
-let ControlledDateTimePicker = DateTimePicker.ControlledComponent;
+let ControlledDateTimePicker = DateTimePicker.ControlledComponent
 
 describe('DateTimePicker', () => {
-
   it('should set initial values', () => {
-    var date = new Date();
+    var date = new Date()
 
     expect(
-      mount(<DateTimePicker defaultValue={date} format="MM-dd-yyyy"/>)
+      mount(<DateTimePicker defaultValue={date} format="MM-dd-yyyy" />)
         .find('.rw-input')
-        .getDOMNode()
-        .value
-    )
-    .to.equal(Globalize.format(date, 'MM-dd-yyyy'))
+        .getDOMNode().value
+    ).to.equal(Globalize.format(date, 'MM-dd-yyyy'))
   })
 
   it('should start closed', () => {
-
     let inst = shallow(<ControlledDateTimePicker />)
 
     expect(inst.prop('open')).to.not.equal(true)
 
-    expect(inst.find('Popup').first().prop('open')).to.not.equal(true)
+    expect(
+      inst
+        .find('Popup')
+        .first()
+        .prop('open')
+    ).to.not.equal(true)
 
     inst.assertNone('.rw-open')
     inst.assertSingle(`DateTimePickerInput[aria-expanded=false]`)
@@ -39,47 +40,57 @@ describe('DateTimePicker', () => {
 
     let wrapper = shallow(<ControlledDateTimePicker onToggle={onOpen} />)
 
-    wrapper.find('Button')
+    wrapper
+      .find('Button')
       .first()
       .simulate('click')
 
-    wrapper.find('Button')
+    wrapper
+      .find('Button')
       .last()
-      .simulate('click');
+      .simulate('click')
 
     expect(onOpen.calledTwice).to.equal(true)
   })
 
+  it('passes default props to calendar', () => {
+    let wrapper = mount(
+      <ControlledDateTimePicker open="date" defaultView="year" />
+    )
+
+    expect(wrapper.find(Calendar.ControlledComponent).props().view).to.equal('year')
+  })
+
   it('should change when selecting a date', () => {
     let change = sinon.spy()
 
     shallow(
       <ControlledDateTimePicker
-        open='date'
+        open="date"
         onChange={change}
-        onToggle={()=>{}}
+        onToggle={() => {}}
       />
     )
-    .assertSingle(Calendar)
-    .simulate('change', new Date());
+      .assertSingle(Calendar)
+      .simulate('change', new Date())
 
     expect(change.calledOnce).to.equal(true)
   })
 
   it('should change when selecting a time', () => {
-    let change = sinon.spy()
-      , select = sinon.spy()
+    let change = sinon.spy(),
+      select = sinon.spy()
 
     shallow(
       <ControlledDateTimePicker
-        open='date'
+        open="date"
         onChange={change}
         onSelect={select}
-        onToggle={()=>{}}
+        onToggle={() => {}}
       />
     )
-    .assertSingle(TimeList)
-    .simulate('select', { date: new Date() });
+      .assertSingle(TimeList)
+      .simulate('select', { date: new Date() })
 
     expect(select.calledOnce).to.equal(true)
     expect(change.calledAfter(select)).to.equal(true)
@@ -87,19 +98,19 @@ describe('DateTimePicker', () => {
   })
 
   it('should change when selecting a time', () => {
-    let change = sinon.spy()
-      , select = sinon.spy();
+    let change = sinon.spy(),
+      select = sinon.spy()
 
     shallow(
       <ControlledDateTimePicker
-        open='date'
+        open="date"
         onChange={change}
         onSelect={select}
-        onToggle={()=>{}}
+        onToggle={() => {}}
       />
     )
-    .assertSingle(TimeList)
-    .simulate('select', { date: new Date() });
+      .assertSingle(TimeList)
+      .simulate('select', { date: new Date() })
 
     expect(select.calledOnce).to.equal(true)
     expect(change.calledAfter(select)).to.equal(true)
@@ -109,27 +120,25 @@ describe('DateTimePicker', () => {
   it('should set id on list', () => {
     expect(
       mount(<DateTimePicker />)
-        .find('ul').getDOMNode()
+        .find('ul')
+        .getDOMNode()
         .hasAttribute('id')
-    ).to.equal(true);
+    ).to.equal(true)
   })
 
   it('should not show time button when not selected', () => {
     var spy = sinon.spy()
 
-    mount(
-      <DateTimePicker time={false} date={false} onToggle={spy }/>
-    )
-    .tap(_ => _.assertNone('.rw-btn-time'))
-    .tap(_ => _.assertNone('.rw-btn-calendar'))
-    .simulate('keyDown', { altKey: true })
-    .simulate('keyDown', { altKey: true })
+    mount(<DateTimePicker time={false} date={false} onToggle={spy} />)
+      .tap(_ => _.assertNone('.rw-btn-time'))
+      .tap(_ => _.assertNone('.rw-btn-calendar'))
+      .simulate('keyDown', { altKey: true })
+      .simulate('keyDown', { altKey: true })
 
     expect(spy.callCount).to.equal(0)
   })
 
-
-  it('should simulate focus/blur events', (done) => {
+  it('should simulate focus/blur events', done => {
     let blur = sinon.spy()
     let focus = sinon.spy()
 
@@ -152,31 +161,27 @@ describe('DateTimePicker', () => {
   })
 
   it('should simulate key events', () => {
-    let kp = sinon.spy()
-      , kd = sinon.spy()
-      , ku = sinon.spy()
+    let kp = sinon.spy(),
+      kd = sinon.spy(),
+      ku = sinon.spy()
 
-    mount(
-      <DateTimePicker onKeyPress={kp} onKeyUp={ku} onKeyDown={kd}/>
-    )
-    .find('.rw-input')
-    .simulate('keyPress')
-    .simulate('keyDown')
-    .simulate('keyUp')
+    mount(<DateTimePicker onKeyPress={kp} onKeyUp={ku} onKeyDown={kd} />)
+      .find('.rw-input')
+      .simulate('keyPress')
+      .simulate('keyDown')
+      .simulate('keyUp')
 
     expect(kp.calledOnce).to.equal(true)
     expect(kd.calledOnce).to.equal(true)
     expect(ku.calledOnce).to.equal(true)
   })
 
-  it('should do nothing when disabled', (done) => {
-    let wrapper = mount(
-      <DateTimePicker defaultValue={new Date()} disabled />
-    )
+  it('should do nothing when disabled', done => {
+    let wrapper = mount(<DateTimePicker defaultValue={new Date()} disabled />)
 
-    let input = wrapper.find('.rw-input').getDOMNode();
+    let input = wrapper.find('.rw-input').getDOMNode()
 
-    expect(input.hasAttribute('disabled')).to.equal(true);
+    expect(input.hasAttribute('disabled')).to.equal(true)
 
     wrapper.find('.rw-i-calendar').simulate('click')
 
@@ -186,14 +191,12 @@ describe('DateTimePicker', () => {
     }, 0)
   })
 
-  it('should do nothing when readonly', (done) => {
-    let wrapper = mount(
-      <DateTimePicker defaultValue={new Date()} readOnly />
-    )
+  it('should do nothing when readonly', done => {
+    let wrapper = mount(<DateTimePicker defaultValue={new Date()} readOnly />)
 
-    let input = wrapper.find('.rw-input').getDOMNode();
+    let input = wrapper.find('.rw-input').getDOMNode()
 
-    expect(input.hasAttribute('readonly')).to.equal(true);
+    expect(input.hasAttribute('readonly')).to.equal(true)
 
     wrapper.find('.rw-i-calendar').simulate('click')
 
@@ -206,9 +209,7 @@ describe('DateTimePicker', () => {
   it('should change values on key down', () => {
     let change = sinon.spy()
 
-    let wrapper = mount(
-      <DateTimePicker onChange={change} />
-    )
+    let wrapper = mount(<DateTimePicker onChange={change} />)
 
     let options = wrapper.find('li').map(n => n.getDOMNode())
 
@@ -222,39 +223,29 @@ describe('DateTimePicker', () => {
 
     wrapper.simulate('keyDown', { key: 'Home' })
 
-    expect(options[0].className)
-      .to.match(/\brw-state-focus\b/)
+    expect(options[0].className).to.match(/\brw-state-focus\b/)
 
     wrapper.simulate('keyDown', { key: 'End' })
 
-    expect(options[options.length - 1].className)
-      .to.match(/\brw-state-focus\b/)
+    expect(options[options.length - 1].className).to.match(/\brw-state-focus\b/)
 
     wrapper.simulate('keyDown', { key: 'ArrowUp' })
 
-    expect(options[options.length - 2].className)
-      .to.match(/\brw-state-focus\b/)
+    expect(options[options.length - 2].className).to.match(/\brw-state-focus\b/)
 
     wrapper.simulate('keyDown', { key: 'ArrowDown' })
 
-    expect(options[options.length - 1].className)
-      .to.match(/\brw-state-focus\b/)
+    expect(options[options.length - 1].className).to.match(/\brw-state-focus\b/)
   })
 
-
   describe('TimeList', () => {
-
-    it('should render max correctly', ()=>{
+    it('should render max correctly', () => {
       let date = new Date(2014, 0, 16, 9, 30)
       let inst = mount(
-        <TimeList
-          value={new Date(2014, 0, 16, 8)}
-          max={date}
-          preserveDate
-        />
+        <TimeList value={new Date(2014, 0, 16, 8)} max={date} preserveDate />
       )
 
-      let dates = inst.state('dates');
+      let dates = inst.state('dates')
       let time = dates[dates.length - 1]
 
       expect(time.date.getHours()).to.eql(9)
@@ -263,7 +254,7 @@ describe('DateTimePicker', () => {
 
       inst.setProps({ value: new Date(2014, 0, 15, 8) })
 
-      dates = inst.state('dates');
+      dates = inst.state('dates')
       time = dates[dates.length - 1]
 
       expect(time.date.getHours()).to.eql(23)
@@ -271,29 +262,30 @@ describe('DateTimePicker', () => {
       expect(time.date.getSeconds()).to.eql(0)
     })
 
-    it('should render min correctly', ()=>{
+    it('should render min correctly', () => {
       let date = new Date(2014, 0, 16, 9, 30)
 
       let inst = mount(
         <TimeList value={new Date(2014, 0, 16, 12)} min={date} preserveDate />
       )
 
-      let time = inst.state('dates')[0];
+      let time = inst.state('dates')[0]
 
       expect(time.date.getHours()).to.eql(9)
       expect(time.date.getMinutes()).to.eql(30)
       expect(time.date.getSeconds()).to.eql(0)
 
-      inst = mount(<TimeList value={new Date(2014, 0, 18, 8)} min={date} preserveDate />)
-      time = inst.state('dates')[0];
+      inst = mount(
+        <TimeList value={new Date(2014, 0, 18, 8)} min={date} preserveDate />
+      )
+      time = inst.state('dates')[0]
 
       expect(time.date.getHours()).to.eql(0)
       expect(time.date.getMinutes()).to.eql(0)
       expect(time.date.getSeconds()).to.eql(0)
     })
 
-
-    it('should set the step property', ()=>{
+    it('should set the step property', () => {
       let dates = mount(<DateTimePicker step={60} />)
         .find(TimeList)
         .instance().state.dates
@@ -311,5 +303,4 @@ describe('DateTimePicker', () => {
       expect(dates[2].date.getHours()).to.equal(4)
     })
   })
-
 })

fix defaultView and simplify RTL logic