forked from jest-community/jest-extended
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
toIncludeAllPartialMembers
(jest-community#210)
- Loading branch information
Showing
8 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/matchers/toIncludeAllPartialMembers/__snapshots__/index.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`.not.toIncludeAllPartialMembers fails when array values matches the members of the set 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).not.toIncludeAllPartialMembers(</><green>expected</><dim>)</> | ||
Expected list to not have all of the following partial members: | ||
<green>[{\\"foo\\": \\"bar\\"}]</> | ||
Received: | ||
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>" | ||
`; | ||
exports[`.toIncludeAllPartialMembers fails when array values do not contain any of the members of the set 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</> | ||
Expected list to have all of the following partial members: | ||
<green>[{\\"foo\\": \\"qux\\"}]</> | ||
Received: | ||
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>" | ||
`; | ||
exports[`.toIncludeAllPartialMembers fails when expected object is not an array 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</> | ||
Expected list to have all of the following partial members: | ||
<green>1</> | ||
Received: | ||
<red>[{\\"hello\\": \\"world\\"}, {\\"baz\\": \\"qux\\", \\"foo\\": \\"bar\\"}]</>" | ||
`; | ||
exports[`.toIncludeAllPartialMembers fails when given object is not an array 1`] = ` | ||
"<dim>expect(</><red>received</><dim>).toIncludeAllPartialMembers(</><green>expected</><dim>)</> | ||
Expected list to have all of the following partial members: | ||
<green>[{\\"foo\\": \\"bar\\"}]</> | ||
Received: | ||
<red>1</>" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = (actual, expected) => () => | ||
matcherHint('.not.toIncludeAllPartialMembers') + | ||
'\n\n' + | ||
'Expected list to not have all of the following partial members:\n' + | ||
` ${printExpected(expected)}\n` + | ||
'Received:\n' + | ||
` ${printReceived(actual)}`; | ||
|
||
const failMessage = (actual, expected) => () => | ||
matcherHint('.toIncludeAllPartialMembers') + | ||
'\n\n' + | ||
'Expected list to have all of the following partial members:\n' + | ||
` ${printExpected(expected)}\n` + | ||
'Received:\n' + | ||
` ${printReceived(actual)}`; | ||
|
||
export default { | ||
toIncludeAllPartialMembers: (actual, expected) => { | ||
const pass = predicate(actual, expected); | ||
if (pass) { | ||
return { pass: true, message: passMessage(actual, expected) }; | ||
} | ||
|
||
return { pass: false, message: failMessage(actual, expected) }; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import matcher from './'; | ||
|
||
expect.extend(matcher); | ||
|
||
describe('.toIncludeAllPartialMembers', () => { | ||
test('passes when array values matches the partial members of the set', () => { | ||
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'bar' }]); | ||
}); | ||
|
||
test('fails when array values do not contain any of the members of the set', () => { | ||
expect(() => | ||
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers([{ foo: 'qux' }]), | ||
).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('fails when given object is not an array', () => { | ||
expect(() => expect(1).toIncludeAllPartialMembers([{ foo: 'bar' }])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
test('fails when expected object is not an array', () => { | ||
expect(() => | ||
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).toIncludeAllPartialMembers(1), | ||
).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
|
||
describe('.not.toIncludeAllPartialMembers', () => { | ||
test('passes when array values does not contain any members of the set', () => { | ||
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).not.toIncludeAllPartialMembers([{ foo: 'qux' }]); | ||
}); | ||
|
||
test('passes when given object is not an array', () => { | ||
expect(1).not.toIncludeAllPartialMembers([{ foo: 'bar' }]); | ||
}); | ||
|
||
test('fails when array values matches the members of the set', () => { | ||
expect(() => | ||
expect([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }]).not.toIncludeAllPartialMembers([{ foo: 'bar' }]), | ||
).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import toContainAnyEntries from '../toContainAnyEntries/predicate'; | ||
|
||
export default (array, set) => | ||
Array.isArray(array) && | ||
Array.isArray(set) && | ||
set.every(partial => array.some(value => toContainAnyEntries(value, Object.entries(partial)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import predicate from './predicate'; | ||
|
||
describe('toIncludeAllPartialMembers Predicate', () => { | ||
describe('returns true', () => { | ||
test('when Array contains all of the same nested partial members of given set', () => { | ||
expect(predicate([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'bar' }])).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('returns false', () => { | ||
test('when Array contains does not contain all of the same nested partial members of given set', () => { | ||
expect(predicate([{ hello: 'world' }, { foo: 'bar', baz: 'qux' }], [{ foo: 'qux' }])).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters