From 3d20bc99a032a7029237d728850cccb59ec5c3f3 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Mon, 1 Mar 2021 17:52:45 +0200 Subject: [PATCH] Fix function support on _fallback --- src/helpers/helpers.config.js | 5 +++-- test/specs/helpers.config.tests.js | 31 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 7fd5674ada9..1a4e2779a6e 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -245,14 +245,15 @@ function resolveFallback(fallback, prop, value) { return isFunction(fallback) ? fallback(prop, value) : fallback; } -const getScope = (key, parent) => key === true ? parent : resolveObjectKey(parent, key); +const getScope = (key, parent) => key === true ? parent + : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; function addScopes(set, parentScopes, key, parentFallback) { for (const parent of parentScopes) { const scope = getScope(key, parent); if (scope) { set.add(scope); - const fallback = scope._fallback; + const fallback = resolveFallback(scope._fallback, key, scope); if (defined(fallback) && fallback !== key && fallback !== parentFallback) { // When we reach the descriptor that defines a new _fallback, return that. // The fallback will resume to that new scope. diff --git a/test/specs/helpers.config.tests.js b/test/specs/helpers.config.tests.js index e9a22a1377e..050d935909b 100644 --- a/test/specs/helpers.config.tests.js +++ b/test/specs/helpers.config.tests.js @@ -128,6 +128,37 @@ describe('Chart.helpers.config', function() { }); }); + it('should support _fallback as function', function() { + const descriptors = { + _fallback: (prop, value) => prop === 'hover' && value.shouldFall && 'interaction', + }; + const defaults = { + interaction: { + mode: 'test', + priority: 'fall' + }, + hover: { + priority: 'main' + } + }; + const options = { + interaction: { + a: 1 + }, + hover: { + shouldFall: true, + b: 2 + } + }; + const resolver = _createResolver([options, defaults, descriptors]); + expect(resolver.hover).toEqualOptions({ + mode: 'test', + priority: 'main', + a: 1, + b: 2 + }); + }); + it('should not fallback by default', function() { const defaults = { hover: {