fix: use maps for quadrant diagrams

This commit is contained in:
Yash Singh 2024-04-17 10:25:54 -07:00
parent 357da0ca28
commit 128c69aa76
2 changed files with 15 additions and 4 deletions

View File

@ -2,6 +2,7 @@
import { parser } from './quadrant.jison';
import type { Mock } from 'vitest';
import { vi } from 'vitest';
import { addClass } from '../../flowchart/flowDb.js';
const parserFnConstructor = (str: string) => {
return () => {
@ -20,6 +21,7 @@ const mockDB: Record<string, Mock<any, any>> = {
setYAxisBottomText: vi.fn(),
setDiagramTitle: vi.fn(),
addPoint: vi.fn(),
addClass: vi.fn(),
};
function clearMocks() {
@ -423,4 +425,13 @@ describe('Testing quadrantChart jison file', () => {
['stroke-width: 10px']
);
});
it('should be able to handle constructor as a className', () => {
const str = `quadrantChart
classDef constructor fill:#ff0000
Microsoft:::constructor: [0.75, 0.75]
`;
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.addClass).toHaveBeenCalledWith('constructor', ['fill:#ff0000']);
});
});

View File

@ -127,7 +127,7 @@ export class QuadrantBuilder {
private config: QuadrantBuilderConfig;
private themeConfig: QuadrantBuilderThemeConfig;
private data: QuadrantBuilderData;
private classes: Record<string, StylesObject> = {};
private classes: Map<string, StylesObject> = new Map();
constructor() {
this.config = this.getDefaultConfig();
@ -202,7 +202,7 @@ export class QuadrantBuilder {
this.config = this.getDefaultConfig();
this.themeConfig = this.getDefaultThemeConfig();
this.data = this.getDefaultData();
this.classes = {};
this.classes = new Map();
log.info('clear called');
}
@ -215,7 +215,7 @@ export class QuadrantBuilder {
}
addClass(className: string, styles: StylesObject) {
this.classes[className] = styles;
this.classes.set(className, styles);
}
setConfig(config: Partial<QuadrantBuilderConfig>) {
@ -486,7 +486,7 @@ export class QuadrantBuilder {
.range([quadrantHeight + quadrantTop, quadrantTop]);
const points: QuadrantPointType[] = this.data.points.map((point) => {
const classStyles = this.classes[point.className as keyof typeof this.classes];
const classStyles = this.classes.get(point.className!);
if (classStyles) {
point = { ...classStyles, ...point };
}