diff --git a/README.md b/README.md index 80e9d74..93bd4a6 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,15 @@ expect(window).to.have.property('expect', expect) expect({a: 'b'}).to.have.property('a'); ``` +**which**: chains tests on your properties + +````js +expect({ + foo : [] +}).to.have.property("foo") + .which.is.an("array"); +```` + **key**/**keys**: asserts the presence of a key. Supports the `only` modifier ```js diff --git a/expect.js b/expect.js index 58c8682..a4d2482 100644 --- a/expect.js +++ b/expect.js @@ -25,6 +25,7 @@ var flags = { not: ['to', 'be', 'have', 'include', 'only'] , to: ['be', 'have', 'include', 'only', 'not'] + , has : ['own'] , only: ['have'] , have: ['own'] , be: ['an'] @@ -339,6 +340,9 @@ */ Assertion.prototype.property = function (name, val) { + // make a new object so the tester can test the new object too + this.which = expect(this.obj[name]); + if (this.flags.own) { this.assert( Object.prototype.hasOwnProperty.call(this.obj, name) diff --git a/test/expect.js b/test/expect.js index d8adad1..98afa45 100644 --- a/test/expect.js +++ b/test/expect.js @@ -395,6 +395,18 @@ describe('expect', function () { }, "expected { length: 12 } to not have own property 'length'"); }); + it("should test property().which", function () { + expect({ foo: { + bar : 0 + }}).to.have.property("foo").which.eql({ bar : 0 }); + }); + + it("should allow .has after .which", function () { + expect({ foo: { + bar : 0 + }}).to.have.property("foo").which.has.property("bar", 0); + }); + it('should test string()', function () { expect('foobar').to.contain('bar'); expect('foobar').to.contain('foo');