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

Created toBeWithinPercent and toContainKeysWithinPercent #301

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ If you've come here to help contribute - Thanks! Take a look at the [contributin
- [.toBeEven()](#tobeeven)
- [.toBeOdd()](#tobeodd)
- [.toBeWithin(start, end)](#tobewithinstart-end)
- [.toBeWithinPercent(mid, percent)](#tobewithinpercentmid-percent)
- [Object](#object)
- [.toBeObject()](#tobeobject)
- [.toContainKey(key)](#tocontainkeykey)
Expand Down Expand Up @@ -599,6 +600,18 @@ test('passes when number is within given bounds', () => {
});
```

#### .toBeWithinPercent(mid, percent)

Use `.toBeWithinPercent` when checking if a number is within x percent of a target number.

```js
test('passes when number is within x percent of target', () => {
expect(55).toBeWithinPercent(50, 10);
expect(20).toBeWithinPercent(10, 100);
expect(100).not.toBeWithinPercent(10, 5);
});
```

### Object

#### .toBeObject()
Expand Down Expand Up @@ -640,6 +653,25 @@ test('passes when object contains all keys', () => {
});
```

#### .toContainKeysWithinPercent([keyObjects])

Use `.toContainKeysWithinPercent` when checking if an object has all of the provided keys and that the value of these keys is within x percent of a target value.

```js
test('passes when object contains all keys', () => {
const data1 = { a: 55, b: 1 };
const data2 = { a: 45, b: 1 }
const data3 = { a: 20, b: 2 }
const data4 = { a: 50 }
const keys = [{key: "a", target: 50, percent: 10}, {key: "b", target: 1, percent: 0}]

expect(data1).toContainKeysWithinPercent(keys);
expect(data2).toContainKeysWithinPercent(keys);
expect(data3).not.toContainKeysWithinPercent(keys);
expect(data4).not.toContainKeysWithinPercent(keys);
});
```

#### .toContainAllKeys([keys])

Use `.toContainAllKeys` when checking if an object only contains all of the provided keys.
Expand Down
37 changes: 37 additions & 0 deletions src/matchers/toBeWithinPercent/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toBeWithinPercent fails when given number is within x percent of the target number 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeWithinPercent(</><green>expected</><dim>)</>

Expected number to not be within percent of target:
target: <green>20</> percent: <green>100</>%
Received:
<red>55</>"
`;

exports[`.not.toBeWithinPercent fails when given number is within x percent of the target number 1`] = `
"<dim>expect(</><red>received</><dim>).not.toBeWithinPercent(</><green>expected</><dim>)</>

Expected number to not be within percent of target:
target: <green>20</> percent: <green>100</>%
Received:
<red>25</>"
`;

exports[`.toBeWithinPercent fails when given number is not within x percent of the target number 1`] = `
"<dim>expect(</><red>received</><dim>).toBeWithinPercent(</><green>expected</><dim>)</>

Expected number to be within percent of target:
target: <green>20</> percent: <green>5</>%
Received:
<red>100</>"
`;

exports[`.toBeWithinPercent fails when given number is not within x percent of the target number 1`] = `
"<dim>expect(</><red>received</><dim>).toBeWithinPercent(</><green>expected</><dim>)</>

Expected number to be within percent of target:
target: <green>50</> percent: <green>10</>%
Received:
<red>56</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toBeWithinPercent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (number, target, percent) => () =>
matcherHint('.not.toBeWithinPercent') +
'\n\n' +
'Expected number to not be within percent of target:\n' +
` target: ${printExpected(target)} percent: ${printExpected(percent)}%\n` +
'Received:\n' +
` ${printReceived(number)}`;

const failMessage = (number, target, percent) => () =>
matcherHint('.toBeWithinPercent') +
'\n\n' +
'Expected number to be within percent of target:\n' +
` target: ${printExpected(target)} percent: ${printExpected(percent)}%\n` +
'Received:\n' +
` ${printReceived(number)}`;

export default {
toBeWithinPercent: (number, target, percent) => {
const pass = predicate(number, target, percent);
if (pass) {
return { pass: true, message: passMessage(number, target, percent) };
}

return { pass: false, message: failMessage(number, target, percent) };
}
};
23 changes: 23 additions & 0 deletions src/matchers/toBeWithinPercent/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import matcher from './';

expect.extend(matcher);

describe('.toBeWithinPercent', () => {
test('passes when given number is within x percent of the target number', () => {
expect(55).toBeWithinPercent(50, 10);
});

test('fails when given number is not within x percent of the target number', () => {
expect(() => expect(56).toBeWithinPercent(50, 10)).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toBeWithinPercent', () => {
test('passes when given number is not within x percent of the target number', () => {
expect(100).not.toBeWithinPercent(20, 5);
});

test('fails when given number is within x percent of the target number', () => {
expect(() => expect(25).not.toBeWithinPercent(20, 100)).toThrowErrorMatchingSnapshot();
});
});
2 changes: 2 additions & 0 deletions src/matchers/toBeWithinPercent/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default (number, target, percent) =>
number <= target * (1 + percent / 100) && number >= target * (1 - percent / 100);
11 changes: 11 additions & 0 deletions src/matchers/toBeWithinPercent/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import predicate from './predicate';

describe('toBeWithinPercent Predicate', () => {
test('returns true when given number is within percent of target', () => {
expect(predicate(55, 50, 10)).toBe(true);
});

test('returns false when given number is not within percent of target', () => {
expect(predicate(60, 50, 10)).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`.not.toContainKeys fails when object contains all keys and they are within x percent of target 1`] = `
"<dim>expect(</><red>received</><dim>).not.toContainKeysWithinPercent(</><green>expected</><dim>)</>

Expected object to not contain all keys or the keys' values to not be within x percent of target:
<green>[{\\"key\\": \\"a\\", \\"percent\\": 10, \\"target\\": 50}, {\\"key\\": \\"b\\", \\"percent\\": 0, \\"target\\": 5}, {\\"key\\": \\"c\\", \\"percent\\": 100, \\"target\\": 1}]</>
Received:
<red>{\\"a\\": 55, \\"b\\": 5, \\"c\\": 1}</>"
`;

exports[`.toContainKeys fails when object does not contain all keys or they are not within x percent of target 1`] = `
"<dim>expect(</><red>received</><dim>).toContainKeysWithinPercent(</><green>expected</><dim>)</>

Expected object to contain all keys and the keys' values to be within x percent of target:
<green>[{\\"key\\": \\"a\\", \\"percent\\": 10, \\"target\\": 50}, {\\"key\\": \\"b\\", \\"percent\\": 0, \\"target\\": 5}, {\\"key\\": \\"c\\", \\"percent\\": 100, \\"target\\": 1}]</>
Received:
<red>{\\"a\\": 56, \\"b\\": 5, \\"c\\": 1}</>"
`;
30 changes: 30 additions & 0 deletions src/matchers/toContainKeysWithinPercent/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';

import predicate from './predicate';

const passMessage = (valueObj, keysArray) => () =>
matcherHint('.not.toContainKeysWithinPercent') +
'\n\n' +
"Expected object to not contain all keys or the keys' values to not be within x percent of target:\n" +
` ${printExpected(keysArray)}\n` +
'Received:\n' +
` ${printReceived(valueObj)}`;

const failMessage = (valueObj, keysArray) => () =>
matcherHint('.toContainKeysWithinPercent') +
'\n\n' +
"Expected object to contain all keys and the keys' values to be within x percent of target:\n" +
` ${printExpected(keysArray)}\n` +
'Received:\n' +
` ${printReceived(valueObj)}`;

export default {
toContainKeysWithinPercent: (valueObj, keysArray) => {
const pass = predicate(valueObj, keysArray);
if (pass) {
return { pass: true, message: passMessage(valueObj, keysArray) };
}

return { pass: false, message: failMessage(valueObj, keysArray) };
}
};
43 changes: 43 additions & 0 deletions src/matchers/toContainKeysWithinPercent/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import matcher from './';

expect.extend(matcher);

const data1 = { a: 55, b: 5, c: 1 };
const data2 = { a: 56, b: 5, c: 1 };

const keys = [
{
key: 'a',
target: 50,
percent: 10
},
{
key: 'b',
target: 5,
percent: 0
},
{
key: 'c',
target: 1,
percent: 100
}
];
describe('.toContainKeys', () => {
test('passes when object contains all keys and they are within x percent of target', () => {
expect(data1).toContainKeysWithinPercent(keys);
});

test('fails when object does not contain all keys or they are not within x percent of target', () => {
expect(() => expect(data2).toContainKeysWithinPercent(keys)).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toContainKeys', () => {
test('passes when object does not contain all keys or they are not within x percent of target', () => {
expect(data2).not.toContainKeysWithinPercent(keys);
});

test('fails when object contains all keys and they are within x percent of target', () => {
expect(() => expect(data1).not.toContainKeysWithinPercent(keys)).toThrowErrorMatchingSnapshot();
});
});
8 changes: 8 additions & 0 deletions src/matchers/toContainKeysWithinPercent/predicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default (valueObj, keys) =>
keys.every(keyObj => {
return (
valueObj[keyObj.key] &&
valueObj[keyObj.key] <= keyObj.target * (1 + keyObj.percent / 100) &&
valueObj[keyObj.key] >= keyObj.target * (1 - keyObj.percent / 100)
);
});
32 changes: 32 additions & 0 deletions src/matchers/toContainKeysWithinPercent/predicate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import predicate from './predicate';

const data1 = { a: 55, b: 5, c: 1 };
const data2 = { a: 56, b: 5, c: 1 };

const keys = [
{
key: 'a',
target: 50,
percent: 10
},
{
key: 'b',
target: 5,
percent: 0
},
{
key: 'c',
target: 1,
percent: 100
}
];

describe('.toContainKeysWithinPercent', () => {
test('passes when object contains all keys and they are within x percent of target', () => {
expect(predicate(data1, keys)).toBe(true);
});

test('fails when object does not contain all keys or they are not within x percent of target', () => {
expect(predicate(data2, keys)).toBe(false);
});
});
15 changes: 15 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ declare namespace jest {
*/
toBeWithin(start: number, end: number): R;

/**
* Use `.toBeWithinPercent` when checking if a number is within x percent of a target number.
*
* @param {Number} target
* @param {Number} percent
*/
toBeWithinPercent(target: number, percent: number): R;

/**
* Use `.toBeObject` when checking if a value is an `Object`.
*/
Expand Down Expand Up @@ -561,6 +569,13 @@ declare namespace jest {
*/
toContainKeys(keys: string[]): any;

/**
* Use `.toContainKeysWithinPercent` when checking if an object has all of the provided keys and the keys' values are within x percent of a target value.
*
* @param {Array.<Object>} keyObjects
*/
toContainKeysWithinPercent(keyObjects: object[]): any;

/**
* Use `.toContainAllKeys` when checking if an object only contains all of the provided keys.
*
Expand Down