jest-mock-random
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -25,2 +25,10 @@ const mockRandomWith = require('../index'); | ||
}); | ||
describe('Corner case', () => { | ||
mockRandomWith([]); | ||
it('throw Error in case we pass an empty array', () => { | ||
const actual = () => Math.random(); | ||
expect(actual).toThrow(TypeError); | ||
}); | ||
}); | ||
describe('Singular value', () => { | ||
@@ -27,0 +35,0 @@ mockRandomWith(0.1); |
22
index.js
@@ -6,5 +6,3 @@ // Warning in case the passed value is not a decimal | ||
const warnBrokenContract = (values) => { | ||
if (!Array.isArray(values)) { | ||
if (!isDecimal(values)) { showWarning(); } | ||
} else if (!values.map(parseFloat).every(isDecimal)) { | ||
if (!values.map(parseFloat).every(isDecimal)) { | ||
showWarning(); | ||
@@ -22,20 +20,20 @@ } | ||
const randomMock = (returnValues) => { | ||
let arrayOfValues = returnValues; | ||
if (!Array.isArray(returnValues)) { | ||
const number = parseFloat(returnValues); | ||
return () => { | ||
warnBrokenContract(number); | ||
return number; | ||
}; | ||
arrayOfValues = [returnValues]; | ||
} | ||
let index = 0; | ||
return () => { | ||
warnBrokenContract(returnValues); | ||
if (index >= returnValues.length) { | ||
if (arrayOfValues.length === 0) { | ||
throw new TypeError('The value list must contain some value'); | ||
} | ||
warnBrokenContract(arrayOfValues); | ||
if (index >= arrayOfValues.length) { | ||
index = 0; | ||
} | ||
return returnValues[index++]; | ||
return arrayOfValues[index++]; | ||
}; | ||
}; | ||
// Through a copy of the global Math object we mock the random method | ||
@@ -42,0 +40,0 @@ const createMathRandomMock = (values) => { |
{ | ||
"name": "jest-mock-random", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Math.random() as a deterministic jest mock function", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# jest-mock-random | ||
Math.random() as deterministic jest mock function | ||
`Math.random()` as deterministic [Jest mock function](https://facebook.github.io/jest/docs/mock-functions.html). | ||
## Install | ||
```bash | ||
~ npm install --save-dev jest-mock-random | ||
// or | ||
~ yarn add --dev jest-mock-random | ||
``` | ||
## Usage | ||
jest-mock-random is used as an additional beforeEach for your sequence of test. | ||
```javascript | ||
import mockRandomWith from 'jest-mock-random'; | ||
describe('Test with random usage', () => { | ||
mockRandomWith([0.1, 0.2, 0.3, 0.6]); | ||
it('assigns random the values that we want to mock in order', () => { | ||
const actual = [Math.random(), Math.random(), Math.random(), Math.random()]; // [0.1, 0.2, 0.3, 0.6] | ||
expect(actual).toEqual([0.1, 0.2, 0.3, 0.6]); | ||
}); | ||
}); | ||
``` | ||
mockRandomWith accept a single value that it will give back every time Math.random is called or an array of values that the mock will use as a circular array. | ||
The values could be or a decimal number or a string that represents a decimal. | ||
WARNING: if a no decimal value is passed it will print some warnings during the test. | ||
```javascript | ||
mockRandomWith([0.1, 0.2]); // OK | ||
mockRandomWith([0.1, '0.2']); // OK | ||
mockRandomWith(['0.2', '0.2']); // OK | ||
mockRandomWith(0.2); // OK | ||
mockRandomWith('0.2'); // OK | ||
mockRandomWith('a'); // WARNING | ||
mockRandomWith(13); // WARNING | ||
mockRandomWith({}); // WARNING | ||
mockRandomWith([]); // WARNING | ||
mockRandomWith(['a', 1]); // WARNING |
135563
139
47