|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { JSDOM } from 'jsdom' |
| 6 | +import { |
| 7 | + getSasjsTags, |
| 8 | + getScriptTags, |
| 9 | + getModulePreload, |
| 10 | + getStyleInPageTags, |
| 11 | + getStyleTags, |
| 12 | + getFaviconTags, |
| 13 | + getImgTags, |
| 14 | + getSourceTags, |
| 15 | + getManifestTags |
| 16 | +} from '../getTags' |
| 17 | + |
| 18 | +describe('getTags', () => { |
| 19 | + // Helper function to create a JSDOM instance with given HTML |
| 20 | + const createDOM = (html: string) => new JSDOM(`<!DOCTYPE html>${html}`) |
| 21 | + |
| 22 | + describe('getSasjsTags', () => { |
| 23 | + it('should return all sasjs tags', () => { |
| 24 | + const html = ` |
| 25 | + <body> |
| 26 | + <sasjs id="sasjs1"></sasjs> |
| 27 | + <sasjs id="sasjs2"></sasjs> |
| 28 | + <div>Not a sasjs tag</div> |
| 29 | + </body> |
| 30 | + ` |
| 31 | + const dom = createDOM(html) |
| 32 | + const result = getSasjsTags(dom) |
| 33 | + |
| 34 | + expect(result).toHaveLength(2) |
| 35 | + expect(result[0].id).toBe('sasjs1') |
| 36 | + expect(result[1].id).toBe('sasjs2') |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return empty array when no sasjs tags are present', () => { |
| 40 | + const html = '<body><div>No sasjs tags here</div></body>' |
| 41 | + const dom = createDOM(html) |
| 42 | + const result = getSasjsTags(dom) |
| 43 | + |
| 44 | + expect(result).toHaveLength(0) |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + describe('getScriptTags', () => { |
| 49 | + it('should return all script tags', () => { |
| 50 | + const html = ` |
| 51 | + <head> |
| 52 | + <script id="script1" src="script1.js"></script> |
| 53 | + </head> |
| 54 | + <body> |
| 55 | + <script id="script2" src="script2.js"></script> |
| 56 | + <div>Not a script tag</div> |
| 57 | + </body> |
| 58 | + ` |
| 59 | + const dom = createDOM(html) |
| 60 | + const result = getScriptTags(dom) |
| 61 | + |
| 62 | + expect(result).toHaveLength(2) |
| 63 | + expect(result[0].id).toBe('script1') |
| 64 | + expect(result[1].id).toBe('script2') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should return empty array when no script tags are present', () => { |
| 68 | + const html = '<body><div>No script tags here</div></body>' |
| 69 | + const dom = createDOM(html) |
| 70 | + const result = getScriptTags(dom) |
| 71 | + |
| 72 | + expect(result).toHaveLength(0) |
| 73 | + }) |
| 74 | + }) |
| 75 | + |
| 76 | + describe('getModulePreload', () => { |
| 77 | + it('should return all link tags with rel="modulepreload"', () => { |
| 78 | + const html = ` |
| 79 | + <head> |
| 80 | + <link id="module1" rel="modulepreload" href="module1.js"> |
| 81 | + <link id="module2" rel="modulepreload" href="module2.js"> |
| 82 | + <link id="stylesheet" rel="stylesheet" href="style.css"> |
| 83 | + </head> |
| 84 | + ` |
| 85 | + const dom = createDOM(html) |
| 86 | + const result = getModulePreload(dom) |
| 87 | + |
| 88 | + expect(result).toHaveLength(2) |
| 89 | + expect(result[0].id).toBe('module1') |
| 90 | + expect(result[1].id).toBe('module2') |
| 91 | + }) |
| 92 | + |
| 93 | + it('should return empty array when no modulepreload links are present', () => { |
| 94 | + const html = '<head><link rel="stylesheet" href="style.css"></head>' |
| 95 | + const dom = createDOM(html) |
| 96 | + const result = getModulePreload(dom) |
| 97 | + |
| 98 | + expect(result).toHaveLength(0) |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('getStyleInPageTags', () => { |
| 103 | + it('should return all style tags', () => { |
| 104 | + const html = ` |
| 105 | + <head> |
| 106 | + <style id="style1">.class1 { color: red; }</style> |
| 107 | + <style id="style2">.class2 { color: blue; }</style> |
| 108 | + </head> |
| 109 | + <body> |
| 110 | + <div>Not a style tag</div> |
| 111 | + </body> |
| 112 | + ` |
| 113 | + const dom = createDOM(html) |
| 114 | + const result = getStyleInPageTags(dom) |
| 115 | + |
| 116 | + expect(result).toHaveLength(2) |
| 117 | + expect(result[0].id).toBe('style1') |
| 118 | + expect(result[1].id).toBe('style2') |
| 119 | + }) |
| 120 | + |
| 121 | + it('should return empty array when no style tags are present', () => { |
| 122 | + const html = '<body><div>No style tags here</div></body>' |
| 123 | + const dom = createDOM(html) |
| 124 | + const result = getStyleInPageTags(dom) |
| 125 | + |
| 126 | + expect(result).toHaveLength(0) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe('getStyleTags', () => { |
| 131 | + it('should return all link tags with rel="stylesheet"', () => { |
| 132 | + const html = ` |
| 133 | + <head> |
| 134 | + <link id="css1" rel="stylesheet" href="style1.css"> |
| 135 | + <link id="css2" rel="stylesheet" href="style2.css"> |
| 136 | + <link id="icon" rel="icon" href="favicon.ico"> |
| 137 | + </head> |
| 138 | + ` |
| 139 | + const dom = createDOM(html) |
| 140 | + const result = getStyleTags(dom) |
| 141 | + |
| 142 | + expect(result).toHaveLength(2) |
| 143 | + expect(result[0].id).toBe('css1') |
| 144 | + expect(result[1].id).toBe('css2') |
| 145 | + }) |
| 146 | + |
| 147 | + it('should return empty array when no stylesheet links are present', () => { |
| 148 | + const html = '<head><link rel="icon" href="favicon.ico"></head>' |
| 149 | + const dom = createDOM(html) |
| 150 | + const result = getStyleTags(dom) |
| 151 | + |
| 152 | + expect(result).toHaveLength(0) |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + describe('getFaviconTags', () => { |
| 157 | + it('should return all link tags with rel containing "icon"', () => { |
| 158 | + const html = ` |
| 159 | + <head> |
| 160 | + <link id="icon1" rel="icon" href="favicon.ico"> |
| 161 | + <link id="icon2" rel="apple-touch-icon" href="apple-touch-icon.png"> |
| 162 | + <link id="css" rel="stylesheet" href="style.css"> |
| 163 | + </head> |
| 164 | + ` |
| 165 | + const dom = createDOM(html) |
| 166 | + const result = getFaviconTags(dom) |
| 167 | + |
| 168 | + expect(result).toHaveLength(2) |
| 169 | + expect(result[0].id).toBe('icon1') |
| 170 | + expect(result[1].id).toBe('icon2') |
| 171 | + }) |
| 172 | + |
| 173 | + it('should return empty array when no icon links are present', () => { |
| 174 | + const html = '<head><link rel="stylesheet" href="style.css"></head>' |
| 175 | + const dom = createDOM(html) |
| 176 | + const result = getFaviconTags(dom) |
| 177 | + |
| 178 | + expect(result).toHaveLength(0) |
| 179 | + }) |
| 180 | + }) |
| 181 | + |
| 182 | + describe('getImgTags', () => { |
| 183 | + it('should return all img tags with src attribute', () => { |
| 184 | + const html = ` |
| 185 | + <body> |
| 186 | + <img id="img1" src="image1.jpg"> |
| 187 | + <img id="img2" src="image2.jpg"> |
| 188 | + <img id="img3"> |
| 189 | + </body> |
| 190 | + ` |
| 191 | + const dom = createDOM(html) |
| 192 | + const result = getImgTags(dom) |
| 193 | + |
| 194 | + expect(result).toHaveLength(2) |
| 195 | + expect(result[0].id).toBe('img1') |
| 196 | + expect(result[1].id).toBe('img2') |
| 197 | + }) |
| 198 | + |
| 199 | + it('should return empty array when no img tags with src are present', () => { |
| 200 | + const html = '<body><img id="noSrc"></body>' |
| 201 | + const dom = createDOM(html) |
| 202 | + const result = getImgTags(dom) |
| 203 | + |
| 204 | + expect(result).toHaveLength(0) |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + describe('getSourceTags', () => { |
| 209 | + it('should return all source tags with src attribute', () => { |
| 210 | + const html = ` |
| 211 | + <body> |
| 212 | + <audio> |
| 213 | + <source id="source1" src="audio.mp3" type="audio/mpeg"> |
| 214 | + </audio> |
| 215 | + <video> |
| 216 | + <source id="source2" src="video.mp4" type="video/mp4"> |
| 217 | + <source id="source3"> |
| 218 | + </video> |
| 219 | + </body> |
| 220 | + ` |
| 221 | + const dom = createDOM(html) |
| 222 | + const result = getSourceTags(dom) |
| 223 | + |
| 224 | + expect(result).toHaveLength(2) |
| 225 | + expect(result[0].id).toBe('source1') |
| 226 | + expect(result[1].id).toBe('source2') |
| 227 | + }) |
| 228 | + |
| 229 | + it('should return empty array when no source tags with src are present', () => { |
| 230 | + const html = '<body><source id="noSrc"></body>' |
| 231 | + const dom = createDOM(html) |
| 232 | + const result = getSourceTags(dom) |
| 233 | + |
| 234 | + expect(result).toHaveLength(0) |
| 235 | + }) |
| 236 | + }) |
| 237 | + |
| 238 | + describe('getManifestTags', () => { |
| 239 | + it('should return all link tags with rel="manifest"', () => { |
| 240 | + const html = ` |
| 241 | + <head> |
| 242 | + <link id="manifest1" rel="manifest" href="manifest.json"> |
| 243 | + <link id="manifest2" rel="manifest" href="another-manifest.json"> |
| 244 | + <link id="css" rel="stylesheet" href="style.css"> |
| 245 | + </head> |
| 246 | + ` |
| 247 | + const dom = createDOM(html) |
| 248 | + const result = getManifestTags(dom) |
| 249 | + |
| 250 | + expect(result).toHaveLength(2) |
| 251 | + expect(result[0].id).toBe('manifest1') |
| 252 | + expect(result[1].id).toBe('manifest2') |
| 253 | + }) |
| 254 | + |
| 255 | + it('should return empty array when no manifest links are present', () => { |
| 256 | + const html = '<head><link rel="stylesheet" href="style.css"></head>' |
| 257 | + const dom = createDOM(html) |
| 258 | + const result = getManifestTags(dom) |
| 259 | + |
| 260 | + expect(result).toHaveLength(0) |
| 261 | + }) |
| 262 | + }) |
| 263 | +}) |
0 commit comments