Skip to content

Commit 5f93aa6

Browse files
s1gr1dAbhiPrasad
andauthored
test(nuxt): Add code snippet unit tests (#12696)
Adding tests for #12681 --------- Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 31c1158 commit 5f93aa6

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

packages/nuxt/src/common/snippets.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4-
// todo: tests
54
/** Returns an import snippet */
65
export function buildSdkInitFileImportSnippet(filePath: string): string {
76
const posixPath = filePath.split(path.sep).join(path.posix.sep);
87

9-
return `import "${posixPath}";`;
8+
// normalize to forward slashed for Windows-based systems
9+
const normalizedPath = posixPath.replace(/\\/g, '/');
10+
11+
return `import '${normalizedPath}';`;
1012
}
1113

12-
// todo: tests
13-
/** Adds an import statement right after <script setup> */
14+
/**
15+
* Adds a top-level import statement right after <script setup>.
16+
* This should happen as early as possible (e.g. in root component)
17+
*/
1418
export function addImportStatement(filePath: string, importStatement: string): void {
1519
try {
1620
const data = fs.readFileSync(filePath, 'utf8');
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as fs from 'fs';
2+
3+
import * as path from 'path';
4+
import { beforeEach, describe, expect, it, vi } from 'vitest';
5+
6+
import { addImportStatement, buildSdkInitFileImportSnippet } from '../../src/common/snippets';
7+
8+
describe('Nuxt Code Snippets', () => {
9+
describe('buildSdkInitFileImportSnippet', () => {
10+
it('returns correct import statement for POSIX path', () => {
11+
const filePath = 'src/myFile.ts';
12+
const expectedOutput = "import 'src/myFile.ts';";
13+
const result = buildSdkInitFileImportSnippet(filePath);
14+
expect(result).toBe(expectedOutput);
15+
});
16+
17+
it('returns correct import statement for Windows path', () => {
18+
const filePath = 'src\\myFile.ts';
19+
const expectedOutput = "import 'src/myFile.ts';";
20+
const result = buildSdkInitFileImportSnippet(filePath);
21+
expect(result).toBe(expectedOutput);
22+
});
23+
24+
it('returns correct import statement for path with multiple segments', () => {
25+
const filePath = path.join('src', 'myDir', 'myFile.ts');
26+
const expectedOutput = "import 'src/myDir/myFile.ts';";
27+
const result = buildSdkInitFileImportSnippet(filePath);
28+
expect(result).toBe(expectedOutput);
29+
});
30+
});
31+
32+
describe('addImportStatement', () => {
33+
vi.mock('fs');
34+
35+
beforeEach(() => {
36+
vi.clearAllMocks();
37+
});
38+
39+
it('should add import statement to file', () => {
40+
const readFileSyncSpy = vi.spyOn(fs, 'readFileSync');
41+
const writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync');
42+
43+
const filePath = 'testFile.ts';
44+
const importStatement = 'import { test } from "./test";';
45+
const fileContent =
46+
'<template>\n' +
47+
' <Suspense @resolve="onResolve">\n' +
48+
' </Suspense>\n' +
49+
'</template>\n' +
50+
'\n' +
51+
'<script setup>\n' +
52+
"import { provide } from 'vue'\n" +
53+
"import { useNuxtApp } from '../nuxt'";
54+
55+
const fileContentExpected =
56+
'<template>\n' +
57+
' <Suspense @resolve="onResolve">\n' +
58+
' </Suspense>\n' +
59+
'</template>\n' +
60+
'\n' +
61+
'<script setup>\n' +
62+
`${importStatement}\n` +
63+
"import { provide } from 'vue'\n" +
64+
"import { useNuxtApp } from '../nuxt'";
65+
66+
readFileSyncSpy.mockReturnValue(fileContent);
67+
writeFileSyncSpy.mockImplementation(() => {});
68+
69+
addImportStatement(filePath, importStatement);
70+
71+
expect(fs.readFileSync).toHaveBeenCalledWith(filePath, 'utf8');
72+
expect(fs.writeFileSync).toHaveBeenCalledWith(filePath, fileContentExpected, 'utf8');
73+
});
74+
});
75+
});

0 commit comments

Comments
 (0)