diff --git a/src/createSatisfier.exec.spec.ts b/src/createSatisfier.exec.spec.ts index 4319a82..9ae2336 100644 --- a/src/createSatisfier.exec.spec.ts +++ b/src/createSatisfier.exec.spec.ts @@ -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) +}) diff --git a/src/createSatisfier.ts b/src/createSatisfier.ts index 33e968b..4266f33 100644 --- a/src/createSatisfier.ts +++ b/src/createSatisfier.ts @@ -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,