Chai Sorted
Chai JS 外掛,用於測試陣列是否具有排序過的值(字串、數字、布林值)。在為實作 Array.prototype.sort()
功能撰寫測試時非常有用
安裝
Node.js
透過 npm 安裝
npm install chai-sorted
像其他 Chai 外掛一樣使用此外掛
var chai = require("chai"),
expect = chai.expect; // preference and tested with expect
chai.use(require("chai-sorted"));
在瀏覽器中
此儲存庫中沒有計畫維護瀏覽器建置,因此請考慮使用 Webpack 或類似工具。
如果您想要瀏覽器安裝支援,請開啟一個 issue。
API 用法
.sorted
方法
測試陣列是否為升序排序
expect(["a","b"]).to.be.sorted()
// or
expect(["a","b"]).to.be.sorted({descending: false})
測試陣列是否為降序排序
expect(["b","apples"]).to.be.sorted({descending: true})
.sortedBy
方法
允許依屬性排序
測試陣列是否依 name
屬性升序排序
expect([{id:2,name:"apple"},{id:3,name:"bat"}]).to.be.sortedBy("name")
測試陣列是否依 name
屬性降序排序
expect([{id:2,name:"bat"},{id:3,name:"apples"}]).to.be.sortedBy("name", {descending: true})
.ascendingBy
方法
與 sortedBy
相同,但更明確
測試陣列是否依 name
屬性升序排序
expect([{id:2,name:"apple"},{id:3,name:"bat"}]).to.be.ascendingBy("name")
.descendingBy
方法
與 sortedBy
相同,但不需要傳遞 true
作為 sortedBy
的第二個參數。 它等同於執行 sortBy("name",true)
測試陣列是否依 name
屬性降序排序
expect([{id:2,name:"bat"},{id:3,name:"apples"}]).to.be.descendingBy("name")
ascending
屬性
使用屬性語法測試陣列是否為升序排序
expect(["a","b"]).to.be.ascending
descending
屬性
使用屬性語法測試陣列是否為降序排序
expect(["b","apples"]).to.be.descending
其他用法
如需其他用法或更多協助,請參閱 ./test/
下的測試