Skip to content

Commit

Permalink
fix: use maps for quadrant diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 committed Apr 17, 2024
1 parent 357da0c commit f730612
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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() {
Expand Down Expand Up @@ -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']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
}

Expand All @@ -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>) {
Expand Down Expand Up @@ -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 };
}
Expand Down

0 comments on commit f730612

Please sign in to comment.