-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty-based.test.js
50 lines (39 loc) · 1.31 KB
/
property-based.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import React from 'react';
import {shallow} from 'enzyme';
import assert from 'assert';
import { sample, check, gen, property } from 'testcheck'; // property based testing library
// Component under test
class Example extends React.Component {
// method that will 'regularize' the input. In this case we just square it
// For this example the important property of 'regularize' is that it makes all input positive
regularize(input){
const x = typeof input == "number" && !isNaN(input) ? input : 0.0 ;
return x*x;
}
render() {
return (
<div >
{this.regularize(this.props.input)}
</div>
);
}
}
describe('property based component test', () => {
it('input must be greater than 0', () => {
// define property
// gen.number randomly generates values from -inf to inf and NaN
const greaterThanZero = property(gen.number, n => {
//render component with the generated input
const wrapper = shallow(<Example input={n} />);
//define the property as a predicate
return parseFloat(wrapper.find('div').first().text()) >= 0;
});
// run property test with 1000 samples
const result = check(greaterThanZero, {numTests: 1000});
console.log(result);
if(!result.result){
console.log(result);
}
assert(result.result);
});
});