diff options
| author | Julien Mougenot <jum@odoo.com> | 2020-04-29 09:52:18 +0000 |
|---|---|---|
| committer | Mathieu Duckerts-Antoine <dam@odoo.com> | 2020-05-05 19:23:20 +0000 |
| commit | 26491fcb4ce1e21584be0454a4178f8249ec75a9 (patch) | |
| tree | 712c16df27cbb7d13f88fecdd84fe7f874dad1c1 /addons/web/static/tests | |
| parent | d64c8fa304279f52ae426bfa8f451617cba9aa06 (diff) | |
[IMP] web: add sortBy to core.utils
Add a "sortBy" helper to core utils. This function takes an array and
an optional criterion as arguments and returns a new array sorted
according to the given criterion.
Diffstat (limited to 'addons/web/static/tests')
| -rw-r--r-- | addons/web/static/tests/core/util_tests.js | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/addons/web/static/tests/core/util_tests.js b/addons/web/static/tests/core/util_tests.js index 96954e9d2f8..da6ac0d7737 100644 --- a/addons/web/static/tests/core/util_tests.js +++ b/addons/web/static/tests/core/util_tests.js @@ -7,7 +7,6 @@ QUnit.module('core', {}, function () { QUnit.module('utils'); - QUnit.test('intersperse', function (assert) { assert.expect(27); @@ -196,6 +195,69 @@ QUnit.module('core', {}, function () { assert.strictEqual(String(round_pr(457.5, 5)), '460'); assert.strictEqual(String(round_pr(457.1, 3)), '456'); }); + + QUnit.test('sortBy', function (assert) { + assert.expect(27); + const { sortBy } = utils; + const bools = [true, false, true]; + const ints = [2, 1, 5]; + const strs = ['b', 'a', 'z']; + const objbools = [{ x: true }, { x: false }, { x: true }]; + const objints = [{ x: 2 }, { x: 1 }, { x: 5 }]; + const objstrss = [{ x: 'b' }, { x: 'a' }, { x: 'z' }]; + + // Invalid + assert.throws( + () => sortBy({}), + new TypeError(`array.slice is not a function`) + ); + assert.throws( + () => sortBy([Symbol('b'), Symbol('a')]), + new TypeError(`Cannot convert a Symbol value to a number`) + ); + assert.throws( + () => sortBy(ints, true), + new Error(`Expected criterion of type 'string' or 'function' and got 'boolean'`) + ); + assert.throws( + () => sortBy(ints, 3), + new Error(`Expected criterion of type 'string' or 'function' and got 'number'`) + ); + assert.throws( + () => sortBy(ints, {}), + new Error(`Expected criterion of type 'string' or 'function' and got 'object'`) + ); + // Do not sort in place + const toSort = [2, 3, 1]; + sortBy(toSort); + assert.deepEqual(toSort, [2, 3, 1]); + // Sort (no criterion) + assert.deepEqual(sortBy([]), []); + assert.deepEqual(sortBy(ints), [1, 2, 5]); + assert.deepEqual(sortBy(bools), [false, true, true]); + assert.deepEqual(sortBy(strs), ['a', 'b', 'z']); + assert.deepEqual(sortBy(objbools), [{ x: true }, { x: false }, { x: true }]); + assert.deepEqual(sortBy(objints), [{ x: 2 }, { x: 1 }, { x: 5 }]); + assert.deepEqual(sortBy(objstrss), [{ x: 'b' }, { x: 'a' }, { x: 'z' }]); + // Sort by property + const prop = 'x'; + assert.deepEqual(sortBy([], prop), []); + assert.deepEqual(sortBy(ints, prop), [2, 1, 5]); + assert.deepEqual(sortBy(bools, prop), [true, false, true]); + assert.deepEqual(sortBy(strs, prop), ['b', 'a', 'z']); + assert.deepEqual(sortBy(objbools, prop), [{ x: false }, { x: true }, { x: true }]); + assert.deepEqual(sortBy(objints, prop), [{ x: 1 }, { x: 2 }, { x: 5 }]); + assert.deepEqual(sortBy(objstrss, prop), [{ x: 'a' }, { x: 'b' }, { x: 'z' }]); + // Sort by getter + const getter = obj => obj.x; + assert.deepEqual(sortBy([], getter), []); + assert.deepEqual(sortBy(ints, getter), [2, 1, 5]); + assert.deepEqual(sortBy(bools, getter), [true, false, true]); + assert.deepEqual(sortBy(strs, getter), ['b', 'a', 'z']); + assert.deepEqual(sortBy(objbools, getter), [{ x: false }, { x: true }, { x: true }]); + assert.deepEqual(sortBy(objints, getter), [{ x: 1 }, { x: 2 }, { x: 5 }]); + assert.deepEqual(sortBy(objstrss, getter), [{ x: 'a' }, { x: 'b' }, { x: 'z' }]); + }); }); }); |
