Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e07f4aa
commit 5b90de6
Showing
7 changed files
with
514 additions
and
15 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { fileURLToPath } from "node:url"; | ||
import { buildNuxtApp, getOutputFiles } from "@/test/utils"; | ||
|
||
const ROOT_DIR = fileURLToPath(new URL("./fixtures/basic", import.meta.url)); | ||
|
||
//TODO: Explore how we can test the generated HTML file | ||
// Need also to investigate if the programmatic build has a different output than the CLI build | ||
describe("nuxt-singlefile", async () => { | ||
await buildNuxtApp({ | ||
rootDir: ROOT_DIR, | ||
}); | ||
|
||
it("builds without throwing an error", async () => { | ||
expect(true).toBe(true); | ||
}); | ||
it("generates an html file", async () => { | ||
const outputFiles = await getOutputFiles({ | ||
rootDir: ROOT_DIR, | ||
}); | ||
|
||
expect(outputFiles).toSatisfy((files: string[]) => | ||
files.some((file) => file.includes("index.html")), | ||
); | ||
}); | ||
it("generates only one html file", async () => { | ||
const outputFiles = await getOutputFiles({ | ||
rootDir: ROOT_DIR, | ||
}); | ||
|
||
expect(outputFiles).toSatisfy( | ||
(files: string[]) => | ||
files.filter((file) => file.includes(".html")).length === 1, | ||
); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
test/src/runtime/utils/generateInlinedStringForTag.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { generateInlinedStringForTag } from "@/src/runtime/utils/generateInlinedStringForTag"; | ||
|
||
describe("generateInlinedStringForTag", () => { | ||
it("should generate an inlined script tag with the given content", () => { | ||
const content = "console.log('Hello, world!');"; | ||
const type = "script"; | ||
|
||
const result = generateInlinedStringForTag(content, type); | ||
|
||
expect(result).toBe(`<script type="module">${content}</script>`); | ||
}); | ||
|
||
it("should generate an inlined style tag with the given content", () => { | ||
const content = "body { background-color: #f0f0f0; }"; | ||
const type = "style"; | ||
|
||
const result = generateInlinedStringForTag(content, type); | ||
|
||
expect(result).toBe(`<style>${content}</style>`); | ||
}); | ||
}); |
130 changes: 130 additions & 0 deletions
130
test/src/runtime/utils/generateSingleFileRenderContext.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { describe, it, expect, vi, afterEach } from "vitest"; | ||
import { generateSingleFileRenderContext } from "@/src/runtime/utils/generateSingleFileRenderContext"; | ||
import type { NuxtRenderHTMLContext } from "nuxt/dist/core/runtime/nitro/renderer"; | ||
|
||
const CORRECT_PATH_TO_FILES = "/path/to/files"; | ||
const FIRST_JS_FILE = "test-file.js"; | ||
const SECOND_JS_FILE = "index2.js"; | ||
const FIRST_CSS_FILE = "index.css"; | ||
const SECOND_CSS_FILE = "test-file2.css"; | ||
|
||
const BASIC_HTML_RENDER_CONTEXT: NuxtRenderHTMLContext = { | ||
island: false, | ||
htmlAttrs: [], | ||
head: [ | ||
`<script src="./${FIRST_JS_FILE}" type="module"></script>`, | ||
`<link rel="stylesheet" href="./${FIRST_CSS_FILE}">`, | ||
], | ||
bodyAttrs: [], | ||
bodyPrepend: [], | ||
body: ["<div></div>"], | ||
bodyAppend: [ | ||
'<script type="application/json"">[{"test":true}]</script>\n<script>window.__NUXT__={};window.__NUXT__.config={testing:true}}</script>', | ||
], | ||
}; | ||
|
||
describe("generateSingleFileRenderContext", () => { | ||
vi.mock("node:fs/promises", async () => ({ | ||
...(await vi.importActual<typeof import("node:fs/promises")>( | ||
"node:fs/promises", | ||
)), | ||
readFile: vi.fn(async (...params: Array<string>) => | ||
params[0].startsWith(CORRECT_PATH_TO_FILES) | ||
? Promise.resolve(`mocked file content for ${params[0]}`) | ||
: Promise.reject(`invalid path: ${params[0]}`), | ||
), | ||
})); | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
it("should generate a new render context with inlined head content", async () => { | ||
const baseHtmlRenderContext = BASIC_HTML_RENDER_CONTEXT; | ||
|
||
const result = await generateSingleFileRenderContext( | ||
baseHtmlRenderContext, | ||
CORRECT_PATH_TO_FILES, | ||
); | ||
|
||
expect(result).toEqual({ | ||
...baseHtmlRenderContext, | ||
head: expect.arrayContaining([ | ||
expect.stringContaining(`<script type="module">`), | ||
expect.stringContaining(`<style>`), | ||
]), | ||
}); | ||
expect(result.head).toEqual( | ||
expect.arrayContaining([ | ||
expect.stringContaining(`/${FIRST_JS_FILE}`), | ||
expect.stringContaining(`/${FIRST_CSS_FILE}`), | ||
]), | ||
); | ||
}); | ||
|
||
it("should generate a new render context with mixed inlined head content", async () => { | ||
const baseHtmlRenderContext = { | ||
...BASIC_HTML_RENDER_CONTEXT, | ||
head: [ | ||
'<meta charset="utf-8">', | ||
'<meta name="viewport" content="width=device-width, initial-scale=1">', | ||
`<script src="./${FIRST_JS_FILE}"></script>`, | ||
`<link rel="stylesheet" href="./${FIRST_CSS_FILE}">`, | ||
`<script src="./${SECOND_JS_FILE}"></script>`, | ||
`<link rel="stylesheet" href="./${SECOND_CSS_FILE}">`, | ||
], | ||
}; | ||
|
||
const result = await generateSingleFileRenderContext( | ||
baseHtmlRenderContext, | ||
CORRECT_PATH_TO_FILES, | ||
); | ||
|
||
expect(result).toEqual({ | ||
...baseHtmlRenderContext, | ||
head: expect.arrayContaining([ | ||
'<meta charset="utf-8">', | ||
'<meta name="viewport" content="width=device-width, initial-scale=1">', | ||
expect.stringContaining(`<script type="module">`), | ||
expect.stringContaining(`<style>`), | ||
expect.stringContaining(`<script type="module">`), | ||
expect.stringContaining(`<style>`), | ||
]), | ||
}); | ||
expect(result.head).toEqual( | ||
expect.arrayContaining([ | ||
expect.stringContaining(`/${FIRST_JS_FILE}`), | ||
expect.stringContaining(`/${FIRST_CSS_FILE}`), | ||
expect.stringContaining(`/${SECOND_JS_FILE}`), | ||
expect.stringContaining(`/${SECOND_CSS_FILE}`), | ||
]), | ||
); | ||
}); | ||
|
||
it("should return the same render context if no tags are found", async () => { | ||
const baseHtmlRenderContext = { | ||
...BASIC_HTML_RENDER_CONTEXT, | ||
head: [ | ||
'<meta charset="utf-8">', | ||
'<meta name="viewport" content="width=device-width, initial-scale=1">', | ||
], | ||
}; | ||
|
||
const result = await generateSingleFileRenderContext( | ||
baseHtmlRenderContext, | ||
CORRECT_PATH_TO_FILES, | ||
); | ||
|
||
expect(result).toEqual(baseHtmlRenderContext); | ||
}); | ||
|
||
it("should throw an error if the path to build is invalid", async () => { | ||
const pathToBuild = "invalid/path/to/build"; | ||
|
||
const baseHtmlRenderContext = BASIC_HTML_RENDER_CONTEXT; | ||
|
||
await expect( | ||
generateSingleFileRenderContext(baseHtmlRenderContext, pathToBuild), | ||
).rejects.toThrow(expect.stringContaining(pathToBuild)); | ||
}); | ||
}); |
Oops, something went wrong.