Skip to content

Commit

Permalink
Fix function support on _fallback (#8545)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Mar 1, 2021
1 parent 86b1892 commit ae95a82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/helpers/helpers.config.js
Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions test/specs/helpers.config.tests.js
Expand Up @@ -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: {
Expand Down

0 comments on commit ae95a82

Please sign in to comment.