Skip to content

Commit a766f8b

Browse files
committed
chore(detector): html generation test cases
1 parent c8ba76e commit a766f8b

File tree

4 files changed

+204
-5
lines changed

4 files changed

+204
-5
lines changed

detector/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@typescript-eslint/eslint-plugin": "^5.53.0",
3131
"@typescript-eslint/parser": "^5.53.0",
3232
"eslint": "^8.34.0",
33+
"node-html-parser": "^6.1.13",
3334
"nodemon": "^3.1.4",
3435
"ts-loader": "*",
3536
"ts-node": "^10.9.2"

detector/src/reporters/layout/example.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<div class="container mx-auto p-5">
2626

2727
<div class="flex justify-between items-center py-5">
28-
<h1 class="text-3xl text-gray-700 dark:text-gray-400">Test smells report</h1>
28+
<h1 class="text-3xl text-gray-700 dark:text-gray-400" data-testid="report-title">Test smells report</h1>
2929

3030
<button id="theme-toggle" type="button"
3131
class="text-gray-500 dark:text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700 focus:outline-none focus:ring-4 focus:ring-gray-200 dark:focus:ring-gray-700 rounded-lg text-sm p-2.5">
@@ -45,14 +45,14 @@ <h1 class="text-3xl text-gray-700 dark:text-gray-400">Test smells report</h1>
4545
<div
4646
class="mb-5 max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700">
4747

48-
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">{{ totalSmells }}</h5>
49-
<p class="font-normal text-gray-700 dark:text-gray-400">Test smells</p>
48+
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white" data-testid="total-smells-found">{{ totalSmells }}</h5>
49+
<p class="font-normal text-gray-700 dark:text-gray-400" data-testid="title-smells-found">Test smells</p>
5050
</div>
5151
<div
5252
class="mb-5 max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700">
5353

54-
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">{{ data.length }}</h5>
55-
<p class="font-normal text-gray-700 dark:text-gray-400">Test cases</p>
54+
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white" data-testid="total-test-files">{{ data.length }}</h5>
55+
<p class="font-normal text-gray-700 dark:text-gray-400" data-testid="title-test-files">Test files</p>
5656
</div>
5757
</div>
5858
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { test, describe, expect, afterEach, beforeEach } from 'vitest';
2+
import { exec } from "child_process";
3+
import { promisify } from "util";
4+
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';
11+
12+
describe('html report', () => {
13+
const exportsOptions: ExportOptions = { to: '.' };
14+
const filePath = `${exportsOptions.to}/smelly-report.html`;
15+
16+
beforeEach(() => {
17+
rmSync(filePath, { force: true });
18+
});
19+
20+
afterEach(() => {
21+
rmSync(filePath, { force: true });
22+
});
23+
24+
test('renders empty table when no tests are found', async () => {
25+
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
26+
const root = parse(generatedHtml);
27+
28+
expect(root.querySelectorAll('table tbody tr').length).toEqual(0);
29+
});
30+
31+
test('renders report title', async () => {
32+
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
33+
const root = parse(generatedHtml);
34+
35+
expect(root.querySelector('[data-testid="report-title"]')?.textContent).toEqual('Test smells report');
36+
});
37+
38+
test('renders total number of smells found', async () => {
39+
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
40+
const root = parse(generatedHtml);
41+
42+
expect(root.querySelector('[data-testid="total-smells-found"]')?.textContent).toEqual('0');
43+
expect(root.querySelector('[data-testid="title-smells-found"]')?.textContent).toEqual('Test smells');
44+
});
45+
46+
test('renders the number of test files', async () => {
47+
const generatedHtml = await buildHtmlReportForTestSmells(exportsOptions, filePath);
48+
const root = parse(generatedHtml);
49+
50+
expect(root.querySelector('[data-testid="total-test-files"]')?.textContent).toEqual('0');
51+
expect(root.querySelector('[data-testid="title-test-files"]')?.textContent).toEqual('Test files');
52+
});
53+
});
54+
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+
}

package-lock.json

Lines changed: 134 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)