Skip to content

Commit

Permalink
Add contains logic to matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Oct 24, 2017
1 parent 99abdd9 commit 34dfba4
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
4 changes: 3 additions & 1 deletion src/matchers/toBeOneOf/predicate.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export default (a, b) => b.indexOf(a) > -1;
import { contains } from '../../utils';

export default (value, list) => contains(list, value);
24 changes: 20 additions & 4 deletions src/matchers/toBeOneOf/predicate.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import each from 'jest-each';
import predicate from './predicate';

describe('.toBeOneOf', () => {
it('passes when value is in given array', () => {
expect(predicate(1, [1, 2, 3])).toBe(true);
each([[1], [null], [undefined], [false], ['']]).it(
'returns true when primitive value: %s is in given array',
value => {
expect(predicate(value, [1, 2, 3, null, undefined, false, ''])).toBe(true);
}
);

each([[{ hello: 'world' }], [['foo']]]).it('returns true when nested value: %s is in given array', value => {
expect(predicate(value, [1, 2, { hello: 'world' }, ['foo']])).toBe(true);
});

it('fails when value is not in given array', () => {
expect(predicate(4, [1, 2, 3])).toBe(false);
each([
[0],
[null],
[undefined],
[false],
[''],
[{ hello: 'world' }],
[['foo']]
]).it('returns false when value: %s is not in given array', value => {
expect(predicate(value, [1, 2, 3])).toBe(false);
});
});
9 changes: 3 additions & 6 deletions src/matchers/toContainValue/predicate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { equals } from 'expect/build/jasmine_utils';
import { contains } from '../../utils';

export default (actual, value) => {
const result = Object.keys(actual)
.map(k => actual[k])
.find(v => equals(v, value));

return result !== undefined || value === undefined;
const objectValues = Object.keys(actual).map(k => actual[k]);
return contains(objectValues, value);
};
15 changes: 11 additions & 4 deletions src/matchers/toContainValue/predicate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('toContainValue Predicate', () => {
describe('returns true', () => {
each([['world'], [false], [undefined], [null], [''], [0]]).it(
'when given object contains primitive value: %s',
() => {
expect(predicate(shallow, 'world')).toBe(true);
value => {
expect(predicate(shallow, value)).toBe(true);
}
);

Expand All @@ -24,8 +24,15 @@ describe('toContainValue Predicate', () => {
});

describe('returns false', () => {
it('when given object does not contain primitive value', () => {
expect(predicate(shallow, 100)).toBe(false);
each([
['world'],
[false],
[undefined],
[null],
[''],
[0]
]).it('when given object does not contain primitive value: %s', value => {
expect(predicate({}, value)).toBe(false);
});

it('when given object does not contain object value', () => {
Expand Down
8 changes: 2 additions & 6 deletions src/matchers/toContainValues/predicate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { equals } from 'expect/build/jasmine_utils';
import { contains } from '../../utils';

export default (object, values) => {
const objectValues = Object.keys(object).map(k => object[k]);

return values.every(value => {
const result = objectValues.find(v => equals(v, value));
return result !== undefined || value === undefined;
});
return values.every(value => contains(objectValues, value));
};
2 changes: 1 addition & 1 deletion src/matchers/toContainValues/predicate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('toContainValue Predicate', () => {

describe('returns false', () => {
it('when given object does not contain all primitive value', () => {
expect(predicate(shallow, [100])).toBe(false);
expect(predicate(shallow, [false, undefined])).toBe(false);
});

it('when given object does not contain all values including objects', () => {
Expand Down

0 comments on commit 34dfba4

Please sign in to comment.