Skip to content

Commit 00bd7b3

Browse files
committed
refactor(detector): test cases for html
1 parent 31acc7c commit 00bd7b3

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

detector/test/html-report-builder.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ export async function buildEmptyHtmlReportForTestSmells(exportsOptions: ExportOp
99
const reporter = new HtmlOutput();
1010
await reporter.writeTo(aggregatedData, exportsOptions);
1111

12-
const generatedHtml = readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
13-
return generatedHtml;
12+
return readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
13+
}
14+
15+
export async function buildHtmlReportForTestSmellsFor(
16+
exportsOptions: ExportOptions,
17+
filePath: string,
18+
aggregatedData: AggregatedData
19+
) {
20+
const reporter = new HtmlOutput();
21+
await reporter.writeTo(aggregatedData, exportsOptions);
22+
23+
return readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
1424
}
Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { test, describe, expect, afterEach, beforeEach } from 'vitest';
22
import { parse } from 'node-html-parser';
3-
import { ExportOptions } from '../src/reporters/types';
3+
import { AggregatedData, ExportOptions, SmellsList } from '../src/reporters/types';
44
import { rmSync } from 'fs';
5-
import { buildEmptyHtmlReportForTestSmells } from './html-report-builder';
5+
import { buildEmptyHtmlReportForTestSmells, buildHtmlReportForTestSmellsFor } from './html-report-builder';
6+
import { SmellsBuilder } from '../src/smells-builder';
7+
import { Smell, SupportedLanguages } from '../src/types';
68

79
describe('html report', () => {
810
const exportsOptions: ExportOptions = { to: '.' };
@@ -16,34 +18,73 @@ describe('html report', () => {
1618
rmSync(filePath, { force: true });
1719
});
1820

19-
test('renders empty table when no tests are found', async () => {
20-
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
21-
const root = parse(generatedHtml);
21+
describe('when no smells are found', () => {
2222

23-
expect(root.querySelectorAll('table tbody tr').length).toEqual(0);
23+
test('renders empty table when no tests are found', async () => {
24+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
25+
const root = parse(generatedHtml);
26+
27+
expect(root.querySelectorAll('table tbody tr').length).toEqual(0);
28+
});
29+
30+
test('renders report title', async () => {
31+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
32+
const root = parse(generatedHtml);
33+
34+
expect(root.querySelector('[data-testid="report-title"]')?.textContent).toEqual('Test smells report');
35+
});
36+
37+
test('renders total number of smells found', async () => {
38+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
39+
const root = parse(generatedHtml);
40+
41+
expect(root.querySelector('[data-testid="total-smells-found"]')?.textContent).toEqual('0');
42+
expect(root.querySelector('[data-testid="title-smells-found"]')?.textContent).toEqual('Test smells');
43+
});
44+
45+
test('renders the number of test files', async () => {
46+
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
47+
const root = parse(generatedHtml);
48+
49+
expect(root.querySelector('[data-testid="total-test-files"]')?.textContent).toEqual('0');
50+
expect(root.querySelector('[data-testid="title-test-files"]')?.textContent).toEqual('Test files');
51+
});
2452
});
2553

26-
test('renders report title', async () => {
27-
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
28-
const root = parse(generatedHtml);
54+
describe('when there are test smells', () => {
55+
const buildAggregatedData = (): AggregatedData => {
56+
const smell: Smell = SmellsBuilder.console(0, 1, 10, 20);
57+
const smellsFound: SmellsList[] = [
58+
{
59+
smells: [smell, smell, smell, smell],
60+
language: SupportedLanguages.javascript,
61+
fileName: 'first_test.js'
62+
},
63+
];
2964

30-
expect(root.querySelector('[data-testid="report-title"]')?.textContent).toEqual('Test smells report');
31-
});
65+
return { data: smellsFound, totalSmells: 0, averageSmellsPerTestFile: 0 };
66+
};
3267

33-
test('renders total number of smells found', async () => {
34-
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
35-
const root = parse(generatedHtml);
68+
test('renders number of test smell for a given file', async () => {
69+
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, buildAggregatedData());
70+
const root = parse(generatedHtml);
3671

37-
expect(root.querySelector('[data-testid="total-smells-found"]')?.textContent).toEqual('0');
38-
expect(root.querySelector('[data-testid="title-smells-found"]')?.textContent).toEqual('Test smells');
39-
});
72+
expect(root.querySelector('table tbody tr')?.textContent).toContain('4');
73+
});
74+
75+
test('renders file name', async () => {
76+
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, buildAggregatedData());
77+
const root = parse(generatedHtml);
78+
79+
expect(root.querySelector('table tbody tr')?.textContent).toContain('first_test.js');
80+
});
4081

41-
test('renders the number of test files', async () => {
42-
const generatedHtml = await buildEmptyHtmlReportForTestSmells(exportsOptions, filePath);
43-
const root = parse(generatedHtml);
82+
test('renders language', async () => {
83+
const generatedHtml = await buildHtmlReportForTestSmellsFor(exportsOptions, filePath, buildAggregatedData());
84+
const root = parse(generatedHtml);
4485

45-
expect(root.querySelector('[data-testid="total-test-files"]')?.textContent).toEqual('0');
46-
expect(root.querySelector('[data-testid="title-test-files"]')?.textContent).toEqual('Test files');
86+
expect(root.querySelector('table tbody tr')?.textContent).toContain('javascript');
87+
});
4788
});
4889
});
4990

0 commit comments

Comments
 (0)