|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { getDevRouteScripts } from '../dist/vite-plugin-routes/index.js'; |
| 4 | + |
| 5 | +describe('getDevRouteScripts', () => { |
| 6 | + it('returns empty array when command is build', () => { |
| 7 | + const scripts = [{ stage: 'page', content: 'console.log("page")' }]; |
| 8 | + const result = getDevRouteScripts('build', scripts); |
| 9 | + assert.deepEqual(result, []); |
| 10 | + }); |
| 11 | + |
| 12 | + it('returns empty array when no scripts are provided in dev mode', () => { |
| 13 | + const result = getDevRouteScripts('dev', []); |
| 14 | + assert.deepEqual(result, []); |
| 15 | + }); |
| 16 | + |
| 17 | + it('includes external page script entry when page-stage scripts exist', () => { |
| 18 | + const scripts = [{ stage: 'page', content: 'import "alpinejs"' }]; |
| 19 | + const result = getDevRouteScripts('dev', scripts); |
| 20 | + |
| 21 | + assert.equal(result.length, 1); |
| 22 | + assert.deepEqual(result[0], { |
| 23 | + type: 'external', |
| 24 | + value: '/@id/astro:scripts/page.js', |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('collapses multiple page scripts into a single external entry', () => { |
| 29 | + const scripts = [ |
| 30 | + { stage: 'page', content: 'import "alpinejs"' }, |
| 31 | + { stage: 'page', content: 'import "other"' }, |
| 32 | + ]; |
| 33 | + const result = getDevRouteScripts('dev', scripts); |
| 34 | + |
| 35 | + const pageEntries = result.filter((s) => 'type' in s && s.type === 'external'); |
| 36 | + assert.equal(pageEntries.length, 1); |
| 37 | + }); |
| 38 | + |
| 39 | + it('includes head-inline scripts with their content', () => { |
| 40 | + const scripts = [{ stage: 'head-inline', content: 'console.log("inline")' }]; |
| 41 | + const result = getDevRouteScripts('dev', scripts); |
| 42 | + |
| 43 | + assert.equal(result.length, 1); |
| 44 | + assert.deepEqual(result[0], { |
| 45 | + stage: 'head-inline', |
| 46 | + children: 'console.log("inline")', |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('includes both page and head-inline scripts together', () => { |
| 51 | + const scripts = [ |
| 52 | + { stage: 'page', content: 'import "alpinejs"' }, |
| 53 | + { stage: 'head-inline', content: 'console.log("inline1")' }, |
| 54 | + { stage: 'head-inline', content: 'console.log("inline2")' }, |
| 55 | + ]; |
| 56 | + const result = getDevRouteScripts('dev', scripts); |
| 57 | + |
| 58 | + assert.equal(result.length, 3); |
| 59 | + assert.deepEqual(result[0], { |
| 60 | + type: 'external', |
| 61 | + value: '/@id/astro:scripts/page.js', |
| 62 | + }); |
| 63 | + assert.deepEqual(result[1], { |
| 64 | + stage: 'head-inline', |
| 65 | + children: 'console.log("inline1")', |
| 66 | + }); |
| 67 | + assert.deepEqual(result[2], { |
| 68 | + stage: 'head-inline', |
| 69 | + children: 'console.log("inline2")', |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('ignores before-hydration and page-ssr stage scripts', () => { |
| 74 | + const scripts = [ |
| 75 | + { stage: 'before-hydration', content: 'console.log("hydration")' }, |
| 76 | + { stage: 'page-ssr', content: 'console.log("ssr")' }, |
| 77 | + ]; |
| 78 | + const result = getDevRouteScripts('dev', scripts); |
| 79 | + |
| 80 | + assert.deepEqual(result, []); |
| 81 | + }); |
| 82 | + |
| 83 | + it('ignores non-relevant stages while still collecting page and head-inline', () => { |
| 84 | + const scripts = [ |
| 85 | + { stage: 'before-hydration', content: 'console.log("hydration")' }, |
| 86 | + { stage: 'page', content: 'import "alpinejs"' }, |
| 87 | + { stage: 'page-ssr', content: 'console.log("ssr")' }, |
| 88 | + { stage: 'head-inline', content: 'window.__config = {}' }, |
| 89 | + ]; |
| 90 | + const result = getDevRouteScripts('dev', scripts); |
| 91 | + |
| 92 | + assert.equal(result.length, 2); |
| 93 | + assert.deepEqual(result[0], { |
| 94 | + type: 'external', |
| 95 | + value: '/@id/astro:scripts/page.js', |
| 96 | + }); |
| 97 | + assert.deepEqual(result[1], { |
| 98 | + stage: 'head-inline', |
| 99 | + children: 'window.__config = {}', |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments