-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import React from 'react'; | ||
import { configure, mount } from 'enzyme'; | ||
import { App } from "../index"; | ||
import Adapter from '@wojtekmaj/enzyme-adapter-react-17'; | ||
import { forceNumber } from "../../lib/utils"; | ||
|
||
configure({ adapter: new Adapter() }); | ||
|
||
describe('App', () => { | ||
let ReactWrapper; | ||
beforeEach(() => { | ||
ReactWrapper = mount(<App />); | ||
}); | ||
|
||
it('should render a div with a p tag and the RangeStepInput component', () => { | ||
expect(ReactWrapper.find('p').length).toEqual(1); | ||
expect(ReactWrapper.find(".input-group").length).toEqual(1); | ||
}); | ||
|
||
it('should change the state when changing RangeStepInput', () => { | ||
expect(ReactWrapper.find(".input-group-text").text()).toEqual('50'); | ||
ReactWrapper.find('input').simulate('change', { target: { value: 100 } }); | ||
expect(ReactWrapper.find(".input-group-text").text()).toEqual('100'); | ||
}); | ||
}); | ||
|
||
describe('forceNumber', () => { | ||
describe('input value is a number', () => { | ||
it('should return the provided value as a number', () => { | ||
expect(typeof forceNumber('10')).toEqual('number'); | ||
expect(forceNumber('10')).toBe(10); | ||
}); | ||
|
||
it('should return a negative value as a number', () => { | ||
expect(forceNumber('-10')).toBe(-10); | ||
}); | ||
|
||
it('should return a floating value as a number', () => { | ||
expect(forceNumber('0.7')).toBe(0.7); | ||
}); | ||
|
||
it('should return 0 value as a number', () => { | ||
expect(forceNumber('0')).toBe(0); | ||
}); | ||
|
||
it('should return a value starting with 0 as a number not starting with 0', () => { | ||
expect(forceNumber('007')).toBe(7); | ||
}); | ||
|
||
it('should return a value of type number as a number', () => { | ||
expect(forceNumber(20)).toBe(20); | ||
}); | ||
}); | ||
|
||
describe('input value is not a numbe', () => { | ||
it('should return 0 if input is a text', () => { | ||
expect(forceNumber('hello')).toBe(0); | ||
expect(forceNumber('5hello')).toBe(0); | ||
expect(forceNumber('5 hello')).toBe(0); | ||
expect(forceNumber('5 7')).toBe(0); | ||
expect(forceNumber('')).toBe(0); | ||
expect(forceNumber(undefined)).toBe(0); | ||
expect(forceNumber({})).toBe(0); | ||
expect(forceNumber(null)).toBe(0); | ||
expect(forceNumber([1, 2, 3])).toBe(0); | ||
expect(forceNumber(NaN)).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input contains a number and text', () => { | ||
expect(forceNumber('5hello')).toBe(0); | ||
expect(forceNumber('5 hello')).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input contains spaces', () => { | ||
expect(forceNumber('5 7')).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input is an empty string', () => { | ||
expect(forceNumber('')).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input is undefined or null', () => { | ||
expect(forceNumber(undefined)).toBe(0); | ||
expect(forceNumber(null)).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input is an empty object', () => { | ||
expect(forceNumber({})).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input is an array of numbers', () => { | ||
expect(forceNumber([1, 2, 3])).toBe(0); | ||
}); | ||
|
||
it('should return 0 if input is NaN', () => { | ||
expect(forceNumber(NaN)).toBe(0); | ||
}); | ||
}) | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,49 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import {RangeStepInput} from '..'; | ||
import { RangeStepInput } from '..'; | ||
|
||
const forceNumber = function(n) { | ||
n = Number(n); | ||
if (isNaN(n) || typeof n === 'undefined') { | ||
n = 0; | ||
} | ||
return n; | ||
const forceNumber = function (n) { | ||
n = Number(n); | ||
if (isNaN(n) || typeof n === 'undefined') { | ||
n = 0; | ||
} | ||
return n; | ||
}; | ||
|
||
class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
value: 50 | ||
}; | ||
} | ||
render() { | ||
return <div> | ||
<p>Here's an example RangeStepInput:</p> | ||
<div className="input-group mb-3"> | ||
<RangeStepInput | ||
min={0} max={100} | ||
value={this.state.value} step={1} | ||
onChange={this.onChange.bind(this)} | ||
/> | ||
<div className="input-group-append"> | ||
<span className="input-group-text ml-2"> | ||
{this.state.value} | ||
</span> | ||
</div> | ||
</div> | ||
</div>; | ||
export class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
value: 50 | ||
}; | ||
onChange(e) { | ||
const newVal = forceNumber(e.target.value); | ||
this.setState({value: newVal}); | ||
} | ||
}; | ||
} | ||
|
||
render() { | ||
return <div> | ||
<p>Here's an example RangeStepInput:</p> | ||
<div className="input-group mb-3"> | ||
<RangeStepInput | ||
min={0} | ||
max={100} | ||
value={this.state.value} | ||
step={1} | ||
onChange={this.onChange.bind(this)} | ||
/> | ||
<div className="input-group-append"> | ||
<span className="input-group-text ml-2"> | ||
{this.state.value} | ||
</span> | ||
</div> | ||
</div> | ||
</div>; | ||
}; | ||
|
||
onChange(e) { | ||
const newVal = forceNumber(e.target.value); | ||
this.setState({ value: newVal }); | ||
} | ||
} | ||
|
||
ReactDOM.render(<App />, document.getElementById('react-container')); | ||
if (document.getElementById('react-container')) { | ||
ReactDOM.render(<App />, document.getElementById('react-container')); | ||
} |