(
name: string,
shapePredicate: (obj: any) => obj is T,
expected: Partial<T> = {},
)
| 21 | * @param expected Expected set of properties to be found on the object. |
| 22 | */ |
| 23 | export function matchObjectShape<T>( |
| 24 | name: string, |
| 25 | shapePredicate: (obj: any) => obj is T, |
| 26 | expected: Partial<T> = {}, |
| 27 | ): jasmine.AsymmetricMatcher<T> { |
| 28 | const matcher = function () {}; |
| 29 | let _actual: any = null; |
| 30 | let _matcherUtils: jasmine.MatchersUtil = null!; |
| 31 | |
| 32 | matcher.asymmetricMatch = function (actual: any, matcherUtils: jasmine.MatchersUtil) { |
| 33 | _actual = actual; |
| 34 | _matcherUtils = matcherUtils; |
| 35 | if (!shapePredicate(actual)) return false; |
| 36 | for (const key in expected) { |
| 37 | if (expected.hasOwnProperty(key) && !matcherUtils.equals(actual[key], expected[key])) { |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | return true; |
| 42 | }; |
| 43 | matcher.jasmineToString = function (pp: (value: any) => string) { |
| 44 | let errors: string[] = []; |
| 45 | if (!_actual || typeof _actual !== 'object') { |
| 46 | return `Expecting ${pp(expect)} got ${pp(_actual)}`; |
| 47 | } |
| 48 | for (const key in expected) { |
| 49 | if (expected.hasOwnProperty(key) && !_matcherUtils.equals(_actual[key], expected[key])) |
| 50 | errors.push(`\n property obj.${key} to equal ${expected[key]} but got ${_actual[key]}`); |
| 51 | } |
| 52 | return errors.join('\n'); |
| 53 | }; |
| 54 | |
| 55 | return matcher; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Asymmetric matcher which matches a `TView` of a given shape. |
no test coverage detected
searching dependent graphs…