2016-03-29 16:41:24 +08:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import {
|
|
|
|
Simulate,
|
|
|
|
findRenderedDOMComponentWithClass,
|
2017-06-25 16:24:25 +08:00
|
|
|
} from 'react-dom/test-utils';
|
2016-03-29 16:41:24 +08:00
|
|
|
|
2016-03-04 04:37:20 +08:00
|
|
|
import {DateUtils} from 'nylas-exports'
|
|
|
|
import DateInput from '../../src/components/date-input';
|
|
|
|
import {renderIntoDocument} from '../nylas-test-utils'
|
|
|
|
|
2016-03-29 16:41:24 +08:00
|
|
|
const {findDOMNode} = ReactDOM;
|
2016-03-04 04:37:20 +08:00
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
const makeInput = (props = {}) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
const input = renderIntoDocument(<DateInput {...props} dateFormat="blah" />);
|
|
|
|
if (props.initialState) {
|
|
|
|
input.setState(props.initialState)
|
|
|
|
}
|
|
|
|
return input
|
|
|
|
};
|
|
|
|
|
2016-05-05 05:03:15 +08:00
|
|
|
describe('DateInput', function dateInput() {
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('onInputKeyDown', () => {
|
|
|
|
it('should submit the input if Enter or Escape pressed', () => {
|
2016-10-14 00:43:20 +08:00
|
|
|
const onDateSubmitted = jasmine.createSpy('onDateSubmitted')
|
|
|
|
const component = makeInput({onDateSubmitted: onDateSubmitted})
|
2016-05-07 05:10:28 +08:00
|
|
|
const inputNode = ReactDOM.findDOMNode(component).querySelector('input')
|
2016-03-04 04:37:20 +08:00
|
|
|
const stopPropagation = jasmine.createSpy('stopPropagation')
|
|
|
|
const keys = ['Enter', 'Return']
|
|
|
|
inputNode.value = 'tomorrow'
|
|
|
|
spyOn(DateUtils, 'futureDateFromString').andReturn('someday')
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
keys.forEach((key) => {
|
2016-03-04 04:37:20 +08:00
|
|
|
Simulate.keyDown(inputNode, {key, stopPropagation})
|
|
|
|
expect(stopPropagation).toHaveBeenCalled()
|
2016-10-14 00:43:20 +08:00
|
|
|
expect(onDateSubmitted).toHaveBeenCalledWith('someday', 'tomorrow')
|
2016-03-04 04:37:20 +08:00
|
|
|
stopPropagation.reset()
|
2016-10-14 00:43:20 +08:00
|
|
|
onDateSubmitted.reset()
|
2016-03-04 04:37:20 +08:00
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
describe('render', () => {
|
|
|
|
beforeEach(() => {
|
2016-03-04 04:37:20 +08:00
|
|
|
spyOn(DateUtils, 'format').andReturn('formatted')
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('should render a date interpretation if a date has been inputted', () => {
|
2016-05-07 05:10:28 +08:00
|
|
|
const component = makeInput({initialState: {inputDate: 'something!'}})
|
|
|
|
spyOn(component, 'setState')
|
|
|
|
const dateInterpretation = findDOMNode(findRenderedDOMComponentWithClass(component, 'date-interpretation'))
|
2016-03-04 04:37:20 +08:00
|
|
|
|
|
|
|
expect(dateInterpretation.textContent).toEqual('formatted')
|
|
|
|
});
|
|
|
|
|
2016-05-06 13:30:34 +08:00
|
|
|
it('should not render a date interpretation if no input date available', () => {
|
2016-05-07 05:10:28 +08:00
|
|
|
const component = makeInput({initialState: {inputDate: null}})
|
|
|
|
spyOn(component, 'setState')
|
2016-05-06 13:30:34 +08:00
|
|
|
expect(() => {
|
2016-05-07 05:10:28 +08:00
|
|
|
findRenderedDOMComponentWithClass(component, 'date-interpretation')
|
2016-03-04 04:37:20 +08:00
|
|
|
}).toThrow()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|