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)
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.
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.
Objects have two types of properties: data and accessor properties.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With