Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Commit fe0a5d6

Browse files
committed
Merge branch 'theresakhoo-master'
2 parents 6aa767c + ad854f0 commit fe0a5d6

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/DateTimeField.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class DateTimeField extends Component {
6868
state.inputValue = moment(nextProps.dateTime, nextProps.format, true).format(nextProps.inputFormat);
6969
}
7070

71-
if (moment(nextProps.dateTime, nextProps.format, true).isValid()) {
71+
if (nextProps.dateTime !== this.props.dateTime && moment(nextProps.dateTime, nextProps.format, true).isValid()) {
7272
state.viewDate = moment(nextProps.dateTime, nextProps.format, true).startOf("month");
7373
state.selectedDate = moment(nextProps.dateTime, nextProps.format, true);
7474
state.inputValue = moment(nextProps.dateTime, nextProps.format, true).format(nextProps.inputFormat ? nextProps.inputFormat : this.state.inputFormat);

src/__tests__/DateTimeField-test.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,62 @@ describe("DateTimeField", function() {
77
const moment = require("moment");
88
const DateTimeField = require("../DateTimeField.js");
99
const happyDate = moment("1990-06-05 07:30");
10-
let parent, TestParent;
11-
12-
beforeEach(() => {
13-
TestParent = React.createFactory(React.createClass({
14-
getInitialState() {
15-
return { dateTime: happyDate.format("x") };
16-
},
17-
18-
render() {
19-
return <DateTimeField {...this.state} />;
20-
}
21-
}));
22-
parent = TestUtils.renderIntoDocument(TestParent()); // eslint-disable-line
23-
});
10+
let createParent, TestParent;
11+
2412

2513
describe("By default", function() {
2614

2715
it("shows the right date for a given dateTime and inputFormat", function() {
28-
const input = TestUtils.findRenderedDOMComponentWithTag(parent, "input");
16+
const component = TestUtils.renderIntoDocument(<DateTimeField dateTime={happyDate.format("x")} />);
17+
const input = TestUtils.findRenderedDOMComponentWithTag(component, "input");
2918
expect(input.getDOMNode().value).toBe("06/05/90 7:30 AM");
3019
});
3120

3221
});
3322

3423
describe("When changing props", function() {
3524

36-
it("changes the displayed date when dateTime changes", function() {
37-
const input = TestUtils.findRenderedDOMComponentWithTag(parent, "input");
38-
expect(input.getDOMNode().value).toBe("06/05/90 7:30 AM");
39-
parent.setState({dateTime: moment("1981-06-04 05:45").format("x")});
40-
expect(input.getDOMNode().value).toBe("06/04/81 5:45 AM");
25+
beforeEach(() => {
26+
TestParent = React.createFactory(React.createClass({
27+
getInitialState() {
28+
return {
29+
dateTime: happyDate.format("x"),
30+
...this.props
31+
};
32+
},
33+
34+
render() {
35+
return <DateTimeField {...this.state} />;
36+
}
37+
}));
38+
createParent = (initalState) => TestUtils.renderIntoDocument(TestParent(initalState)); // eslint-disable-line
4139
});
4240

43-
it("changes the displayed format when inputFormat changes", function() {
44-
const input = TestUtils.findRenderedDOMComponentWithTag(parent, "input");
45-
expect(input.getDOMNode().value).toBe("06/05/90 7:30 AM");
46-
parent.setState({inputFormat: "x"});
47-
expect(input.getDOMNode().value).toBe(happyDate.format("x"));
41+
it("changes the displayed date when dateTime changes", function() {
42+
const Parent = createParent();
43+
const input = TestUtils.findRenderedDOMComponentWithTag(Parent, "input");
44+
expect(input.getDOMNode().value).toBe("06/05/90 7:30 AM");
45+
Parent.setState({dateTime: moment("1981-06-04 05:45").format("x")});
46+
expect(input.getDOMNode().value).toBe("06/04/81 5:45 AM");
47+
});
48+
49+
it("changes the displayed format when inputFormat changes", function() {
50+
const Parent = createParent();
51+
const input = TestUtils.findRenderedDOMComponentWithTag(Parent, "input");
52+
expect(input.getDOMNode().value).toBe("06/05/90 7:30 AM");
53+
Parent.setState({inputFormat: "x"});
54+
expect(input.getDOMNode().value).toBe(happyDate.format("x"));
55+
});
56+
57+
it("doesn't change the defaultText if dateTime didn't change", function() {
58+
const Parent = createParent({defaultText: "Pick a date"});
59+
const input = TestUtils.findRenderedDOMComponentWithTag(Parent, "input");
60+
expect(input.getDOMNode().value).toBe("Pick a date");
61+
Parent.setState({});
62+
expect(input.getDOMNode().value).toBe("Pick a date");
4863
});
4964

65+
5066
});
67+
5168
});

0 commit comments

Comments
 (0)