Skip to content

Commit

Permalink
refactor(banira): refactor doc page generation interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schürmann committed Jan 3, 2025
1 parent 61e16a7 commit 292de16
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
17 changes: 16 additions & 1 deletion packages/banira/src/doc-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,29 @@ export class DocGen {
syntaxKind: TSDocTagSyntaxKind.BlockTag
});

constructor() {
public readonly tagName: string;

get src(): string {
return `./dist/${this.tagName}.js`;
}

get title(): string {
return `<${this.tagName}> Component Demo`;
}

constructor(tagName: string = "my-circle") {
this.tagName = tagName;
// Add custom tag definitions to detect demo blocks
this.customConfiguration.addTagDefinitions([
DocGen.CUSTOM_BLOCK_DEFINITION_DEMO
]);
this.tsdocParser = new TSDocParser(this.customConfiguration);
}

fromString(sourceCode: string): ParserContext {
return this.tsdocParser.parseString(sourceCode);
}

/**
* Parses a TypeScript source file to extract its documentation.
*
Expand Down
5 changes: 1 addition & 4 deletions packages/banira/src/formatter/doc-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export class FormatterDocPage {
return this.context.docComment.summarySection
}

createDocPage(): string {
const title = "MyCircle Component Demo";
const src = "../dist/my-circle.js"
const tagName = "my-circle"
createDocPage(tagName: string = "my-circle", src: string = "../dist/my-circle.js", title: string = "MyCircle Component Demo"): string {
return `<!DOCTYPE html>
<html lang="en">
<head>
Expand Down
49 changes: 36 additions & 13 deletions packages/banira/test/doc-gen.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
import { describe, it } from "node:test";
import { describe, it, before } from "node:test";
import assert from "node:assert";
import { DocGen } from '../src/doc-gen';
import { ParserContext } from "@microsoft/tsdoc";

describe('DocGen', () => {
it('should parse a typescript file and return a ParserContext', async () => {
const docGen = new DocGen();
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
assert.ok(result, 'ParserContext should be returned');
let docGen: DocGen;
let context: ParserContext;

before(async () => {
docGen = new DocGen('my-circle');
context = await docGen.parseDoc('./test/fixtures/my-circle.ts');
});

it('should parse a typescript file and return a ParserContext', () => {
assert.ok(context, 'ParserContext should be returned');
});

it('returns the demo tag', async () => {
const docGen = new DocGen();
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
assert.equal(result.docComment.customBlocks.length, 1);
it('returns the demo tag', () => {
assert.equal(context.docComment.customBlocks.length, 1);
});

it('rendered result contains tag', async () => {
const docGen = new DocGen();
const result = await docGen.parseDoc('./test/fixtures/my-circle.ts');
const doc = docGen.renderDocs(result);
it('rendered result contains tag', () => {
const doc = docGen.renderDocs(context);
assert.match(doc, /my-circle/);
});

describe('getters', () => {

it('returns correct path for default tag', () => {
assert.equal(docGen.src, './dist/my-circle.js');
});

it('returns correct path for custom tag', () => {
const customDocGen = new DocGen('custom-element');
assert.equal(customDocGen.src, './dist/custom-element.js');
});
it('returns formatted title for default tag', () => {
assert.equal(docGen.title, '<my-circle> Component Demo');
});

it('returns formatted title for custom tag', () => {
const customDocGen = new DocGen('custom-element');
assert.equal(customDocGen.title, '<custom-element> Component Demo');
});
});
});
6 changes: 3 additions & 3 deletions packages/banira/test/doc.gen.formatter-page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('DocGen Formatter', () => {
let formatter: FormatterDocPage;

before(async () => {
docGen = new DocGen();
docGen = new DocGen("my-circle");
parsed = await docGen.parseDoc('./test/fixtures/my-circle.ts');
formatter = new FormatterDocPage(parsed);
})
Expand All @@ -19,12 +19,12 @@ describe('DocGen Formatter', () => {
});

it('should create a doc page', async () => {
const result = formatter.createDocPage();
const result = formatter.createDocPage(docGen.tagName, docGen.src, docGen.title);
assert.ok(result, 'Doc page should be created');
});

it('contains the demo tag', async () => {
const result = formatter.createDocPage();
const result = formatter.createDocPage(docGen.tagName, docGen.src, docGen.title);
assert.match(result, /<my-circle><\/my-circle>/);
});
});

0 comments on commit 292de16

Please sign in to comment.