Skip to content

Commit

Permalink
fix: property predicate on array. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
unional authored Jan 10, 2018
1 parent c0e5327 commit 19e4218
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/createSatisfier.exec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,20 @@ test('failing array in hash', t => {
t.is(actual.length, 1)
assertExec(t, actual[0], ['a', '[2]'], 'a', 'b')
})

test('apply expectation function to each element in array', t => {
const satisfier = createSatisfier(e => e.login)
t.is(satisfier.exec([{ login: 'a' }]), undefined)
t.not(satisfier.exec([{ foo: 'a' }]), undefined)
})

test('apply property predicate to each element in array', t => {
const satisfier = createSatisfier({ data: e => e && e.login });

t.is(satisfier.exec({ data: { login: 'a' } }), undefined)
t.is(satisfier.exec({ data: [{ login: 'a' }] }), undefined)
const actual = satisfier.exec({ data: [{ login: 'a' }, {}] })!
t.true(createSatisfier({ path: ['data', '[1]'], actual: {} }).test(actual[0]))
t.not(satisfier.exec([{ data: { foo: 'a' } }]), undefined)
t.not(satisfier.exec([{ foo: 'b' }]), undefined)
})
7 changes: 6 additions & 1 deletion src/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ function detectDiff(actual, expected, path: string[] = []) {
const diff: SatisfierExec[] = []
const expectedType = typeof expected
if (expectedType === 'function') {
if (!(expected as Function)(actual)) {
if (Array.isArray(actual)) {
actual.forEach((a, i) => {
diff.push(...detectDiff(a, expected, path.concat([`[${i}]`])))
})
}
else if (!(expected as Function)(actual)) {
diff.push({
path,
expected,
Expand Down

0 comments on commit 19e4218

Please sign in to comment.