1
1
import * as fs from 'fs'
2
2
import { describe , it , expect , vi , beforeEach } from 'vitest'
3
- // @ts -ignore
4
- import parse5utils from 'parse5-utils'
5
3
import { getAssetsFromHtml } from '../../html-lib/utils'
6
4
7
5
vi . mock ( 'fs' , ( ) => ( {
8
6
readFileSync : vi . fn ( )
9
7
} ) )
10
8
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
-
31
9
describe ( 'getAssetsFromHtml' , ( ) => {
32
10
const htmlFilePath = '/path/to/index.html'
33
11
@@ -39,16 +17,14 @@ describe('getAssetsFromHtml', () => {
39
17
const htmlContent =
40
18
'<html><head><link rel="stylesheet" href="styles.css"></head></html>'
41
19
; ( fs . readFileSync as any ) . mockReturnValue ( 'This should not be read' )
42
- setupParseMock ( createMockHtmlDocument ( [ ] ) )
43
20
44
- getAssetsFromHtml ( htmlFilePath , htmlContent )
21
+ const assets = getAssetsFromHtml ( htmlFilePath , htmlContent )
45
22
46
23
expect ( fs . readFileSync ) . not . toHaveBeenCalled ( )
47
- expect ( parse5utils . parse ) . toHaveBeenCalledWith ( htmlContent )
24
+ expect ( assets ?. css ) . toEqual ( [ '/path/to/styles.css' ] )
48
25
} )
49
26
50
27
it ( 'should return empty arrays if HTML has no assets' , ( ) => {
51
- setupParseMock ( createMockHtmlDocument ( [ ] ) )
52
28
; ( fs . readFileSync as any ) . mockReturnValue ( '<html></html>' )
53
29
54
30
const assets = getAssetsFromHtml ( htmlFilePath )
@@ -61,17 +37,53 @@ describe('getAssetsFromHtml', () => {
61
37
it ( 'should ignore external URLs for assets' , ( ) => {
62
38
const htmlContent =
63
39
'<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
- )
72
40
73
41
const assets = getAssetsFromHtml ( htmlFilePath , htmlContent )
74
42
75
43
expect ( assets ?. js ) . toEqual ( [ ] )
76
44
} )
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
+ } )
77
89
} )
0 commit comments