Skip to content

Commit

Permalink
fix: handles function-object
Browse files Browse the repository at this point in the history
fixes #228
  • Loading branch information
unional committed Oct 8, 2022
1 parent 260ca9c commit 6abc8c3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ts/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,20 @@ function diff(expected: any, actual: any, path: Diff['path'] = [], _index?: numb
}

if (expectedType === 'function') {
if (Object.keys(expected).length > 0) {
const ro = diffObject(expected, actual, path, _index)
if (ro.length === 0) return ro
}
const r = (expected as Predicate)(actual, path)
if (r === true) return noDiff
return r ? r : [{ path, expected, actual }]
}

// expected is an object
return diffObject(expected, actual, path, _index)
}

function diffObject(expected: any, actual: any, path: Diff['path'] = [], _index?: number): Diff[] {
if (actual === undefined || actual === null) {
return [{ path, expected, actual }]
}
Expand Down
4 changes: 4 additions & 0 deletions ts/satisfies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ describe('when actual is object', () => {
test('deep expected property can be predicate', () => {
expect(satisfies({ a: { b: 'foo' } }, { a: { b: v => v === 'foo' } })).toBe(true)
})

it.only('works with function-object', () => {
expect(satisfies({ a: 1 }, Object.assign(() => { }, { a: 1 }))).toBe(true)
})
})

describe('when actual is array', () => {
Expand Down

0 comments on commit 6abc8c3

Please sign in to comment.