From 35c6b671de36543d7fbcd86a0b7063abe1ca638c Mon Sep 17 00:00:00 2001 From: Yokozuna59 Date: Mon, 19 Jun 2023 17:46:19 +0300 Subject: [PATCH] add config unit test cases for pie chart --- packages/mermaid/src/diagrams/pie/pie.spec.ts | 283 ++++++++++-------- 1 file changed, 152 insertions(+), 131 deletions(-) diff --git a/packages/mermaid/src/diagrams/pie/pie.spec.ts b/packages/mermaid/src/diagrams/pie/pie.spec.ts index 8f4a66bb0..af02ab7bb 100644 --- a/packages/mermaid/src/diagrams/pie/pie.spec.ts +++ b/packages/mermaid/src/diagrams/pie/pie.spec.ts @@ -1,6 +1,6 @@ // @ts-ignore - jison doesn't export types import { parser } from './parser/pie.jison'; -import { db } from './pieDb.js'; +import { DEFAULT_PIE_DB, db } from './pieDb.js'; import { setConfig } from '../../config.js'; setConfig({ @@ -10,139 +10,160 @@ setConfig({ describe('pie chart', () => { beforeEach(() => { parser.yy = db; - parser.yy.clear(); + db.clear(); + db.reset(); }); - it('should handle very simple pie', () => { - parser.parse(`pie - "ash": 100 - `); - const sections = db.getSections(); - expect(sections['ash']).toBe(100); - }); - - it('should handle simple pie', () => { - parser.parse(`pie - "ash" : 60 - "bat" : 40 - `); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with comments', () => { - parser.parse(`pie - %% comments - "ash" : 60 - "bat" : 40 - `); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with a directive', () => { - parser.parse(`%%{init: {'logLevel':0}}%% - pie - "ash" : 60 - "bat" : 40 - `); - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with a title', () => { - parser.parse(`pie title a 60/40 pie - "ash" : 60 - "bat" : 40 - `); - - const title = db.getDiagramTitle(); - expect(title).toBe('a 60/40 pie'); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with an acc title (accTitle)', () => { - parser.parse(`pie title a neat chart - accTitle: a neat acc title - "ash" : 60 - "bat" : 40 - `); - - const title = db.getDiagramTitle(); - expect(title).toBe('a neat chart'); - - const accTitle = db.getAccTitle(); - expect(accTitle).toBe('a neat acc title'); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with an acc description (accDescr)', () => { - parser.parse(`pie title a neat chart - accDescr: a neat description - "ash" : 60 - "bat" : 40 - `); - - const title = db.getDiagramTitle(); - expect(title).toBe('a neat chart'); - - const description = db.getAccDescription(); - expect(description).toBe('a neat description'); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with a multiline acc description (accDescr)', () => { - parser.parse(`pie title a neat chart - accDescr { - a neat description - on multiple lines - } - "ash" : 60 - "bat" : 40 - `); - - const title = db.getDiagramTitle(); - expect(title).toBe('a neat chart'); - - const description = db.getAccDescription(); - expect(description).toBe('a neat description\non multiple lines'); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with positive decimal', () => { - parser.parse(`pie - "ash" : 60.67 - "bat" : 40 - `); - - const sections = db.getSections(); - expect(sections['ash']).toBe(60.67); - expect(sections['bat']).toBe(40); - }); - - it('should handle simple pie with negative decimal', () => { - expect(() => { + describe('parse', () => { + it('should handle very simple pie', () => { parser.parse(`pie - "ash" : -60.67 - "bat" : 40.12 + "ash": 100 `); - }).toThrowError(); + const sections = db.getSections(); + expect(sections['ash']).toBe(100); + }); + + it('should handle simple pie', () => { + parser.parse(`pie + "ash" : 60 + "bat" : 40 + `); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with comments', () => { + parser.parse(`pie + %% comments + "ash" : 60 + "bat" : 40 + `); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with a directive', () => { + parser.parse(`%%{init: {'logLevel':0}}%% + pie + "ash" : 60 + "bat" : 40 + `); + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with a title', () => { + parser.parse(`pie title a 60/40 pie + "ash" : 60 + "bat" : 40 + `); + + const title = db.getDiagramTitle(); + expect(title).toBe('a 60/40 pie'); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with an acc title (accTitle)', () => { + parser.parse(`pie title a neat chart + accTitle: a neat acc title + "ash" : 60 + "bat" : 40 + `); + + const title = db.getDiagramTitle(); + expect(title).toBe('a neat chart'); + + const accTitle = db.getAccTitle(); + expect(accTitle).toBe('a neat acc title'); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with an acc description (accDescr)', () => { + parser.parse(`pie title a neat chart + accDescr: a neat description + "ash" : 60 + "bat" : 40 + `); + + const title = db.getDiagramTitle(); + expect(title).toBe('a neat chart'); + + const description = db.getAccDescription(); + expect(description).toBe('a neat description'); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with a multiline acc description (accDescr)', () => { + parser.parse(`pie title a neat chart + accDescr { + a neat description + on multiple lines + } + "ash" : 60 + "bat" : 40 + `); + + const title = db.getDiagramTitle(); + expect(title).toBe('a neat chart'); + + const description = db.getAccDescription(); + expect(description).toBe('a neat description\non multiple lines'); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with positive decimal', () => { + parser.parse(`pie + "ash" : 60.67 + "bat" : 40 + `); + + const sections = db.getSections(); + expect(sections['ash']).toBe(60.67); + expect(sections['bat']).toBe(40); + }); + + it('should handle simple pie with negative decimal', () => { + expect(() => { + parser.parse(`pie + "ash" : -60.67 + "bat" : 40.12 + `); + }).toThrowError(); + }); + }); + + describe('config', () => { + it('setConfig', () => { + db.setConfig({ useWidth: 850, useMaxWidth: undefined }); + expect(db.getConfig().useWidth).toBe(850); + expect(db.getConfig().useMaxWidth).toBeTruthy(); + }); + + it('getConfig', () => { + expect(db.getConfig()).toStrictEqual(DEFAULT_PIE_DB.config); + }); + + it('reset', () => { + db.setConfig({ useWidth: 850 }); + db.reset(); + expect(db.getConfig().useWidth).toStrictEqual(DEFAULT_PIE_DB.config.useWidth); + }); }); });