Skip to content

Commit

Permalink
react tutorial: fix behaviour of 0 and .
Browse files Browse the repository at this point in the history
  • Loading branch information
evelyn.graumann committed Jan 23, 2020
1 parent 106aa43 commit 2ff9cfa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/Calculator/Calculator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class Calculator extends Component {
// set displayValue to '0' if displayValue is empty string
if (displayValue === '') displayValue = '0';
} else {
// replace displayValue with value if displayValue equal to '0'
// replace displayValue with value if displayValue equal to '0' and it does not include the dot
// else concatenate displayValue and value
displayValue === '0' ? displayValue = value : displayValue += value;
displayValue === '0' && value!== '.' ? displayValue = value : displayValue += value;
}

this.setState({ displayValue });
Expand Down
7 changes: 6 additions & 1 deletion src/components/Calculator/Calculator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ describe('updateDisplay', () => {
expect(wrapper.state('displayValue')).toEqual('0');
});

it('prevents showing only . when 0 as initial value', () => {
wrapper.instance().updateDisplay('0');
wrapper.instance().updateDisplay('.');
expect(wrapper.state('displayValue')).toEqual('0.5');
});

it('removes last char of displayValue', () => {
wrapper.instance().updateDisplay('5');
wrapper.instance().updateDisplay('0');
Expand Down Expand Up @@ -161,7 +167,6 @@ describe('callOperator', () => {
expect(wrapper.state('displayValue')).toEqual('6.05');
});


it('updates displayValue to the sum of storedValue and displayValue', () => {
wrapper.setState({ storedValue: '3' });
wrapper.setState({ displayValue: '2' });
Expand Down

0 comments on commit 2ff9cfa

Please sign in to comment.