2021-10-28 06:27:35 -04:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
2022-01-07 20:16:24 -05:00
|
|
|
import {
|
|
|
|
CategoricalScheme,
|
|
|
|
getCategoricalSchemeRegistry,
|
|
|
|
CategoricalColorNamespace,
|
|
|
|
} from '@superset-ui/core';
|
|
|
|
import {
|
2018-11-03 04:39:14 -04:00
|
|
|
getNamespace,
|
|
|
|
getScale,
|
|
|
|
getColor,
|
|
|
|
DEFAULT_NAMESPACE,
|
2022-01-07 20:16:24 -05:00
|
|
|
} from '../../src/color/CategoricalColorNamespace';
|
2018-11-03 04:39:14 -04:00
|
|
|
|
|
|
|
describe('CategoricalColorNamespace', () => {
|
|
|
|
beforeAll(() => {
|
|
|
|
getCategoricalSchemeRegistry()
|
|
|
|
.registerValue(
|
|
|
|
'testColors',
|
|
|
|
new CategoricalScheme({
|
2018-11-17 14:09:14 -05:00
|
|
|
id: 'testColors',
|
2018-11-03 04:39:14 -04:00
|
|
|
colors: ['red', 'green', 'blue'],
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
.registerValue(
|
|
|
|
'testColors2',
|
|
|
|
new CategoricalScheme({
|
2018-11-17 14:09:14 -05:00
|
|
|
id: 'testColors2',
|
2018-11-03 04:39:14 -04:00
|
|
|
colors: ['red', 'green', 'blue'],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('The class constructor cannot be accessed directly', () => {
|
|
|
|
expect(typeof CategoricalColorNamespace).not.toBe('Function');
|
|
|
|
});
|
|
|
|
describe('static getNamespace()', () => {
|
|
|
|
it('returns default namespace if name is not specified', () => {
|
|
|
|
const namespace = getNamespace();
|
|
|
|
expect(namespace !== undefined).toBe(true);
|
|
|
|
expect(namespace.name).toBe(DEFAULT_NAMESPACE);
|
|
|
|
});
|
|
|
|
it('returns namespace with specified name', () => {
|
|
|
|
const namespace = getNamespace('myNamespace');
|
|
|
|
expect(namespace !== undefined).toBe(true);
|
|
|
|
expect(namespace.name).toBe('myNamespace');
|
|
|
|
});
|
|
|
|
it('returns existing instance if the name already exists', () => {
|
|
|
|
const ns1 = getNamespace('myNamespace');
|
|
|
|
const ns2 = getNamespace('myNamespace');
|
|
|
|
expect(ns1).toBe(ns2);
|
|
|
|
const ns3 = getNamespace();
|
|
|
|
const ns4 = getNamespace();
|
|
|
|
expect(ns3).toBe(ns4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('.getScale()', () => {
|
|
|
|
it('returns a CategoricalColorScale from given scheme name', () => {
|
|
|
|
const namespace = getNamespace('test-get-scale1');
|
|
|
|
const scale = namespace.getScale('testColors');
|
|
|
|
expect(scale).toBeDefined();
|
|
|
|
expect(scale.getColor('dog')).toBeDefined();
|
|
|
|
});
|
2019-01-07 13:35:16 -05:00
|
|
|
it('returns a scale when a schemeId is not specified and there is no default key', () => {
|
|
|
|
getCategoricalSchemeRegistry().clearDefaultKey();
|
|
|
|
const namespace = getNamespace('new-space');
|
|
|
|
const scale = namespace.getScale();
|
|
|
|
expect(scale).toBeDefined();
|
|
|
|
getCategoricalSchemeRegistry().setDefaultKey('testColors');
|
|
|
|
});
|
2018-11-03 04:39:14 -04:00
|
|
|
});
|
|
|
|
describe('.setColor()', () => {
|
|
|
|
it('overwrites color for all CategoricalColorScales in this namespace', () => {
|
|
|
|
const namespace = getNamespace('test-set-scale1');
|
|
|
|
namespace.setColor('dog', 'black');
|
|
|
|
const scale = namespace.getScale('testColors');
|
|
|
|
expect(scale.getColor('dog')).toBe('black');
|
|
|
|
expect(scale.getColor('boy')).not.toBe('black');
|
|
|
|
});
|
|
|
|
it('can override forcedColors in each scale', () => {
|
|
|
|
const namespace = getNamespace('test-set-scale2');
|
|
|
|
namespace.setColor('dog', 'black');
|
|
|
|
const scale = namespace.getScale('testColors');
|
|
|
|
scale.setColor('dog', 'pink');
|
|
|
|
expect(scale.getColor('dog')).toBe('black');
|
|
|
|
expect(scale.getColor('boy')).not.toBe('black');
|
|
|
|
});
|
|
|
|
it('does not affect scales in other namespaces', () => {
|
|
|
|
const ns1 = getNamespace('test-set-scale3.1');
|
|
|
|
ns1.setColor('dog', 'black');
|
|
|
|
const scale1 = ns1.getScale('testColors');
|
|
|
|
const ns2 = getNamespace('test-set-scale3.2');
|
|
|
|
const scale2 = ns2.getScale('testColors');
|
|
|
|
expect(scale1.getColor('dog')).toBe('black');
|
|
|
|
expect(scale2.getColor('dog')).not.toBe('black');
|
|
|
|
});
|
|
|
|
it('returns the namespace instance', () => {
|
|
|
|
const ns1 = getNamespace('test-set-scale3.1');
|
|
|
|
const ns2 = ns1.setColor('dog', 'black');
|
|
|
|
expect(ns1).toBe(ns2);
|
|
|
|
});
|
2021-11-03 01:02:48 -04:00
|
|
|
it('should reset colors', () => {
|
|
|
|
const ns1 = getNamespace('test-set-scale3.1');
|
|
|
|
ns1.setColor('dog', 'black');
|
|
|
|
ns1.resetColors();
|
|
|
|
expect(ns1.forcedItems).toMatchObject({});
|
|
|
|
});
|
2018-11-03 04:39:14 -04:00
|
|
|
});
|
|
|
|
describe('static getScale()', () => {
|
|
|
|
it('getScale() returns a CategoricalColorScale with default scheme in default namespace', () => {
|
|
|
|
const scale = getScale();
|
|
|
|
expect(scale).toBeDefined();
|
|
|
|
const scale2 = getNamespace().getScale();
|
2021-11-02 09:16:49 -04:00
|
|
|
expect(scale2).toBeDefined();
|
2018-11-03 04:39:14 -04:00
|
|
|
});
|
|
|
|
it('getScale(scheme) returns a CategoricalColorScale with specified scheme in default namespace', () => {
|
2021-11-02 09:16:49 -04:00
|
|
|
const scale = getNamespace().getScale('testColors');
|
2018-11-03 04:39:14 -04:00
|
|
|
expect(scale).toBeDefined();
|
|
|
|
});
|
|
|
|
it('getScale(scheme, namespace) returns a CategoricalColorScale with specified scheme in specified namespace', () => {
|
2021-11-02 09:16:49 -04:00
|
|
|
const scale = getNamespace('test-getScale').getScale('testColors');
|
2018-11-03 04:39:14 -04:00
|
|
|
expect(scale).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('static getColor()', () => {
|
|
|
|
it('getColor(value) returns a color from default scheme in default namespace', () => {
|
|
|
|
const value = 'dog';
|
|
|
|
const color = getColor(value);
|
2020-04-01 21:53:41 -04:00
|
|
|
const color2 = getNamespace().getScale().getColor(value);
|
2018-11-03 04:39:14 -04:00
|
|
|
expect(color).toBe(color2);
|
|
|
|
});
|
|
|
|
it('getColor(value, scheme) returns a color from specified scheme in default namespace', () => {
|
|
|
|
const value = 'dog';
|
|
|
|
const scheme = 'testColors';
|
|
|
|
const color = getColor(value, scheme);
|
2020-04-01 21:53:41 -04:00
|
|
|
const color2 = getNamespace().getScale(scheme).getColor(value);
|
2018-11-03 04:39:14 -04:00
|
|
|
expect(color).toBe(color2);
|
|
|
|
});
|
|
|
|
it('getColor(value, scheme, namespace) returns a color from specified scheme in specified namespace', () => {
|
|
|
|
const value = 'dog';
|
|
|
|
const scheme = 'testColors';
|
|
|
|
const namespace = 'test-getColor';
|
|
|
|
const color = getColor(value, scheme, namespace);
|
2020-04-01 21:53:41 -04:00
|
|
|
const color2 = getNamespace(namespace).getScale(scheme).getColor(value);
|
2018-11-03 04:39:14 -04:00
|
|
|
expect(color).toBe(color2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|