Skip to content

Commit 31acc7c

Browse files
committed
refactor(detector): extracted html report builder
1 parent a766f8b commit 31acc7c

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types';
2+
import { HtmlOutput } from '../src/reporters/Output';
3+
import { readFileSync } from 'fs';
4+
5+
export async function buildEmptyHtmlReportForTestSmells(exportsOptions: ExportOptions, filePath: string) {
6+
const smellsFound: SmellsList[] = [];
7+
const aggregatedData: AggregatedData = { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0 };
8+
9+
const reporter = new HtmlOutput();
10+
await reporter.writeTo(aggregatedData, exportsOptions);
11+
12+
const generatedHtml = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
13+
return generatedHtml;
14+
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { test, describe, expect, afterEach, beforeEach } from 'vitest';
2-
import { exec } from "child_process";
3-
import { promisify } from "util";
42
import { parse } from 'node-html-parser';
5-
import { SmellsBuilder } from '../src/smells-builder';
6-
import { SmellsAggreagtor } from '../src/reporters/SmellsAgreggator';
7-
import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types';
8-
import { Smell, SmellType, SupportedLanguages } from '../src/types';
9-
import { HtmlOutput } from '../src/reporters/Output';
10-
import { readFileSync, rmSync } from 'fs';
3+
import { ExportOptions } from '../src/reporters/types';
4+
import { rmSync } from 'fs';
5+
import { buildEmptyHtmlReportForTestSmells } from './html-report-builder';
116

127
describe('html report', () => {
138
const exportsOptions: ExportOptions = { to: '.' };
@@ -22,43 +17,33 @@ describe('html report', () => {
2217
});
2318

2419
test('renders empty table when no tests are found', async () => {
25-
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
20+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
2621
const root = parse(generatedHtml);
2722

2823
expect(root.querySelectorAll('table tbody tr').length).toEqual(0);
2924
});
3025

3126
test('renders report title', async () => {
32-
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
27+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
3328
const root = parse(generatedHtml);
3429

3530
expect(root.querySelector('[data-testid="report-title"]')?.textContent).toEqual('Test smells report');
3631
});
3732

3833
test('renders total number of smells found', async () => {
39-
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
34+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
4035
const root = parse(generatedHtml);
4136

4237
expect(root.querySelector('[data-testid="total-smells-found"]')?.textContent).toEqual('0');
4338
expect(root.querySelector('[data-testid="title-smells-found"]')?.textContent).toEqual('Test smells');
4439
});
4540

4641
test('renders the number of test files', async () => {
47-
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
42+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
4843
const root = parse(generatedHtml);
4944

5045
expect(root.querySelector('[data-testid="total-test-files"]')?.textContent).toEqual('0');
5146
expect(root.querySelector('[data-testid="title-test-files"]')?.textContent).toEqual('Test files');
5247
});
5348
});
5449

55-
async function buildHtmlReportForTestSmells(exportsOptions: ExportOptions, filePath: string) {
56-
const smellsFound: SmellsList[] = [];
57-
const aggregatedData: AggregatedData = { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0 };
58-
59-
const reporter = new HtmlOutput();
60-
await reporter.writeTo(aggregatedData, exportsOptions);
61-
62-
const generatedHtml = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
63-
return generatedHtml;
64-
}

0 commit comments

Comments
 (0)