Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.which property #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down