Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if object has multiple properties

I'm having trouble checking this object is there something I need to do to chain multiple .to.have.property statements? I believe I'm just returning the result from the last .to.have.property into the next one.

expect(shopify.formatRequestOptions("shop"))
  .to.have.property('url', "https://"+settings.shop+"/admin/shop.json")
  .to.have.property('method', "GET")
  .to.have.deep.property('headers.X-Shopify-Access-Token', settings.accessToken)

It seems I can use something like this chai-subset to check an object. Is there no way to chain these together? I'd hate to have to do this.

var result = shopify.formatRequestOptions("shop")
expect(result).to.have.property('url', "https://"+settings.shop+"/admin/shop.json")
expect(result).to.have.property('method', "GET")
expect(result).to.have.deep.property('headers.X-Shopify-Access-Token', settings.accessToken)
like image 309
ThomasReggi Avatar asked Jul 02 '15 18:07

ThomasReggi


People also ask

How do you know if an object has more than one property?

Summary. Use the hasOwnProperty() method to check if an property exists in the own properties of an object. Use the in operator to check if a property exists in both own properties and inherited properties of an object.

How do you check if an object has a certain property?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.

How many object properties does an object have?

Objects have two types of properties: data and accessor properties.


1 Answers

Could build your own function that just returns true / false and has any interface.

let example = {
  'name': 'thomas'
}

let hasAllProps = (obj, props) => {
  let propsTrue = _.chain(props)
  .map(prop => _.has(obj, prop))
  .without(false)
  .value()
  return (propsTrue.length === props.length)
}

console.log(hasAllProps(example, ['name'])) // true
console.log(hasAllProps(example, ['age'])) // false
like image 185
ThomasReggi Avatar answered Oct 12 '22 04:10

ThomasReggi