All files / src/testing-helpers/jasmine-matchers to-have-properties.matcher.ts

100% Statements 13/13
100% Branches 8/8
100% Functions 7/7
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34          1x   41x   41x 41x 96x 96x   96x 192x 96x   41x               96x         96x    
// tslint:disable:triple-equals
 
// Ref: https://github.com/hyzhak/to-have-property/blob/master/lib/index.js
//      https://github.com/angular/angular/issues/5456
 
export const toHavePropertiesMatcher = {
  toHaveProperties: (util) => {
    return {
      compare: (actual, expected) => {
        const result = {pass: true, message: null};
        const results = Object.keys(expected)
          .filter(prop => typeof expected[prop] !== 'function' )
          .map(key => checkProperty(actual, expected, key))
          .reduce((acc, curr) => {
            acc.pass = acc.pass && curr.pass;
            acc.message = [acc.message, curr.message].filter(msg => !!msg).join(', ');
            return acc;
          }, result);
        return results;
      }
    };
  }
};
 
function checkProperty(actual, expected, key) {
  const result =
    !actual.hasOwnProperty(key)
    ? { pass: false, message: `Expected property ${key} was not found` }
    : expected[key] !== undefined && expected[key] != actual[key]
      ? { pass: false, message: `Expected property ${key} to have value '${expected[key]}', but actual value is '${actual[key]}'` }
      : { pass: true, message: null };
  return result;
}