Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incremental PR towards #347 #462

Merged
merged 4 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/number_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class NumberFormat extends React.Component {

const lastValueWithNewFormat = this.formatNumString(lastNumStr);

const formattedValue = props.value === undefined ? lastValueWithNewFormat : this.formatValueProp();
const formattedValue = props.value === undefined || props.value === null ? lastValueWithNewFormat : this.formatValueProp();
const numAsString = this.removeFormatting(formattedValue);

const floatValue = parseFloat(numAsString);
Expand Down
2 changes: 1 addition & 1 deletion test/library/format_as_text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('NumberFormat as text', () => {
expect(wrapper.find('span').text()).toEqual('4111.36');
});

it('it should add zeros if fixedDecimalScale is provided', () => {
it('should add zeros if fixedDecimalScale is provided', () => {
const wrapper = shallow(<NumberFormat value="4111.11" displayType={'text'} decimalScale={4} fixedDecimalScale={true}/>);
expect(wrapper.find('span').text()).toEqual('4111.1100');

Expand Down
27 changes: 27 additions & 0 deletions test/library/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ describe('NumberFormat as input', () => {
expect(wrapper.find('input').instance().value).toEqual('$2,456,981');
});

it('should load the default value when initial value is null', () => {
const wrapper = mount(<NumberFormat value={null} defaultValue={89} />);
expect(wrapper.state().value).toEqual('89');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never check for state value. As it's implementation details. Instead we should test for the end side-effect.

expect(wrapper.find('input').instance().value).toEqual('89');

});

it('should load the prevous valid value if the state is changed to null', () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo on the statement.

  should load the previous valid value

class WrapperComponent extends React.Component {
constructor() {
super ();
this.state = {
testState: 90,
};
}
render() {
return (<NumberFormat value={this.state.testState} />)
}
}

const wrapper = mount(<WrapperComponent />);
const input = wrapper.find('input');
const domInput = input.instance();

expect(domInput.value).toEqual('90');
wrapper.setState({testState: null});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec could be simplified you just mount <NumberFormat value={this.state.testState} /> and then change the props through setProps.

Also FYI. Try avoiding interacting with states in specs. You shouldn't depend much on implementation detail.

expect(domInput.value).toEqual('90');
});

it('should use defaultValue as initial value', () => {
const wrapper = mount(<NumberFormat defaultValue={2456981} thousandSeparator={true} prefix={'$'} />);
expect(wrapper.state().value).toEqual('$2,456,981');
Expand Down