Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(nuxt): Add code snippet unit tests #12696

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages/nuxt/src/common/snippets.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import * as fs from 'fs';
import * as path from 'path';

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

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

return `import '${normalizedPath}';`;
}

// todo: tests
/** Adds an import statement right after <script setup> */
/**
* Adds a top-level import statement right after <script setup>.
* This should happen as early as possible (e.g. in root component)
*/
export function addImportStatement(filePath: string, importStatement: string): void {
try {
const data = fs.readFileSync(filePath, 'utf8');
Expand Down
75 changes: 75 additions & 0 deletions packages/nuxt/test/common/snippets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as fs from 'fs';

import * as path from 'path';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { addImportStatement, buildSdkInitFileImportSnippet } from '../../src/common/snippets';

describe('Nuxt Code Snippets', () => {
describe('buildSdkInitFileImportSnippet', () => {
it('returns correct import statement for POSIX path', () => {
const filePath = 'src/myFile.ts';
const expectedOutput = "import 'src/myFile.ts';";
const result = buildSdkInitFileImportSnippet(filePath);
expect(result).toBe(expectedOutput);
});

it('returns correct import statement for Windows path', () => {
const filePath = 'src\\myFile.ts';
const expectedOutput = "import 'src/myFile.ts';";
const result = buildSdkInitFileImportSnippet(filePath);
expect(result).toBe(expectedOutput);
});

it('returns correct import statement for path with multiple segments', () => {
const filePath = path.join('src', 'myDir', 'myFile.ts');
const expectedOutput = "import 'src/myDir/myFile.ts';";
const result = buildSdkInitFileImportSnippet(filePath);
expect(result).toBe(expectedOutput);
});
});

describe('addImportStatement', () => {
vi.mock('fs');

beforeEach(() => {
vi.clearAllMocks();
});

it('should add import statement to file', () => {
const readFileSyncSpy = vi.spyOn(fs, 'readFileSync');
const writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync');

const filePath = 'testFile.ts';
const importStatement = 'import { test } from "./test";';
const fileContent =
'<template>\n' +
' <Suspense @resolve="onResolve">\n' +
' </Suspense>\n' +
'</template>\n' +
'\n' +
'<script setup>\n' +
"import { provide } from 'vue'\n" +
"import { useNuxtApp } from '../nuxt'";

const fileContentExpected =
'<template>\n' +
' <Suspense @resolve="onResolve">\n' +
' </Suspense>\n' +
'</template>\n' +
'\n' +
'<script setup>\n' +
`${importStatement}\n` +
"import { provide } from 'vue'\n" +
"import { useNuxtApp } from '../nuxt'";

readFileSyncSpy.mockReturnValue(fileContent);
writeFileSyncSpy.mockImplementation(() => {});

addImportStatement(filePath, importStatement);

expect(fs.readFileSync).toHaveBeenCalledWith(filePath, 'utf8');
expect(fs.writeFileSync).toHaveBeenCalledWith(filePath, fileContentExpected, 'utf8');
});
});
});
Loading