Skip to content

Commit def7b9e

Browse files
committed
Several test cases for the HTML plugin
1 parent 04f8c98 commit def7b9e

18 files changed

+2057
-675
lines changed
Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
import * as fs from 'fs'
22
import {describe, it, expect, vi, beforeEach} from 'vitest'
3-
// @ts-ignore
4-
import parse5utils from 'parse5-utils'
53
import {getAssetsFromHtml} from '../../html-lib/utils'
64

75
vi.mock('fs', () => ({
86
readFileSync: vi.fn()
97
}))
108

11-
vi.mock('parse5-utils', () => ({
12-
parse: vi.fn(),
13-
getAttribute: vi.fn()
14-
}))
15-
16-
const setupParseMock = (htmlDocument: any) => {
17-
;(parse5utils.parse as any).mockImplementation(() => htmlDocument)
18-
}
19-
20-
function createMockHtmlDocument(childNodes: any[]): any {
21-
return {
22-
childNodes: [
23-
{
24-
nodeName: 'html',
25-
childNodes
26-
}
27-
]
28-
}
29-
}
30-
319
describe('getAssetsFromHtml', () => {
3210
const htmlFilePath = '/path/to/index.html'
3311

@@ -39,16 +17,14 @@ describe('getAssetsFromHtml', () => {
3917
const htmlContent =
4018
'<html><head><link rel="stylesheet" href="styles.css"></head></html>'
4119
;(fs.readFileSync as any).mockReturnValue('This should not be read')
42-
setupParseMock(createMockHtmlDocument([]))
4320

44-
getAssetsFromHtml(htmlFilePath, htmlContent)
21+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
4522

4623
expect(fs.readFileSync).not.toHaveBeenCalled()
47-
expect(parse5utils.parse).toHaveBeenCalledWith(htmlContent)
24+
expect(assets?.css).toEqual(['/path/to/styles.css'])
4825
})
4926

5027
it('should return empty arrays if HTML has no assets', () => {
51-
setupParseMock(createMockHtmlDocument([]))
5228
;(fs.readFileSync as any).mockReturnValue('<html></html>')
5329

5430
const assets = getAssetsFromHtml(htmlFilePath)
@@ -61,17 +37,53 @@ describe('getAssetsFromHtml', () => {
6137
it('should ignore external URLs for assets', () => {
6238
const htmlContent =
6339
'<html><head><script src="https://cezaraugusto.com/script.js"></script></head></html>'
64-
setupParseMock(
65-
createMockHtmlDocument([
66-
{
67-
nodeName: 'script',
68-
attrs: [{name: 'src', value: 'https://cezaraugusto.com/script.js'}]
69-
}
70-
])
71-
)
7240

7341
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
7442

7543
expect(assets?.js).toEqual([])
7644
})
45+
46+
it('should extract JavaScript files', () => {
47+
const htmlContent =
48+
'<html><head><script src="app.js"></script></head></html>'
49+
50+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
51+
52+
expect(assets?.js).toEqual(['/path/to/app.js'])
53+
})
54+
55+
it('should extract CSS files', () => {
56+
const htmlContent =
57+
'<html><head><link rel="stylesheet" href="styles.css"></head></html>'
58+
59+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
60+
61+
expect(assets?.css).toEqual(['/path/to/styles.css'])
62+
})
63+
64+
it('should extract static assets', () => {
65+
const htmlContent = '<html><body><img src="image.png"></body></html>'
66+
67+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
68+
69+
expect(assets?.static).toEqual(['/path/to/image.png'])
70+
})
71+
72+
it('should handle public paths correctly', () => {
73+
const htmlContent =
74+
'<html><head><link rel="stylesheet" href="/public/styles.css"></head></html>'
75+
76+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent, 'public')
77+
78+
expect(assets?.css).toEqual(['/public/styles.css'])
79+
})
80+
81+
it('should handle relative paths correctly', () => {
82+
const htmlContent =
83+
'<html><head><link rel="stylesheet" href="./styles.css"></head></html>'
84+
85+
const assets = getAssetsFromHtml(htmlFilePath, htmlContent)
86+
87+
expect(assets?.css).toEqual(['/path/to/styles.css'])
88+
})
7789
})

0 commit comments

Comments
 (0)