|
| 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