Skip to content
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
23 changes: 16 additions & 7 deletions src/matchers/toIncludeAllMembers/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,38 @@ Received:
<red>[1, 2, 3]</>"
`;

exports[`.toIncludeAllMembers fails when array values do not contain any of the members of the set 1`] = `
exports[`.toIncludeAllMembers fails when expected object is not an iterable 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllMembers(</><green>expected</><dim>)</>

Expected list to have all of the following members:
<green>2</>
Received:
<red>[1, 2, 3]</>"
`;

exports[`.toIncludeAllMembers fails when given object is not an iterable 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllMembers(</><green>expected</><dim>)</>

Expected list to have all of the following members:
<green>[1, 2, 3]</>
Received:
<red>[4, 5, 6]</>"
<red>2</>"
`;

exports[`.toIncludeAllMembers fails when expected object is not an array 1`] = `
exports[`.toIncludeAllMembers fails when iterable values do not contain any of the members of the set 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllMembers(</><green>expected</><dim>)</>

Expected list to have all of the following members:
<green>2</>
<green>[1, 2, 3]</>
Received:
<red>[1, 2, 3]</>"
<red>[4, 5, 6]</>"
`;

exports[`.toIncludeAllMembers fails when given object is not an array 1`] = `
exports[`.toIncludeAllMembers fails when iterable values do not contain any of the members of the set 2`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAllMembers(</><green>expected</><dim>)</>

Expected list to have all of the following members:
<green>[1, 2, 3]</>
Received:
<red>2</>"
<red>Set {4, 5, 6}</>"
`;
12 changes: 8 additions & 4 deletions src/matchers/toIncludeAllMembers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@ describe('.toIncludeAllMembers', () => {
const array1 = [1, 2, 3];
const array2 = [1, 2, 2];

test('passes when array values matches the members of the set', () => {
test('passes when iterable values matches the members of the set', () => {
expect(array1).toIncludeAllMembers([2, 1, 3]);
expect(array2).toIncludeAllMembers([2, 1]);
expect(new Set(array1)).toIncludeAllMembers([2, 1, 3]);
expect(new Set(array2)).toIncludeAllMembers([2, 1]);

expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeAllMembers([{ foo: 'bar' }]);
});

test('fails when array values do not contain any of the members of the set', () => {
test('fails when iterable values do not contain any of the members of the set', () => {
expect(() => expect([4, 5, 6]).toIncludeAllMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([4, 5, 6])).toIncludeAllMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
});

test('fails when given object is not an array', () => {
test('fails when given object is not an iterable', () => {
expect(() => expect(2).toIncludeAllMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
});

test('fails when expected object is not an array', () => {
test('fails when expected object is not an iterable', () => {
expect(() => expect(array1).toIncludeAllMembers(2)).toThrowErrorMatchingSnapshot();
});
});
Expand Down
9 changes: 6 additions & 3 deletions src/matchers/toIncludeAllMembers/predicate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { contains } from '../../utils';
import { asArray, contains } from '../../utils';

export default (array, set) => {
return Array.isArray(array) && Array.isArray(set) && set.every(val => contains(array, val));
export default (iterable, members) => {
const first = asArray(iterable);
const second = first && asArray(members);

return first != null && second != null && second.every(val => contains(first, val));
};
14 changes: 10 additions & 4 deletions src/matchers/toIncludeAllMembers/predicate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ describe('toIncludeAllMembers Predicate', () => {
const set2 = [1, 2, 3];

describe('returns true', () => {
test('when Array contains all the same members of given set', () => {
test('when iterable contains all the same members of given set', () => {
expect(predicate(array1, set1)).toBe(true);
expect(predicate(new Set(array1), set1)).toBe(true);
expect(predicate(new Set(array1), new Set(set1))).toBe(true);
});

test('when Array contains all of the same nested members of given set', () => {
test('when iterable contains all of the same nested members of given set', () => {
expect(predicate([{ hello: 'world' }, { foo: 'bar' }], [{ foo: 'bar' }])).toBe(true);
expect(predicate(new Set([{ hello: 'world' }, { foo: 'bar' }]), [{ foo: 'bar' }])).toBe(true);
});
});

describe('returns false', () => {
test('when Array does not contain any of the members of given set', () => {
test('when iterable does not contain any of the members of given set', () => {
expect(predicate(array2, set2)).toBe(false);
expect(predicate(new Set(array2), set2)).toBe(false);
expect(predicate(new Set(array2), new Set(set2))).toBe(false);
});

test('when Array contains does not contain all of the same nested members of given set', () => {
test('when iterable contains does not contain all of the same nested members of given set', () => {
expect(predicate([{ hello: 'world' }, { foo: 'bar' }], [{ foo: 'qux' }])).toBe(false);
expect(predicate(new Set([{ hello: 'world' }, { foo: 'bar' }]), new Set([{ foo: 'qux' }]))).toBe(false);
});
});
});
40 changes: 38 additions & 2 deletions src/matchers/toIncludeAnyMembers/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Received:
<red>[[{\\"hello\\": \\"world\\"}]]</>"
`;

exports[`.not.toIncludeAnyMembers fails when given array contains array value 2`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to not include any of the following members:
<green>[[{\\"hello\\": \\"world\\"}], 7]</>
Received:
<red>Set {[{\\"hello\\": \\"world\\"}]}</>"
`;

exports[`.not.toIncludeAnyMembers fails when given array contains object value 1`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAnyMembers(</><green>expected</><dim>)</>

Expand All @@ -18,6 +27,15 @@ Received:
<red>[{\\"foo\\": \\"bar\\"}]</>"
`;

exports[`.not.toIncludeAnyMembers fails when given array contains object value 2`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to not include any of the following members:
<green>[{\\"foo\\": \\"bar\\"}]</>
Received:
<red>Set {{\\"foo\\": \\"bar\\"}}</>"
`;

exports[`.not.toIncludeAnyMembers fails when given object contains primitive value 1`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAnyMembers(</><green>expected</><dim>)</>

Expand All @@ -27,6 +45,15 @@ Received:
<red>[7]</>"
`;

exports[`.not.toIncludeAnyMembers fails when given object contains primitive value 2`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to not include any of the following members:
<green>[7, 8]</>
Received:
<red>Set {7}</>"
`;

exports[`.toIncludeAnyMembers fails when expected object does not contain array value 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAnyMembers(</><green>expected</><dim>)</>

Expand All @@ -36,7 +63,7 @@ Received:
<red>[1, 2, 3]</>"
`;

exports[`.toIncludeAnyMembers fails when given array does not contain any members 1`] = `
exports[`.toIncludeAnyMembers fails when given iterables does not contain any members 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to include any of the following members:
Expand All @@ -45,7 +72,16 @@ Received:
<red>[4, 5, 6]</>"
`;

exports[`.toIncludeAnyMembers fails when given object is not an array 1`] = `
exports[`.toIncludeAnyMembers fails when given iterables does not contain any members 2`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to include any of the following members:
<green>[1, 2, 3]</>
Received:
<red>Set {4, 5, 6}</>"
`;

exports[`.toIncludeAnyMembers fails when given object is not an iterable 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeAnyMembers(</><green>expected</><dim>)</>

Expected list to include any of the following members:
Expand Down
17 changes: 14 additions & 3 deletions src/matchers/toIncludeAnyMembers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ describe('.toIncludeAnyMembers', () => {
const shallow = { hello: 'world' };
const deep = { message: shallow };

test('passes when given array is a subset', () => {
test('passes when given iterable is a subset', () => {
expect([2, 3]).toIncludeAnyMembers([1, 2, 3]);
expect(new Set([2, 3])).toIncludeAnyMembers([1, 2, 3]);
});

test('passes when given array contains some of the members', () => {
expect([1, 2, 3]).toIncludeAnyMembers([2, 3, 4]);
expect(new Set([1, 2, 3])).toIncludeAnyMembers([2, 3, 4]);
});

test('passes when given array contains object value', () => {
expect([shallow]).toIncludeAnyMembers([shallow, deep]);
expect(new Set([shallow])).toIncludeAnyMembers([shallow, deep]);
});

test('fails when given array does not contain any members ', () => {
test('fails when given iterables does not contain any members ', () => {
expect(() => expect([4, 5, 6]).toIncludeAnyMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([4, 5, 6])).toIncludeAnyMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
});

test('fails when given object is not an array', () => {
test('fails when given object is not an iterable', () => {
expect(() => expect(7).toIncludeAnyMembers([1, 2, 3])).toThrowErrorMatchingSnapshot();
});

Expand All @@ -36,10 +40,12 @@ describe('.not.toIncludeAnyMembers', () => {

test('passes when given array does not contain primitive value', () => {
expect(['hola', 'mundo']).not.toIncludeAnyMembers(['hello', 'world']);
expect(new Set(['hola', 'mundo'])).not.toIncludeAnyMembers(['hello', 'world']);
});

test('passes when given array does not contain object value', () => {
expect([{ hello: 'world' }]).not.toIncludeAnyMembers([{ world: 'hello' }]);
expect(new Set([{ hello: 'world' }])).not.toIncludeAnyMembers([{ world: 'hello' }]);
});

test('passes when given object is not array', () => {
Expand All @@ -48,13 +54,18 @@ describe('.not.toIncludeAnyMembers', () => {

test('fails when given object contains primitive value', () => {
expect(() => expect([7]).not.toIncludeAnyMembers([7, 8])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([7])).not.toIncludeAnyMembers([7, 8])).toThrowErrorMatchingSnapshot();
});

test('fails when given array contains object value', () => {
expect(() => expect([{ foo: 'bar' }]).not.toIncludeAnyMembers([{ foo: 'bar' }])).toThrowErrorMatchingSnapshot();
expect(() =>
expect(new Set([{ foo: 'bar' }])).not.toIncludeAnyMembers([{ foo: 'bar' }]),
).toThrowErrorMatchingSnapshot();
});

test('fails when given array contains array value', () => {
expect(() => expect([[shallow]]).not.toIncludeAnyMembers([[shallow], 7])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([[shallow]])).not.toIncludeAnyMembers([[shallow], 7])).toThrowErrorMatchingSnapshot();
});
});
9 changes: 6 additions & 3 deletions src/matchers/toIncludeAnyMembers/predicate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { contains } from '../../utils';
import { asArray, contains } from '../../utils';

export default (array, members) => {
return Array.isArray(array) && Array.isArray(members) && members.some(member => contains(array, member));
export default (iterable, members) => {
const first = asArray(iterable);
const second = first && asArray(members);

return first != null && second != null && second.some(val => contains(first, val));
};
8 changes: 8 additions & 0 deletions src/matchers/toIncludeAnyMembers/predicate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,37 @@ describe('toIncludeAnyMembers Predicate', () => {
'when given array contains primitive value: %s',
given => {
expect(predicate([given], array)).toBe(true);
expect(predicate(new Set([given]), array)).toBe(true);
expect(predicate(new Set([given]), new Set(array))).toBe(true);
},
);

test('when given array contains object value', () => {
expect(predicate([shallow, 7], [shallow])).toBe(true);
expect(predicate(new Set([shallow, 7]), [shallow])).toBe(true);
});

test('when given object contains array value', () => {
expect(predicate([[shallow]], [[shallow], 7])).toBe(true);
expect(predicate(new Set([[shallow]]), [[shallow], 7])).toBe(true);
});
});

describe('returns false', () => {
test('when given array does not contain primitive value', () => {
expect(predicate([3, 4, 5], [1])).toBe(false);
expect(predicate(new Set([3, 4, 5]), [1])).toBe(false);
expect(predicate(new Set([3, 4, 5]), new Set([1]))).toBe(false);
});

test('when given array does not contain object value', () => {
expect(predicate([3], [{ foo: 'bar' }])).toBe(false);
expect(predicate(new Set([3]), [{ foo: 'bar' }])).toBe(false);
});

test('when given object does not contain array value', () => {
expect(predicate([7], [[7]])).toBe(false);
expect(predicate(new Set([7]), [[7]])).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,29 @@ Received:
<red>[1]</>"
`;

exports[`.toIncludeSameMembers fails when the arrays are not equal in length 1`] = `
exports[`.not.toIncludeSameMembers fails when array contents match 2`] = `
"<dim>expect(</><red>received</><dim>).not.toIncludeSameMembers(</><green>expected</><dim>)</>

Expected list to not exactly match the members of:
<green>[1]</>
Received:
<red>Set {1}</>"
`;

exports[`.toIncludeSameMembers fails when the iterables are not equal in length 1`] = `
"<dim>expect(</><red>received</><dim>).toIncludeSameMembers(</><green>expected</><dim>)</>

Expected list to have the following members and no more:
<green>[1]</>
Received:
<red>[1, 2]</>"
`;

exports[`.toIncludeSameMembers fails when the iterables are not equal in length 2`] = `
"<dim>expect(</><red>received</><dim>).toIncludeSameMembers(</><green>expected</><dim>)</>

Expected list to have the following members and no more:
<green>[1, 2, 2]</>
Received:
<red>Set {1, 2}</>"
`;
18 changes: 15 additions & 3 deletions src/matchers/toIncludeSameMembers/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,53 @@ expect.extend(matcher);
describe('.toIncludeSameMembers', () => {
test('passes when arrays are empty', () => {
expect([]).toIncludeSameMembers([]);
expect(new Set()).toIncludeSameMembers([]);
});

test('passes when arrays match', () => {
expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3]);
expect(new Set([1, 2, 3])).toIncludeSameMembers([1, 2, 3]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ foo: 'bar' }, { baz: 'qux' }]);
expect(new Set([{ foo: 'bar' }, { baz: 'qux' }])).toIncludeSameMembers([{ foo: 'bar' }, { baz: 'qux' }]);
});

test('passes when arrays match in a different order', () => {
expect([1, 2, 3]).toIncludeSameMembers([3, 1, 2]);
expect(new Set([1, 2, 3])).toIncludeSameMembers([3, 1, 2]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).toIncludeSameMembers([{ baz: 'qux' }, { foo: 'bar' }]);
});

test('fails when the arrays are not equal in length', () => {
test('fails when the iterables are not equal in length', () => {
expect(() => expect([1, 2]).toIncludeSameMembers([1])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([1, 2])).toIncludeSameMembers([1, 2, 2])).toThrowErrorMatchingSnapshot();
});
});

describe('.not.toIncludeSameMembers', () => {
test('fails when array contents match', () => {
expect(() => expect([1]).not.toIncludeSameMembers([1])).toThrowErrorMatchingSnapshot();
expect(() => expect(new Set([1])).not.toIncludeSameMembers([1])).toThrowErrorMatchingSnapshot();
});

test('passes when the arrays are different in length', () => {
expect([1, 2, 3]).not.toIncludeSameMembers([1, 2]);
expect(new Set([1, 2])).not.toIncludeSameMembers([1, 2, 3]);
expect([{ foo: 'bar' }, { baz: 'qux' }]).not.toIncludeSameMembers([{ hello: 'world' }]);
});

test('passes when given object is not an array', () => {
test('passes when given object is not an iterable', () => {
expect(4).not.toIncludeSameMembers([4, 5, 6]);
});

test('passes when arrays do not match', () => {
test('passes when iterables do not match', () => {
expect([]).not.toIncludeSameMembers([1]);
expect([1]).not.toIncludeSameMembers([1, 1]);
expect([1, 2]).not.toIncludeSameMembers([1, 2, 2]);
expect([1, 2, 3]).not.toIncludeSameMembers([2, 3, 4]);

expect(new Set([])).not.toIncludeSameMembers([1]);
expect(new Set([1])).not.toIncludeSameMembers([1, 1]);
expect(new Set([1, 2])).not.toIncludeSameMembers([1, 2, 2]);
expect(new Set([1, 2, 3])).not.toIncludeSameMembers([2, 3, 4]);
});
});
Loading