various fixes + add unit tests for parsed styles

This commit is contained in:
ilyes-ced
2024-02-07 00:21:33 +00:00
parent 2640120be8
commit 17959f648a
5 changed files with 79 additions and 26 deletions

View File

@@ -96,9 +96,9 @@ style: styleComponent
;
stylesOpt: style
{$$ = [$style]}
{$$ = [$style.trim()]}
| stylesOpt COMMA style
{$stylesOpt.push($style);$$ = $stylesOpt;}
{$stylesOpt.push($style.trim());$$ = $stylesOpt;}
;
classDefStatement

View File

@@ -203,20 +203,34 @@ describe('Testing quadrantChart jison file', () => {
it('should be able to parse points', () => {
let str = 'quadrantChart\npoint1: [0.1, 0.4]';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.addPoint).toHaveBeenCalledWith({ text: 'point1', type: 'text' }, '0.1', '0.4');
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'point1', type: 'text' },
'',
'0.1',
'0.4',
[]
);
clearMocks();
str = 'QuadRantChart \n Point1 : [0.1, 0.4] ';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.addPoint).toHaveBeenCalledWith({ text: 'Point1', type: 'text' }, '0.1', '0.4');
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Point1', type: 'text' },
'',
'0.1',
'0.4',
[]
);
clearMocks();
str = 'QuadRantChart \n "Point1 : (* +=[❤": [1, 0] ';
expect(parserFnConstructor(str)).not.toThrow();
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Point1 : (* +=[❤', type: 'text' },
'',
'1',
'0'
'0',
[]
);
clearMocks();
@@ -255,16 +269,32 @@ describe('Testing quadrantChart jison file', () => {
expect(mockDB.setQuadrant4Text).toHaveBeenCalledWith({ text: 'Visionaries', type: 'text' });
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Microsoft', type: 'text' },
'',
'0.75',
'0.75'
'0.75',
[]
);
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Salesforce', type: 'text' },
'',
'0.55',
'0.60'
'0.60',
[]
);
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'IBM', type: 'text' },
'',
'0.51',
'0.40',
[]
);
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Incorta', type: 'text' },
'',
'0.20',
'0.30',
[]
);
expect(mockDB.addPoint).toHaveBeenCalledWith({ text: 'IBM', type: 'text' }, '0.51', '0.40');
expect(mockDB.addPoint).toHaveBeenCalledWith({ text: 'Incorta', type: 'text' }, '0.20', '0.30');
});
it('should be able to parse the whole chart with point styling with all params or some params', () => {
@@ -301,7 +331,7 @@ describe('Testing quadrantChart jison file', () => {
'',
'0.75',
'0.75',
'radius: 10'
['radius: 10']
);
expect(mockDB.addPoint).toHaveBeenCalledWith(
{ text: 'Salesforce', type: 'text' },

View File

@@ -0,0 +1,28 @@
import quadrantDb from './quadrantDb.js';
describe('quadrant unit tests', () => {
it('should parse the styles array and return a StylesObject', () => {
const styles = ['radius: 10', 'color: #ff0000', 'stroke-color: #ff00ff', 'stroke-width: 10px'];
const result = quadrantDb.parseStyles(styles);
expect(result).toEqual({
radius: 10,
color: '#ff0000',
strokeColor: '#ff00ff',
strokeWidth: '10px',
});
});
it('should throw an error for unacceptable style name', () => {
const styles: string[] = ['test_name: value'];
expect(() => quadrantDb.parseStyles(styles)).toThrowError(
'stlye named test_name is unacceptable'
);
});
it('should return an empty StylesObject for an empty input array', () => {
const styles: string[] = [];
const result = quadrantDb.parseStyles(styles);
expect(result).toEqual({});
});
});

View File

@@ -92,11 +92,7 @@ function addPoint(textObj: LexTextObj, className: string, x: number, y: number,
}
function addClass(className: string, styles: string[]) {
const ss = parseStyles(styles);
if (Object.keys(ss).length === 0) {
throw new Error('class defintions require ss');
}
quadrantBuilder.addClass(className, ss);
quadrantBuilder.addClass(className, parseStyles(styles));
}
function setWidth(width: number) {
@@ -150,6 +146,7 @@ export default {
setXAxisRightText,
setYAxisTopText,
setYAxisBottomText,
parseStyles,
addPoint,
addClass,
getQuadrantData,