chai-sorted
Advanced tools
Comparing version 0.1.2 to 0.2.0
'use strict' | ||
var isSorted = require('./is.sorted') | ||
var chaiIsSorted = function (chai, array, useDescendingOrder) { | ||
var chaiIsSorted = function (chai, array, options) { | ||
var useDescendingOrder = typeof options === 'object' ? !!options.descending : false | ||
// Backwards compatibility | ||
if (typeof options === 'boolean') { | ||
useDescendingOrder = options | ||
} | ||
var Assertion = chai.Assertion | ||
@@ -16,19 +23,19 @@ new Assertion(array).to.be.a('array') | ||
module.exports = function (chai, utils) { | ||
chai.Assertion.addMethod('sorted', function (useDescendingOrder) { | ||
chaiIsSorted.call(this, chai, this._obj, useDescendingOrder) | ||
chai.Assertion.addMethod('sorted', function (options) { | ||
chaiIsSorted.call(this, chai, this._obj, options) | ||
}) | ||
utils.addProperty(chai.Assertion.prototype, 'descending', function () { | ||
chaiIsSorted.call(this, chai, this._obj, true) | ||
chaiIsSorted.call(this, chai, this._obj, { descending: true }) | ||
}) | ||
utils.addProperty(chai.Assertion.prototype, 'ascending', function () { | ||
chaiIsSorted.call(this, chai, this._obj, false) | ||
chaiIsSorted.call(this, chai, this._obj) | ||
}) | ||
chai.Assertion.addMethod('sortedBy', function (key, useDescendingOrder) { | ||
chai.Assertion.addMethod('sortedBy', function (key, options) { | ||
var array = utils.flag(this, 'object').map(function (item) { | ||
return item[key] | ||
}) | ||
chaiIsSorted.call(this, chai, array, useDescendingOrder) | ||
chaiIsSorted.call(this, chai, array, options) | ||
}) | ||
@@ -40,3 +47,3 @@ | ||
}) | ||
chaiIsSorted.call(this, chai, array, true) | ||
chaiIsSorted.call(this, chai, array, { descending: true }) | ||
}) | ||
@@ -48,4 +55,4 @@ | ||
}) | ||
chaiIsSorted.call(this, chai, array, false) | ||
chaiIsSorted.call(this, chai, array) | ||
}) | ||
} |
{ | ||
"name": "chai-sorted", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Chai JS Plugin for testing if an array has sorted values (strings, numbers, booleans). Very helpful when writing tests for features that implement Array.prototype.sort()", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -47,3 +47,3 @@ [![npm Version](https://img.shields.io/npm/v/chai-sorted.svg)](https://npmjs.org/package/chai-sorted) | ||
// or | ||
expect(["a","b"]).to.be.sorted(false) | ||
expect(["a","b"]).to.be.sorted({descending: false}) | ||
``` | ||
@@ -54,3 +54,3 @@ | ||
```javascript | ||
expect(["b","apples"]).to.be.sorted(true) | ||
expect(["b","apples"]).to.be.sorted({descending: true}) | ||
``` | ||
@@ -71,3 +71,3 @@ | ||
```javascript | ||
expect([{id:2,name:"bat"},{id:3,name:"apples"}]).to.be.sortedBy("name", true) | ||
expect([{id:2,name:"bat"},{id:3,name:"apples"}]).to.be.sortedBy("name", {descending: true}) | ||
``` | ||
@@ -74,0 +74,0 @@ |
@@ -25,11 +25,22 @@ 'use strict' | ||
describe('to.be.sorted(true) in descending order', function () { | ||
it('with array of numbers', function () { | ||
describe('Backwards compatibility', function () { | ||
it('sorts in descending order with boolean argument', function () { | ||
expect([3, 2, 1]).to.be.sorted(true) | ||
expect(['b', 'apples']).to.be.sorted(true) | ||
expect(['validCharacters', 'process', 'decal']).to.be.sorted(true) | ||
expect([{id: 3}, {id: 2}, {id: 2}]).to.be.sortedBy('id', true) | ||
expect([{id: 'c'}, {id: 'b'}, {id: 'a'}]).to.be.sortedBy('id', true) | ||
expect([{id: 1, name: 'cat'}, {id: 34, name: 'bat'}, {id: 3, name: 'b'}, {size: 'large', name: 'apple'}]).to.be.sortedBy('name', true) | ||
}) | ||
}) | ||
describe('to.be.sorted({ descending: true }) in descending order', function () { | ||
it('with array of numbers', function () { | ||
expect(['b', 'apples']).to.be.sorted(true) | ||
expect([3, 2, 1]).to.be.sorted({ descending: true }) | ||
}) | ||
it('with array of numbers', function () { | ||
expect(['b', 'apples']).to.be.sorted({ descending: true }) | ||
}) | ||
it('with array of words with mixed case', function () { | ||
expect(['validCharacters', 'process', 'decal']).to.be.sorted(true) | ||
expect(['validCharacters', 'process', 'decal']).to.be.sorted({ descending: true }) | ||
}) | ||
@@ -51,11 +62,17 @@ // Additional condition testing of values is done in test/is.sorted.js | ||
describe('to.be.sortedBy({ descending: false }) in ascending order', function () { | ||
it('key id of numbers', function () { | ||
expect([1, 2, 3]).to.be.sorted({ descending: false }) | ||
}) | ||
}) | ||
describe('to.be.sortedBy() in descending order', function () { | ||
it('key id of numbers', function () { | ||
expect([{id: 3}, {id: 2}, {id: 2}]).to.be.sortedBy('id', true) | ||
expect([{id: 3}, {id: 2}, {id: 2}]).to.be.sortedBy('id', { descending: true }) | ||
}) | ||
it('key id of strings', function () { | ||
expect([{id: 'c'}, {id: 'b'}, {id: 'a'}]).to.be.sortedBy('id', true) | ||
expect([{id: 'c'}, {id: 'b'}, {id: 'a'}]).to.be.sortedBy('id', { descending: true }) | ||
}) | ||
it('key name of words and letters', function () { | ||
expect([{id: 1, name: 'cat'}, {id: 34, name: 'bat'}, {id: 3, name: 'b'}, {size: 'large', name: 'apple'}]).to.be.sortedBy('name', true) | ||
expect([{id: 1, name: 'cat'}, {id: 34, name: 'bat'}, {id: 3, name: 'b'}, {size: 'large', name: 'apple'}]).to.be.sortedBy('name', { descending: true }) | ||
}) | ||
@@ -62,0 +79,0 @@ }) |
15351
271