Mailspring/spec/components/date-input-spec.jsx

68 lines
2.3 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
import {
Simulate,
findRenderedDOMComponentWithClass,
} from 'react-addons-test-utils';
import {DateUtils} from 'nylas-exports'
import DateInput from '../../src/components/date-input';
import {renderIntoDocument} from '../nylas-test-utils'
const {findDOMNode} = ReactDOM;
2016-05-06 13:30:34 +08:00
const makeInput = (props = {}) => {
const input = renderIntoDocument(<DateInput {...props} dateFormat="blah" />);
if (props.initialState) {
input.setState(props.initialState)
}
return input
};
describe('DateInput', function dateInput() {
2016-05-06 13:30:34 +08:00
describe('onInputKeyDown', () => {
it('should submit the input if Enter or Escape pressed', () => {
const onSubmitDate = jasmine.createSpy('onSubmitDate')
2016-05-07 05:10:28 +08:00
const component = makeInput({onSubmitDate: onSubmitDate})
const inputNode = ReactDOM.findDOMNode(component).querySelector('input')
const stopPropagation = jasmine.createSpy('stopPropagation')
const keys = ['Enter', 'Return']
inputNode.value = 'tomorrow'
spyOn(DateUtils, 'futureDateFromString').andReturn('someday')
2016-05-07 05:10:28 +08:00
spyOn(component, 'setState')
2016-05-06 13:30:34 +08:00
keys.forEach((key) => {
Simulate.keyDown(inputNode, {key, stopPropagation})
expect(stopPropagation).toHaveBeenCalled()
expect(onSubmitDate).toHaveBeenCalledWith('someday', 'tomorrow')
2016-05-07 05:10:28 +08:00
expect(component.setState).toHaveBeenCalledWith({inputDate: null})
stopPropagation.reset()
onSubmitDate.reset()
2016-05-07 05:10:28 +08:00
component.setState.reset()
})
});
});
2016-05-06 13:30:34 +08:00
describe('render', () => {
beforeEach(() => {
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'))
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')
}).toThrow()
});
});
});