Skip to content

Commit

Permalink
updated change to work w/ non-number values + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmpolis committed Dec 29, 2014
1 parent 647e49c commit cb1c33b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 13 deletions.
13 changes: 9 additions & 4 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1428,15 +1428,18 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object');
new Assertion(object, msg).to.have.property(prop);
new Assertion(fn).is.a('function');
flag(this, 'property', prop);

var initial = object[prop];
fn();
var delta = object[prop] - initial;
flag(this, 'delta', delta);
flag(this, 'property', prop);

if ('number' == typeof initial) {
flag(this, 'delta', object[prop] - initial);
}

this.assert(
delta != 0
initial !== object[prop]
, 'expected .' + prop + ' to change'
, 'expected .' + prop + ' to not change'
);
Expand Down Expand Up @@ -1469,6 +1472,7 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object');
new Assertion(object, msg).to.have.property(prop);
new Assertion(fn).is.a('function');

var initial = object[prop];
fn();
Expand Down Expand Up @@ -1510,6 +1514,7 @@ module.exports = function (chai, _) {
if (msg) flag(this, 'message', msg);
var fn = flag(this, 'object');
new Assertion(object, msg).to.have.property(prop);
new Assertion(fn).is.a('function');

var initial = object[prop];
fn();
Expand Down
8 changes: 5 additions & 3 deletions test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,14 @@ describe('assert', function () {
});

it('change', function() {
var obj = { value: 10 },
fn = function() { obj.value += 5 },
smFn = function() { 'foo' + 'bar' };
var obj = { value: 10, str: 'foo' },
fn = function() { obj.value += 5 },
bangFn = function() { obj.str += '!' },
smFn = function() { 'foo' + 'bar' };

assert.changes(fn, obj, 'value');
assert.doesNotChange(smFn, obj, 'value');
assert.changes(bangFn, obj, 'str');
});

it('increase, decrease', function() {
Expand Down
9 changes: 6 additions & 3 deletions test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -901,13 +901,16 @@ describe('expect', function () {
});

it('change', function() {
var obj = { value: 10 },
fn = function() { obj.value += 5 },
sameFn = function() { 'foo' + 'bar' };
var obj = { value: 10, str: 'foo' },
fn = function() { obj.value += 5 },
sameFn = function() { 'foo' + 'bar' },
bangFn = function() { obj.str += '!' };

expect(fn).to.change(obj, 'value');
expect(fn).to.change(obj, 'value').by(5);
expect(sameFn).to.not.change(obj, 'value');
expect(sameFn).to.not.change(obj, 'str');
expect(bangFn).to.change(obj, 'str');
});

it('increase, decrease', function() {
Expand Down
9 changes: 6 additions & 3 deletions test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,15 +780,18 @@ describe('should', function() {
});

it('change', function() {
var obj = { value: 10 },
var obj = { value: 10, str: 'foo' },
fn = function() { obj.value += 5 },
sameFn = function() { 'foo' + 'bar' },
decFn = function() { obj.value -= 3 };
sameFn = function() { obj.value += 0 },
decFn = function() { obj.value -= 3 },
bangFn = function() { obj.str += '!' };

fn.should.change(obj, 'value');
fn.should.change(obj, 'value').by(5);
decFn.should.change(obj, 'value').by(-3);
sameFn.should.not.change(obj, 'value');
sameFn.should.not.change(obj, 'str');
bangFn.should.change(obj, 'str');
});

it('increase, decrease', function() {
Expand Down

0 comments on commit cb1c33b

Please sign in to comment.