|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { HeadTanstackRouterAdapter } from '../tanstack-router-adapter'; |
| 3 | +import type { HeadElement } from '../../types'; |
| 4 | + |
| 5 | +describe('HeadTanstackRouterAdapter', () => { |
| 6 | + const adapter = new HeadTanstackRouterAdapter(); |
| 7 | + |
| 8 | + describe('transform', () => { |
| 9 | + it('should return empty arrays when given empty elements', () => { |
| 10 | + const result = adapter.transform([]); |
| 11 | + expect(result).toEqual({ |
| 12 | + meta: [], |
| 13 | + links: [], |
| 14 | + scripts: [], |
| 15 | + styles: [], |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should transform meta element', () => { |
| 20 | + const elements: HeadElement[] = [ |
| 21 | + { |
| 22 | + type: 'meta', |
| 23 | + attributes: { name: 'viewport', content: 'width=device-width' }, |
| 24 | + }, |
| 25 | + ]; |
| 26 | + |
| 27 | + const result = adapter.transform(elements); |
| 28 | + |
| 29 | + expect(result.meta).toHaveLength(1); |
| 30 | + expect(result.meta?.[0]).toEqual({ |
| 31 | + name: 'viewport', |
| 32 | + content: 'width=device-width', |
| 33 | + }); |
| 34 | + expect(result.links).toHaveLength(0); |
| 35 | + expect(result.scripts).toHaveLength(0); |
| 36 | + expect(result.styles).toHaveLength(0); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should transform link element', () => { |
| 40 | + const elements: HeadElement[] = [ |
| 41 | + { |
| 42 | + type: 'link', |
| 43 | + attributes: { rel: 'icon', href: '/favicon.ico' }, |
| 44 | + }, |
| 45 | + ]; |
| 46 | + |
| 47 | + const result = adapter.transform(elements); |
| 48 | + |
| 49 | + expect(result.links).toHaveLength(1); |
| 50 | + expect(result.links?.[0]).toEqual({ rel: 'icon', href: '/favicon.ico' }); |
| 51 | + expect(result.meta).toHaveLength(0); |
| 52 | + expect(result.scripts).toHaveLength(0); |
| 53 | + expect(result.styles).toHaveLength(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should transform script element', () => { |
| 57 | + const elements: HeadElement[] = [ |
| 58 | + { |
| 59 | + type: 'script', |
| 60 | + attributes: { src: '/script.js', async: true }, |
| 61 | + }, |
| 62 | + ]; |
| 63 | + |
| 64 | + const result = adapter.transform(elements); |
| 65 | + |
| 66 | + expect(result.scripts).toHaveLength(1); |
| 67 | + expect(result.scripts?.[0]).toEqual({ src: '/script.js', async: true }); |
| 68 | + expect(result.meta).toHaveLength(0); |
| 69 | + expect(result.links).toHaveLength(0); |
| 70 | + expect(result.styles).toHaveLength(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should transform style element', () => { |
| 74 | + const elements: HeadElement[] = [ |
| 75 | + { |
| 76 | + type: 'style', |
| 77 | + attributes: { children: 'body { margin: 0; }' }, |
| 78 | + }, |
| 79 | + ]; |
| 80 | + |
| 81 | + const result = adapter.transform(elements); |
| 82 | + |
| 83 | + expect(result.styles).toHaveLength(1); |
| 84 | + expect(result.styles?.[0]).toEqual({ children: 'body { margin: 0; }' }); |
| 85 | + expect(result.meta).toHaveLength(0); |
| 86 | + expect(result.links).toHaveLength(0); |
| 87 | + expect(result.scripts).toHaveLength(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should transform title element into meta with title property', () => { |
| 91 | + const elements: HeadElement[] = [ |
| 92 | + { |
| 93 | + type: 'title', |
| 94 | + attributes: { children: 'My Page' }, |
| 95 | + }, |
| 96 | + ]; |
| 97 | + |
| 98 | + const result = adapter.transform(elements); |
| 99 | + |
| 100 | + expect(result.meta).toHaveLength(1); |
| 101 | + expect(result.meta?.[0]).toEqual({ title: 'My Page' }); |
| 102 | + expect(result.links).toHaveLength(0); |
| 103 | + expect(result.scripts).toHaveLength(0); |
| 104 | + expect(result.styles).toHaveLength(0); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should transform multiple elements into categorized configuration', () => { |
| 108 | + const elements: HeadElement[] = [ |
| 109 | + { |
| 110 | + type: 'title', |
| 111 | + attributes: { children: 'My Page' }, |
| 112 | + }, |
| 113 | + { |
| 114 | + type: 'meta', |
| 115 | + attributes: { name: 'description', content: 'A description' }, |
| 116 | + }, |
| 117 | + { |
| 118 | + type: 'meta', |
| 119 | + attributes: { name: 'viewport', content: 'width=device-width' }, |
| 120 | + }, |
| 121 | + { |
| 122 | + type: 'link', |
| 123 | + attributes: { rel: 'icon', href: '/favicon.ico' }, |
| 124 | + }, |
| 125 | + { |
| 126 | + type: 'link', |
| 127 | + attributes: { rel: 'stylesheet', href: '/styles.css' }, |
| 128 | + }, |
| 129 | + { |
| 130 | + type: 'script', |
| 131 | + attributes: { src: '/script.js', async: true }, |
| 132 | + }, |
| 133 | + { |
| 134 | + type: 'script', |
| 135 | + attributes: { children: 'console.log("Hello World!");', async: true }, |
| 136 | + }, |
| 137 | + { |
| 138 | + type: 'style', |
| 139 | + attributes: { children: 'body { margin: 0; }' }, |
| 140 | + }, |
| 141 | + ]; |
| 142 | + |
| 143 | + const result = adapter.transform(elements); |
| 144 | + |
| 145 | + expect(result.meta).toHaveLength(3); |
| 146 | + expect(result.meta?.[0]).toEqual({ title: 'My Page' }); |
| 147 | + expect(result.meta?.[1]).toEqual({ |
| 148 | + name: 'description', |
| 149 | + content: 'A description', |
| 150 | + }); |
| 151 | + expect(result.meta?.[2]).toEqual({ |
| 152 | + name: 'viewport', |
| 153 | + content: 'width=device-width', |
| 154 | + }); |
| 155 | + |
| 156 | + expect(result.links).toHaveLength(2); |
| 157 | + expect(result.links?.[0]).toEqual({ rel: 'icon', href: '/favicon.ico' }); |
| 158 | + expect(result.links?.[1]).toEqual({ |
| 159 | + rel: 'stylesheet', |
| 160 | + href: '/styles.css', |
| 161 | + }); |
| 162 | + |
| 163 | + expect(result.scripts).toHaveLength(2); |
| 164 | + expect(result.scripts?.[0]).toEqual({ src: '/script.js', async: true }); |
| 165 | + expect(result.scripts?.[1]).toEqual({ |
| 166 | + children: 'console.log("Hello World!");', |
| 167 | + async: true, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.styles).toHaveLength(1); |
| 171 | + expect(result.styles?.[0]).toEqual({ children: 'body { margin: 0; }' }); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should ignore unknown element types', () => { |
| 175 | + const elements: HeadElement[] = [ |
| 176 | + { |
| 177 | + type: 'meta', |
| 178 | + attributes: { name: 'description', content: 'A description' }, |
| 179 | + }, |
| 180 | + { |
| 181 | + // oxlint-disable-next-line typescript/no-unsafe-type-assertion |
| 182 | + type: 'base' as any, |
| 183 | + attributes: { href: 'https://devsantara.com' }, |
| 184 | + }, |
| 185 | + { |
| 186 | + type: 'meta', |
| 187 | + attributes: { name: 'viewport', content: 'width=device-width' }, |
| 188 | + }, |
| 189 | + ]; |
| 190 | + |
| 191 | + const result = adapter.transform(elements); |
| 192 | + |
| 193 | + // Only the meta element should be included |
| 194 | + expect(result.meta).toHaveLength(2); |
| 195 | + expect(result.meta?.[0]).toEqual({ |
| 196 | + name: 'description', |
| 197 | + content: 'A description', |
| 198 | + }); |
| 199 | + expect(result.meta?.[1]).toEqual({ |
| 200 | + name: 'viewport', |
| 201 | + content: 'width=device-width', |
| 202 | + }); |
| 203 | + expect(result.links).toHaveLength(0); |
| 204 | + expect(result.scripts).toHaveLength(0); |
| 205 | + expect(result.styles).toHaveLength(0); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments