forked from feat-agency/vite-plugin-webfont-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex-html-processor.test.ts
More file actions
69 lines (45 loc) · 2.59 KB
/
Copy pathindex-html-processor.test.ts
File metadata and controls
69 lines (45 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { readFileSync } from 'fs';
import { IndexHtmlProcessor } from 'src/index-html-processor';
import { describe, expect, it } from 'vitest';
describe('index.html processor', () => {
it('should find Google Fonts', () => {
const html = readFileSync(__dirname + '/fixtures/index-google-fonts.html').toString();
const fonts = (new IndexHtmlProcessor()).parse(html);
expect(fonts.size).eq(1);
expect(fonts.has('https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap')).toBeTruthy();
});
it('should find Bunny Fonts', () => {
const html = readFileSync(__dirname + '/fixtures/index-bunny-fonts.html').toString();
const fonts = (new IndexHtmlProcessor()).parse(html);
expect(fonts.size).eq(1);
expect(fonts.has('https://fonts.bunny.net/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap')).toBeTruthy();
});
it('should not find any fonts', () => {
const html = readFileSync(__dirname + '/fixtures/index-no-fonts.html').toString();
const fonts = (new IndexHtmlProcessor()).parse(html);
expect(fonts.size).eq(0);
});
it('should remove Google Fonts tags', () => {
const htmlBefore = readFileSync(__dirname + '/fixtures/index-google-fonts.html').toString();
const htmlAfter = (new IndexHtmlProcessor()).removeTags(htmlBefore);
const preconnectTag1 = '<link rel="preconnect" href="https://fonts.googleapis.com">';
const preconnectTag2 = '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>';
const stylesheetTag = '<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap" rel="stylesheet">';
expect(htmlBefore).contains(preconnectTag1);
expect(htmlAfter).not.contains(preconnectTag1);
expect(htmlBefore).contains(preconnectTag2);
expect(htmlAfter).not.contains(preconnectTag2);
expect(htmlBefore).contains(stylesheetTag);
expect(htmlAfter).not.contains(stylesheetTag);
});
it('should remove Bunny Fonts tags', () => {
const htmlBefore = readFileSync(__dirname + '/fixtures/index-bunny-fonts.html').toString();
const htmlAfter = (new IndexHtmlProcessor()).removeTags(htmlBefore);
const preconnectTag = '<link rel="preconnect" href="https://fonts.bunny.net">';
const stylesheetTag = '<link href="https://fonts.bunny.net/css2?family=Roboto+Mono:wght@300;700&family=Roboto:wght@300;700&display=swap" rel="stylesheet" />';
expect(htmlBefore).contains(preconnectTag);
expect(htmlAfter).not.contains(preconnectTag);
expect(htmlBefore).contains(stylesheetTag);
expect(htmlAfter).not.contains(stylesheetTag);
});
});