Skip to content

Commit

Permalink
test: usecaseDb
Browse files Browse the repository at this point in the history
  • Loading branch information
teyc committed Dec 20, 2024
1 parent c96174f commit 3e5edca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/mermaid/src/diagrams/usecase/usecase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { UsecaseDB } from './usecaseDB.js';

describe('Usecase diagram', function () {
const db = new UsecaseDB();
beforeEach(function () {
db.clear();
});
it('should add node when adding participants', function () {
db.addParticipants({ service: 'Authz' });
expect(db.getServices()).toStrictEqual(['Authz']);
});
it('should add actor when adding relationship', function () {
db.addRelationship('Student', '(Enrol)', '-->');
expect(db.getActors()).toStrictEqual(['Student']);
});
it('should add use case and service when adding relationship', function () {
db.addRelationship('(Enrol)', 'Enrolment System', '-->');
expect(db.getServices()).toStrictEqual(['Enrolment System']);
expect(db.getUseCases()).toStrictEqual(['(Enrol)']);
});
});
24 changes: 21 additions & 3 deletions packages/mermaid/src/diagrams/usecase/usecaseDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ export class UsecaseDB {
}
}

addRelationship(source: string, target: string, token: string): void {
/**
* Adds an edge to the graph
* @param source - id of node
* @param target - id of node
* @param arrowString - '-->' or '->' or '-- some text -->'
*/
addRelationship(source: string, target: string, arrowString: string): void {
source = common.sanitizeText(source, getConfig());
target = common.sanitizeText(target, getConfig());
const sourceNode = this.getNode(source, isUseCaseLabel(source) ? 'usecase' : 'actor');
const targetNode = this.getNode(target, isUseCaseLabel(target) ? 'usecase' : 'service');
const label = (/--(.+?)(-->|->)/.exec(token)?.[1] ?? '').trim();
const arrow = token.includes('-->') ? '-->' : '->';
const label = (/--(.+?)(-->|->)/.exec(arrowString)?.[1] ?? '').trim();
const arrow = arrowString.includes('-->') ? '-->' : '->';
this.links.push(new UsecaseLink(sourceNode, targetNode, arrow, label));
}

Expand Down Expand Up @@ -181,6 +187,16 @@ export class UsecaseDB {
return this.systemBoundaries;
}

getUseCases(): string[] {
const useCases = [];
for (const node of this.nodes) {
if (node.nodeType === 'usecase') {
useCases.push(node.id);
}
}
return useCases;
}

getAccDescription = getAccDescription;
getAccTitle = getAccTitle;
getDiagramTitle = getDiagramTitle;
Expand Down Expand Up @@ -241,7 +257,9 @@ export default {
getData: db.getData.bind(db),
getRelationships: db.getRelationships.bind(db),
getDiagramTitle: db.getDiagramTitle.bind(db),
getServices: db.getServices.bind(db),
getSystemBoundaries: db.getSystemBoundaries.bind(db),
getUseCases: db.getUseCases.bind(db),
setAccDescription,
setAccTitle,
setDiagramTitle: db.setDiagramTitle.bind(db),
Expand Down

0 comments on commit 3e5edca

Please sign in to comment.