feat:Setup Prettier for code formatting

This commit is contained in:
NanditaPatil-dotcom
2025-10-17 11:37:32 +05:30
parent 87c191213c
commit f1d830d81d
96 changed files with 9420 additions and 7154 deletions

View File

@@ -4,7 +4,10 @@ import { PDFDocument } from 'pdf-lib';
import Sortable from 'sortablejs';
import * as helpers from '@/js/utils/helpers';
import * as ui from '@/js/ui';
import { setupAlternateMergeTool, alternateMerge } from '@/js/logic/alternate-merge';
import {
setupAlternateMergeTool,
alternateMerge,
} from '@/js/logic/alternate-merge';
vi.mock('pdf-lib', () => ({
PDFDocument: {
@@ -47,7 +50,9 @@ describe('Alternate Merge Tool', () => {
mockPdfDoc1 = { getPageCount: vi.fn(() => 2) };
mockPdfDoc2 = { getPageCount: vi.fn(() => 3) };
vi.mocked(helpers.readFileAsArrayBuffer).mockResolvedValue(new ArrayBuffer(8));
vi.mocked(helpers.readFileAsArrayBuffer).mockResolvedValue(
new ArrayBuffer(8)
);
vi.mocked(PDFDocument.load)
.mockResolvedValueOnce(mockPdfDoc1)
.mockResolvedValueOnce(mockPdfDoc2);
@@ -66,16 +71,18 @@ describe('Alternate Merge Tool', () => {
expect(ui.showLoader).toHaveBeenCalledWith('Loading PDF documents...');
expect(ui.hideLoader).toHaveBeenCalled();
expect(PDFDocument.load).toHaveBeenCalledTimes(2);
expect(document.querySelectorAll('#alternate-file-list li').length).toBe(2);
expect(document.querySelectorAll('#alternate-file-list li').length).toBe(
2
);
expect(Sortable.create).toHaveBeenCalled();
});
it('should show alert on load failure', async () => {
vi.mocked(PDFDocument.load).mockReset();
vi.mocked(PDFDocument.load).mockRejectedValueOnce(new Error('bad pdf'));
await setupAlternateMergeTool();
expect(ui.showAlert).toHaveBeenCalledWith(
'Error',
expect.stringContaining('Failed to load one or more PDF files')
@@ -90,12 +97,12 @@ describe('Alternate Merge Tool', () => {
state.files = [new File(['dummy1'], 'file1.pdf')];
vi.mocked(PDFDocument.load).mockReset();
vi.mocked(PDFDocument.load).mockResolvedValueOnce(mockPdfDoc1);
await setupAlternateMergeTool();
vi.clearAllMocks(); // Clear the setup calls
await alternateMerge();
expect(ui.showAlert).toHaveBeenCalledWith(
'Not Enough Files',
expect.stringContaining('Please upload at least two PDF files')
@@ -106,7 +113,7 @@ describe('Alternate Merge Tool', () => {
// First setup the tool to populate internal state
await setupAlternateMergeTool();
vi.clearAllMocks(); // Clear setup calls
const mockCopyPages = vi.fn(() =>
Promise.resolve([{ page: 'mockPage' }] as any)
);
@@ -129,12 +136,17 @@ describe('Alternate Merge Tool', () => {
await alternateMerge();
expect(ui.showLoader).toHaveBeenCalledWith(expect.stringContaining('Alternating'));
expect(ui.showLoader).toHaveBeenCalledWith(
expect.stringContaining('Alternating')
);
expect(mockCopyPages).toHaveBeenCalled();
expect(mockAddPage).toHaveBeenCalled();
expect(mockSave).toHaveBeenCalled();
expect(helpers.downloadFile).toHaveBeenCalled();
expect(ui.showAlert).toHaveBeenCalledWith('Success', expect.stringContaining('mixed successfully'));
expect(ui.showAlert).toHaveBeenCalledWith(
'Success',
expect.stringContaining('mixed successfully')
);
expect(ui.hideLoader).toHaveBeenCalled();
});
@@ -142,7 +154,7 @@ describe('Alternate Merge Tool', () => {
// Setup the tool first to populate internal state with 2 PDFs
await setupAlternateMergeTool();
vi.clearAllMocks(); // Clear setup calls
// Mock PDFDocument.create to reject
vi.mocked(PDFDocument.create).mockRejectedValue(new Error('broken'));
@@ -155,4 +167,4 @@ describe('Alternate Merge Tool', () => {
expect(ui.hideLoader).toHaveBeenCalled();
});
});
});
});

View File

@@ -211,4 +211,4 @@ describe('helpers', () => {
expect(result).toEqual([0, 4]);
});
});
});
});

View File

@@ -1,8 +1,11 @@
import { describe, it, expect } from 'vitest';
import { singlePdfLoadTools, simpleTools, multiFileTools } from '@/js/config/pdf-tools';
import {
singlePdfLoadTools,
simpleTools,
multiFileTools,
} from '@/js/config/pdf-tools';
describe('Tool Configuration Arrays', () => {
// --- Tests for singlePdfLoadTools ---
describe('singlePdfLoadTools', () => {
it('should be an array of non-empty strings', () => {
@@ -30,7 +33,7 @@ describe('Tool Configuration Arrays', () => {
it('should be an array of non-empty strings', () => {
expect(Array.isArray(simpleTools)).toBe(true);
expect(simpleTools.length).toBeGreaterThan(0);
simpleTools.forEach(tool => {
simpleTools.forEach((tool) => {
expect(typeof tool).toBe('string');
expect(tool.length).toBeGreaterThan(0);
});
@@ -51,7 +54,7 @@ describe('Tool Configuration Arrays', () => {
it('should be an array of non-empty strings', () => {
expect(Array.isArray(multiFileTools)).toBe(true);
expect(multiFileTools.length).toBeGreaterThan(0);
multiFileTools.forEach(tool => {
multiFileTools.forEach((tool) => {
expect(typeof tool).toBe('string');
expect(tool.length).toBeGreaterThan(0);
});
@@ -82,5 +85,4 @@ describe('Tool Configuration Arrays', () => {
expect(uniqueTools.size).toBe(allTools.length);
});
});
});
});

View File

@@ -1,5 +1,8 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { setupRemoveBlankPagesTool, removeBlankPages } from '@/js/logic/remove-blank-pages';
import {
setupRemoveBlankPagesTool,
removeBlankPages,
} from '@/js/logic/remove-blank-pages';
import * as ui from '@/js/ui';
import * as helpers from '@/js/utils/helpers';
import { state } from '@/js/state';
@@ -11,7 +14,12 @@ if (typeof ImageData === 'undefined') {
width: number;
height: number;
colorSpace: string;
constructor(data: Uint8ClampedArray, width: number, height: number, options?: { colorSpace: string }) {
constructor(
data: Uint8ClampedArray,
width: number,
height: number,
options?: { colorSpace: string }
) {
this.data = data;
this.width = width;
this.height = height;
@@ -27,11 +35,17 @@ const mockContext: CanvasRenderingContext2D = {
} as unknown as CanvasRenderingContext2D;
HTMLCanvasElement.prototype.getContext = vi.fn().mockReturnValue(mockContext);
HTMLCanvasElement.prototype.toDataURL = vi.fn().mockReturnValue('data:image/png;base64,mock');
HTMLCanvasElement.prototype.toDataURL = vi
.fn()
.mockReturnValue('data:image/png;base64,mock');
function createMockPage(isBlank: boolean) {
return {
getViewport: vi.fn(({ scale }) => ({ width: 800 * scale, height: 600 * scale, scale })),
getViewport: vi.fn(({ scale }) => ({
width: 800 * scale,
height: 600 * scale,
scale,
})),
render: vi.fn(() => {
// Return ImageData depending on blank/content
mockContext.getImageData = vi.fn(() => {
@@ -187,4 +201,4 @@ describe('Remove Blank Pages Tool', () => {
);
expect(helpers.downloadFile).not.toHaveBeenCalled();
});
});
});

View File

@@ -13,7 +13,7 @@ global.ResizeObserver = vi.fn().mockImplementation(() => ({
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation(query => ({
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
@@ -29,4 +29,4 @@ global.IntersectionObserver = vi.fn().mockImplementation(() => ({
observe: vi.fn(),
unobserve: vi.fn(),
disconnect: vi.fn(),
}));
}));

View File

@@ -2,7 +2,6 @@ import { state, resetState } from '@/js/state';
import { describe, it, expect, beforeEach } from 'vitest';
describe('State Management', () => {
// Test the initial state on import
describe('Initial State', () => {
it('should have the correct initial values', () => {
@@ -16,7 +15,6 @@ describe('State Management', () => {
// Test the resetState function
describe('resetState function', () => {
// Before each test in this block, we'll "dirty" the state
// to ensure the reset function is actually doing something.
beforeEach(() => {
@@ -29,7 +27,8 @@ describe('State Management', () => {
// 2. Create the DOM element that the function interacts with
// The setup.ts file will clean this up automatically after each test.
document.body.innerHTML = '<div id="tool-content">Some old tool content</div>';
document.body.innerHTML =
'<div id="tool-content">Some old tool content</div>';
});
it('should reset all state properties to their initial values', () => {
@@ -57,4 +56,4 @@ describe('State Management', () => {
expect(toolContentElement?.innerHTML).toBe('');
});
});
});
});

View File

@@ -4,7 +4,6 @@ import { categories } from '@/js/config/tools';
import { describe, it, expect } from 'vitest';
describe('Tool Categories Configuration', () => {
// 1. Basic Structure and Type Checking
it('should be an array of category objects', () => {
expect(Array.isArray(categories)).toBe(true);
@@ -13,7 +12,6 @@ describe('Tool Categories Configuration', () => {
// 2. Loop through each category to perform specific checks
describe.each(categories)('Category: "$name"', (category) => {
// Check that the category object itself is well-formed
it('should have a non-empty "name" string and a non-empty "tools" array', () => {
expect(typeof category.name).toBe('string');
@@ -24,7 +22,7 @@ describe('Tool Categories Configuration', () => {
// **KEY CHANGE**: This test now ensures IDs are unique only WITHIN this specific category.
it('should not contain any duplicate tool IDs within its own list', () => {
const toolIds = category.tools.map(tool => tool.id);
const toolIds = category.tools.map((tool) => tool.id);
const uniqueToolIds = new Set(toolIds);
// This assertion checks for duplicates inside THIS category only.
@@ -49,4 +47,4 @@ describe('Tool Categories Configuration', () => {
});
});
});
});
});