Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Change accepts a function as object
  • Loading branch information
lucasfcosta committed Feb 11, 2016
commit 0fe471885ca225218d2e38b34e98e1dba2a334ca
21 changes: 15 additions & 6 deletions lib/chai/core/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1635,7 +1635,7 @@ module.exports = function (chai, _) {
* @alias changes
* @alias Change
* @param {String} object
* @param {String} property name
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace BDD
* @api public
Expand All @@ -1644,16 +1644,25 @@ module.exports = function (chai, _) {
function assertChanges (object, prop, msg) {
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];
if (!prop) {
new Assertion(object).is.a('function');
var initial = object();
} else {
new Assertion(object, msg).to.have.property(prop);
initial = object[prop];
}

fn();

var final = prop === undefined ? object() : object[prop];
var msgObj = prop === undefined ? initial : '.' + prop;

this.assert(
initial !== object[prop]
, 'expected .' + prop + ' to change'
, 'expected .' + prop + ' to not change'
initial !== final
, 'expected ' + msgObj + ' to change'
, 'expected ' + msgObj + ' to not change'
);
}

Expand Down
18 changes: 14 additions & 4 deletions lib/chai/interface/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,14 +1354,19 @@ module.exports = function (chai, util) {
*
* @name changes
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.changes = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop
prop = null;
}

new Assertion(fn, msg).to.change(obj, prop);
}

Expand All @@ -1376,14 +1381,19 @@ module.exports = function (chai, util) {
*
* @name doesNotChange
* @param {Function} modifier function
* @param {Object} object
* @param {String} property name
* @param {Object} object or getter function
* @param {String} property name _optional_
* @param {String} message _optional_
* @namespace Assert
* @api public
*/

assert.doesNotChange = function (fn, obj, prop, msg) {
if (arguments.length === 3 && typeof obj === 'function') {
msg = prop;
prop = null;
}

new Assertion(fn, msg).to.not.change(obj, prop);
}

Expand Down
7 changes: 6 additions & 1 deletion test/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,13 +873,18 @@ describe('assert', function () {

it('change', function() {
var obj = { value: 10, str: 'foo' },
laughs = ['lol', 'haha'],
fn = function() { obj.value += 5 },
bangFn = function() { obj.str += '!' },
smFn = function() { 'foo' + 'bar' };
smFn = function() { 'foo' + 'bar' },
addFn = function() { laughs.push('hue') },
getFn = function() { return laughs.length };

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

it('increase, decrease', function() {
Expand Down
7 changes: 6 additions & 1 deletion test/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,14 +1121,19 @@ describe('expect', function () {

it('change', function() {
var obj = { value: 10, str: 'foo' },
heroes = ['spiderman', 'superman'],
fn = function() { obj.value += 5 },
sameFn = function() { 'foo' + 'bar' },
bangFn = function() { obj.str += '!' };
bangFn = function() { obj.str += '!' },
batFn = function() { heroes.push('batman') },
lenFn = function() { return heroes.length };

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

it('increase, decrease', function() {
Expand Down
8 changes: 7 additions & 1 deletion test/should.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,15 +950,21 @@ describe('should', function() {

it('change', function() {
var obj = { value: 10, str: 'foo' },
fruits = ['apple', 'pear'],
fn = function() { obj.value += 5 },
sameFn = function() { obj.value += 0 },
decFn = function() { obj.value -= 3 },
bangFn = function() { obj.str += '!' };
bangFn = function() { obj.str += '!' },
nanaFn = function() { fruits.push('banana') },
lenFn = function() { return fruits.length },
noFn = function() { return null };

fn.should.change(obj, 'value');
sameFn.should.not.change(obj, 'value');
sameFn.should.not.change(obj, 'str');
bangFn.should.change(obj, 'str');
nanaFn.should.change(lenFn);
noFn.should.not.change(lenFn);
});

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