|
| 1 | +import { parseTemplate } from '../src/parser/parseTemplate.js'; |
| 2 | +import { TemplateParser } from '../src/parser/TemplateParser.js'; |
| 3 | +import type { StyleInlineItem, StyleItem } from '../src/types.js'; |
| 4 | +import type { ScriptItem } from '../src/types.js'; |
| 5 | +import type { ScriptInlineItem } from '../src/types.js'; |
| 6 | + |
| 7 | +describe('parseTemplate', () => { |
| 8 | + it('should create parser with empty options', () => { |
| 9 | + const parser = parseTemplate('<html><head></head><body></body></html>'); |
| 10 | + expect(parser).toBeInstanceOf(TemplateParser); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should update title when provided', () => { |
| 14 | + const parser = parseTemplate('<html><head></head></html>', { |
| 15 | + title: 'Test Title', |
| 16 | + }) |
| 17 | + .upsertTitleTag('Test Title') |
| 18 | + .upsertTitleTag('Test Title 2'); |
| 19 | + const result = parser.serialize(); |
| 20 | + expect(result).toContain('<title>Test Title 2</title>'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should corrent upsert body script by order', () => { |
| 24 | + const parser = parseTemplate('<html><body></body></html>', { |
| 25 | + bodyScripts: [ |
| 26 | + { id: 'script1', src: 'script1.js', position: 'beginning', order: 1 }, |
| 27 | + { id: 'script2', src: 'script2.js', position: 'beginning', order: 2 }, |
| 28 | + ], |
| 29 | + }); |
| 30 | + const result = parser |
| 31 | + .upsertBodyScripts([ |
| 32 | + { id: 'script2', src: 'script2.js', position: 'beginning', order: 2 }, |
| 33 | + ]) |
| 34 | + .upsertBodyScripts([ |
| 35 | + { id: 'script2', src: 'script2.js', position: 'beginning', order: 2 }, |
| 36 | + ]) |
| 37 | + .serialize(); |
| 38 | + expect(result).toContain('<script id="script1" src="script1.js"></script>'); |
| 39 | + expect(result).toContain('<script id="script2" src="script2.js"></script>'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should update favicon when provided', () => { |
| 43 | + const parser = parseTemplate('<html><head></head></html>', { |
| 44 | + favicon: { |
| 45 | + href: '/favicon.ico', |
| 46 | + rel: 'icon', |
| 47 | + attributes: { |
| 48 | + sizes: '32x32', |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + expect(parser.serialize()).toContain( |
| 53 | + '<link rel="icon" href="/favicon.ico" sizes="32x32">' |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should update meta tags when provided', () => { |
| 58 | + const metaTags = ['<meta name="description" content="Test">']; |
| 59 | + const parser = parseTemplate('<html><head></head></html>', { |
| 60 | + headMetaTags: metaTags, |
| 61 | + }); |
| 62 | + expect(parser.serialize()).toContain( |
| 63 | + '<meta name="description" content="Test">' |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should update head styles when provided', () => { |
| 68 | + const styles: StyleItem[] = [ |
| 69 | + { |
| 70 | + href: 'style.css', |
| 71 | + id: 'style1', |
| 72 | + position: 'beginning', |
| 73 | + }, |
| 74 | + ]; |
| 75 | + const parser = parseTemplate('<html><head></head></html>', { |
| 76 | + headStyles: styles, |
| 77 | + }); |
| 78 | + expect(parser.serialize()).toContain( |
| 79 | + '<link rel="stylesheet" href="style.css" id="style1">' |
| 80 | + ); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should update inline styles when provided', () => { |
| 84 | + const inlineStyles: StyleInlineItem[] = [ |
| 85 | + { |
| 86 | + content: 'body {}', |
| 87 | + id: 'style1', |
| 88 | + position: 'beginning', |
| 89 | + }, |
| 90 | + ]; |
| 91 | + const parser = parseTemplate('<html><head></head></html>', { |
| 92 | + headInlineStyles: inlineStyles, |
| 93 | + }); |
| 94 | + expect(parser.serialize()).toContain('<style id="style1">body {}</style>'); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should update head scripts when provided', () => { |
| 98 | + const scripts: ScriptItem[] = [ |
| 99 | + { |
| 100 | + src: 'script.js', |
| 101 | + id: 'script1', |
| 102 | + position: 'beginning', |
| 103 | + }, |
| 104 | + ]; |
| 105 | + const parser = parseTemplate('<html><head></head></html>', { |
| 106 | + headScripts: scripts, |
| 107 | + }); |
| 108 | + expect(parser.serialize()).toContain( |
| 109 | + '<script id="script1" src="script.js"></script>' |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should update head inline scripts when provided', () => { |
| 114 | + const inlineScripts: ScriptInlineItem[] = [ |
| 115 | + { |
| 116 | + content: 'console.log()', |
| 117 | + id: 'script1', |
| 118 | + position: 'beginning', |
| 119 | + }, |
| 120 | + ]; |
| 121 | + const parser = parseTemplate('<html><head></head></html>', { |
| 122 | + headInlineScripts: inlineScripts, |
| 123 | + }); |
| 124 | + expect(parser.serialize()).toContain( |
| 125 | + '<script id="script1">console.log()</script>' |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should update body scripts when provided', () => { |
| 130 | + const bodyScripts: ScriptItem[] = [ |
| 131 | + { |
| 132 | + src: 'script.js', |
| 133 | + id: 'script1', |
| 134 | + position: 'beginning', |
| 135 | + }, |
| 136 | + ]; |
| 137 | + const parser = parseTemplate('<html><head></head><body></body></html>', { |
| 138 | + bodyScripts: bodyScripts, |
| 139 | + }); |
| 140 | + expect(parser.serialize()).toContain( |
| 141 | + '<script id="script1" src="script.js"></script>' |
| 142 | + ); |
| 143 | + }); |
| 144 | +}); |
0 commit comments