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

Add tests for uniforms-cli #7

Merged
merged 4 commits into from
Nov 29, 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
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ jobs:
run: npm ci --no-audit
- name: Lint
run: npm run lint
- name: Test
run: npm run coverage
- name: Report coverage
uses: codecov/codecov-action@v5
78 changes: 78 additions & 0 deletions __tests__/copyFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { copyFiles } from '../src/lib/copyFiles';

describe('copyFiles', () => {
let sourceFolder: string;
let destinationFolder: string;

beforeEach(() => {
sourceFolder = fs.mkdtempSync(path.join(os.tmpdir(), 'source-'));
destinationFolder = fs.mkdtempSync(path.join(os.tmpdir(), 'destination-'));

// Create some test files in the source folder
fs.writeFileSync(path.join(sourceFolder, 'file1.txt'), 'Content of file 1');
fs.writeFileSync(path.join(sourceFolder, 'file2.txt'), 'Content of file 2');
});

afterEach(() => {
// Clean up the temporary folders
fs.rmSync(sourceFolder, { recursive: true, force: true });
fs.rmSync(destinationFolder, { recursive: true, force: true });
});

it('should copy files from source to destination', () => {
copyFiles(sourceFolder, destinationFolder);

const copiedFiles = fs.readdirSync(destinationFolder);
expect(copiedFiles).toContain('file1.txt');
expect(copiedFiles).toContain('file2.txt');

const file1Content = fs.readFileSync(
path.join(destinationFolder, 'file1.txt'),
'utf-8',
);
const file2Content = fs.readFileSync(
path.join(destinationFolder, 'file2.txt'),
'utf-8',
);
expect(file1Content).toBe('Content of file 1');
expect(file2Content).toBe('Content of file 2');
});

it('should create destination folder if it does not exist', () => {
const nonExistentDestination = path.join(
os.tmpdir(),
'non-existent-destination',
);
copyFiles(sourceFolder, nonExistentDestination);

const copiedFiles = fs.readdirSync(nonExistentDestination);
expect(copiedFiles).toContain('file1.txt');
expect(copiedFiles).toContain('file2.txt');

const file1Content = fs.readFileSync(
path.join(nonExistentDestination, 'file1.txt'),
'utf-8',
);
const file2Content = fs.readFileSync(
path.join(nonExistentDestination, 'file2.txt'),
'utf-8',
);
expect(file1Content).toBe('Content of file 1');
expect(file2Content).toBe('Content of file 2');

// Clean up the non-existent destination folder
fs.rmSync(nonExistentDestination, { recursive: true, force: true });
});

it('should not copy if source folder does not exist', () => {
const nonExistentSource = path.join(os.tmpdir(), 'non-existent-source');
copyFiles(nonExistentSource, destinationFolder);

const copiedFiles = fs.readdirSync(destinationFolder);
expect(copiedFiles.length).toBe(0);
});
});
55 changes: 55 additions & 0 deletions __tests__/createFile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { createFile } from '../src/lib/createFile';
import { Bridges, Themes } from '../src/types';

describe('createFile', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-createFile-'));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it('should create a file with the specified content', () => {
const theme: Themes = Themes.Semantic;
const bridge: Bridges = Bridges.GraphQL;
const extension = 'jsx';
const directory = tempDir;
const customDirPath = 'customDir';
const filePath = path.join(
directory,
customDirPath,
`uniforms-form.${extension}`,
);

createFile(theme, bridge, extension, directory, customDirPath);

expect(fs.existsSync(filePath)).toBe(true);
});

it('should throw an error theme or bridge are bad', () => {
const theme: Themes = 'default' as Themes;
const bridge: Bridges = Bridges.GraphQL;
const extension = 'jsx';
const directory = tempDir;
const customDirPath = 'customDir';
const filePath = path.join(
directory,
customDirPath,
`uniforms-form.${extension}`,
);

fs.mkdirSync(path.join(directory, customDirPath), { recursive: true });
fs.writeFileSync(filePath, 'content');

expect(() =>
createFile(theme, bridge, extension, directory, customDirPath),
).toThrow('No bridge or theme');
});
});
51 changes: 51 additions & 0 deletions __tests__/createFolder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { createFolder } from '../src/lib/createFolder';

describe('createFolder', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-createFolder-'));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it('should create a folder with the specified name', () => {
const folderName = 'newFolder';
const createdFolderPath = createFolder({ folderName, directory: tempDir });

expect(fs.existsSync(createdFolderPath)).toBe(true);
expect(fs.lstatSync(createdFolderPath).isDirectory()).toBe(true);
});

it('should create a folder with the specified name in a custom directory', () => {
const folderName = 'newFolder';
const customDirPath = 'customDir';
const createdFolderPath = createFolder({
folderName,
directory: tempDir,
customDirPath,
});

expect(fs.existsSync(createdFolderPath)).toBe(true);
expect(fs.lstatSync(createdFolderPath).isDirectory()).toBe(true);
});

it('should create a folder with the specified name in the root directory if customDirPath is empty', () => {
const folderName = 'newFolder';
const customDirPath = '';
const createdFolderPath = createFolder({
folderName,
directory: tempDir,
customDirPath,
});

expect(fs.existsSync(createdFolderPath)).toBe(true);
expect(fs.lstatSync(createdFolderPath).isDirectory()).toBe(true);
});
});
38 changes: 38 additions & 0 deletions __tests__/createTsConfigFileInFolder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { createTsConfigFileInFolder } from '../src/lib/createTsConfigFileInFolder';

describe('createTsConfigFileInFolder', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-createTsConfig-'));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it('should create a tsconfig.json file with the correct content', () => {
createTsConfigFileInFolder(tempDir);

const filePath = path.join(tempDir, 'tsconfig.json');
expect(fs.existsSync(filePath)).toBe(true);

const tsConfigContent = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
expect(tsConfigContent).toEqual({
include: ['./**/*'],
compilerOptions: {
outDir: './striped',
target: 'es2020',
module: 'es2020',
strict: false,
esModuleInterop: true,
jsx: 'react',
moduleResolution: 'node',
},
});
});
});
58 changes: 58 additions & 0 deletions __tests__/deleteSpecificFilesInFolder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { deleteSpecificFilesInFolder } from '../src/lib/deleteSpecificFilesInFolder';

describe('deleteSpecificFilesInFolder', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-deleteFiles-'));
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it('should delete specified files in the folder', () => {
const file1 = path.join(tempDir, 'file1.txt');
const file2 = path.join(tempDir, 'file2.txt');
const file3 = path.join(tempDir, 'file3.txt');
fs.writeFileSync(file1, 'content1');
fs.writeFileSync(file2, 'content2');
fs.writeFileSync(file3, 'content3');

deleteSpecificFilesInFolder(tempDir, ['file1.txt', 'file3.txt']);

expect(fs.existsSync(file1)).toBe(false);
expect(fs.existsSync(file2)).toBe(true);
expect(fs.existsSync(file3)).toBe(false);
});

it('should not throw an error if the specified files do not exist', () => {
const file1 = path.join(tempDir, 'file1.txt');
fs.writeFileSync(file1, 'content1');

expect(() => {
deleteSpecificFilesInFolder(tempDir, ['file2.txt']);
}).not.toThrow();

expect(fs.existsSync(file1)).toBe(true);
});

it('should handle an empty list of files to delete', () => {
const file1 = path.join(tempDir, 'file1.txt');
fs.writeFileSync(file1, 'content1');

deleteSpecificFilesInFolder(tempDir, []);

expect(fs.existsSync(file1)).toBe(true);
});

it('should throw an error if the folder does not exist', () => {
expect(() => {
deleteSpecificFilesInFolder('/non-existent-folder', ['file1.txt']);
}).toThrow();
});
});
46 changes: 46 additions & 0 deletions __tests__/fetchFilesFromGithub.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import fetch from 'node-fetch';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { fetchFiles } from '../src/lib/fetchFilesFromGithub';

vi.mock('node-fetch');

describe('fetchFiles', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'test-fetchFiles-'));
vi.spyOn(fs, 'mkdtempSync').mockReturnValue(tempDir);
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
vi.restoreAllMocks();
});

it('should fetch files from GitHub and save them to a temporary directory', async () => {
const mockHtmlResponse = `
<a href="/vazco/uniforms/blob/master/packages/uniforms-unstyled/src/file1.ts">file1.ts</a>
<a href="/vazco/uniforms/blob/master/packages/uniforms-unstyled/src/file2.ts">file2.ts</a>
`;
const mockFileContent = 'console.log("test");';

(fetch as any)
.mockResolvedValueOnce({ text: () => Promise.resolve(mockHtmlResponse) })
.mockResolvedValue({ text: () => Promise.resolve(mockFileContent) });

const result = await fetchFiles();

expect(result.tempDir).toBe(tempDir);
expect(fs.existsSync(path.join(tempDir, 'file1.ts'))).toBe(true);
expect(fs.existsSync(path.join(tempDir, 'file2.ts'))).toBe(true);
expect(fs.readFileSync(path.join(tempDir, 'file1.ts'), 'utf-8')).toBe(
mockFileContent,
);
expect(fs.readFileSync(path.join(tempDir, 'file2.ts'), 'utf-8')).toBe(
mockFileContent,
);
});
});
44 changes: 44 additions & 0 deletions __tests__/findNearestPackageJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { findNearestPackageJson } from '../src/lib/findNearestPackageJson';

describe('findNearestPackageJson', () => {
let tempDir: string;

beforeEach(() => {
tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), 'test-findNearestPackageJson-'),
);
});

afterEach(() => {
fs.rmSync(tempDir, { recursive: true, force: true });
});

it('should find the nearest package.json in the current directory', async () => {
const packageJsonPath = path.join(tempDir, 'package.json');
fs.writeFileSync(packageJsonPath, JSON.stringify({ name: 'test' }));

const result = await findNearestPackageJson(tempDir);
expect(result).toBe(packageJsonPath);
});

it('should find the nearest package.json without passing directory', async () => {
const result = await findNearestPackageJson();
console.log('result', result);

Check warning on line 30 in __tests__/findNearestPackageJson.test.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected console statement
expect(result).toBe(result);
});

it('should return null if no package.json is found in the current directory', async () => {
const result = await findNearestPackageJson(tempDir);
expect(result).toBeNull();
});

it('should return null if the package.json file does not exist', async () => {
const nonExistentPath = path.join(tempDir, 'non-existent-dir');
const result = await findNearestPackageJson(nonExistentPath);
expect(result).toBeNull();
});
});
Loading
Loading