diff options
| author | Julien Mougenot <jum@odoo.com> | 2020-07-13 13:03:23 +0000 |
|---|---|---|
| committer | Mathieu Duckerts-Antoine <dam@odoo.com> | 2020-07-23 08:21:09 +0000 |
| commit | 5698d1fab9cf36714f677ae6942201d5c9354798 (patch) | |
| tree | f6f9c7e70f34adb4a3c375f42700804702aa664c /addons/web/static/tests/core | |
| parent | ea1741bc743b2066e871b526fc0d7cbf30b08fef (diff) | |
[IMP] web: add Registry predicate
Before this commit, any value could be added to registries and the class
needed to be overridden to assert that added values would answer to a
certain predicate.
Now, the registry can be instantiated with a given predicate function
that all added value must pass to be registered (default: all values
are accepted).
Existing implentations have been adapted:
- FieldRegistry
- FieldRegistryOwl.
Diffstat (limited to 'addons/web/static/tests/core')
| -rw-r--r-- | addons/web/static/tests/core/registry_tests.js | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/addons/web/static/tests/core/registry_tests.js b/addons/web/static/tests/core/registry_tests.js index 9f9f7b467cb..eb3389a4383 100644 --- a/addons/web/static/tests/core/registry_tests.js +++ b/addons/web/static/tests/core/registry_tests.js @@ -70,6 +70,21 @@ QUnit.module('core', {}, function () { assert.deepEqual(registry.values(), ['bar', 'foo', 'qux']); }); + QUnit.test("predicate prevents invalid values", function (assert) { + assert.expect(5); + + const predicate = value => typeof value === "number"; + const registry = new Registry(null, predicate); + registry.onAdd((key) => assert.step(key)); + + assert.ok(registry.add("age", 23)); + assert.throws( + () => registry.add("name", "Fred"), + new Error(`Value of key "name" does not pass the addition predicate.`) + ); + assert.deepEqual(registry.entries(), { age: 23 }); + assert.verifySteps(["age"]); + }); }); }); |
