summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/core/util_tests.js
diff options
context:
space:
mode:
authorJulien Mougenot <jum@odoo.com>2020-06-19 07:06:28 +0000
committerAaron Bohy <aab@odoo.com>2020-06-22 13:04:00 +0000
commita6cd6d5a892f5a0501c0e81fe583bd64befff5d0 (patch)
tree428d4f7b10bcda2e08f3b15ae78f53e7f91ab92b /addons/web/static/tests/core/util_tests.js
parent669bba4694896e29c088d6ce69440d3bef8e9fe5 (diff)
[IMP] web: utils: add groupBy function
This commit introduces a new utility function: 'groupBy'. It is similar in API and result as the underscore method of the same name. X-original-commit: 834a1967161dc537cb2d5543314a062dbac08f9c
Diffstat (limited to 'addons/web/static/tests/core/util_tests.js')
-rw-r--r--addons/web/static/tests/core/util_tests.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/addons/web/static/tests/core/util_tests.js b/addons/web/static/tests/core/util_tests.js
index da6ac0d7737..6115833e4f2 100644
--- a/addons/web/static/tests/core/util_tests.js
+++ b/addons/web/static/tests/core/util_tests.js
@@ -7,6 +7,59 @@ QUnit.module('core', {}, function () {
QUnit.module('utils');
+ QUnit.test('groupBy', function (assert) {
+ assert.expect(7);
+
+ const { groupBy } = utils;
+
+ // Invalid
+ assert.throws(
+ () => groupBy({}),
+ new TypeError(`list is not iterable`)
+ );
+ assert.throws(
+ () => groupBy([], true),
+ new Error(`Expected criterion of type 'string' or 'function' and got 'boolean'`)
+ );
+ assert.throws(
+ () => groupBy([], 3),
+ new Error(`Expected criterion of type 'string' or 'function' and got 'number'`)
+ );
+ assert.throws(
+ () => groupBy([], {}),
+ new Error(`Expected criterion of type 'string' or 'function' and got 'object'`)
+ );
+
+ // criterion = default
+ assert.deepEqual(
+ groupBy(["a", "b", 1, true]),
+ {
+ 1: [1],
+ a: ["a"],
+ b: ["b"],
+ true: [true],
+ }
+ );
+ // criterion = string
+ assert.deepEqual(
+ groupBy([{ x: "a" }, { x: "a" }, { x: "b" }], "x"),
+ {
+ a: [{ x: "a" }, { x: "a" }],
+ b: [{ x: "b" }],
+ }
+ );
+ // criterion = function
+ assert.deepEqual(
+ groupBy(["a", "b", 1, true], (x) => `el${x}`),
+ {
+ ela: ["a"],
+ elb: ["b"],
+ el1: [1],
+ eltrue: [true],
+ }
+ );
+ });
+
QUnit.test('intersperse', function (assert) {
assert.expect(27);