Mailspring/packages/client-app/spec/components/date-input-spec.jsx

65 lines
2.2 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
import {
Simulate,
findRenderedDOMComponentWithClass,
} from 'react-dom/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 onDateSubmitted = jasmine.createSpy('onDateSubmitted')
const component = makeInput({onDateSubmitted: onDateSubmitted})
2016-05-07 05:10:28 +08:00
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-06 13:30:34 +08:00
keys.forEach((key) => {
Simulate.keyDown(inputNode, {key, stopPropagation})
expect(stopPropagation).toHaveBeenCalled()
expect(onDateSubmitted).toHaveBeenCalledWith('someday', 'tomorrow')
stopPropagation.reset()
onDateSubmitted.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()
});
});
});