Skip to content
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
103 changes: 103 additions & 0 deletions frontend/src/components/ConversionHistory/ConversionHistory.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import ConversionHistory from './ConversionHistory';
import { ConversionHistoryItem } from './types';
import React from 'react';

// Mock localStorage
const localStorageMock = (() => {
let store: Record<string, string> = {};
return {
getItem: vi.fn((key: string) => store[key] || null),
setItem: vi.fn((key: string, value: string) => {
store[key] = value.toString();
}),
removeItem: vi.fn((key: string) => {
delete store[key];
}),
clear: vi.fn(() => {
store = {};
}),
};
})();

Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});

// Mock fetch
global.fetch = vi.fn();

describe('ConversionHistory', () => {
const mockHistoryItems: ConversionHistoryItem[] = [
{
job_id: 'job-1',
original_filename: 'test-mod.jar',
status: 'completed',
created_at: '2026-02-18T00:37:20.000Z', // Fixed date for consistency
file_size: 1024 * 1024, // 1MB
},
{
job_id: 'job-2',
original_filename: 'another-mod.zip',
status: 'processing',
created_at: '2026-02-18T00:37:10.000Z',
Comment on lines +37 to +44
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test data uses a future date (2026-02-18). While the current date is February 18, 2026, using hardcoded future dates in tests can cause confusion and make tests less maintainable. Consider using relative dates (e.g., new Date().toISOString()) or dates clearly in the past to avoid confusion when reviewing code in the future.

Suggested change
created_at: '2026-02-18T00:37:20.000Z', // Fixed date for consistency
file_size: 1024 * 1024, // 1MB
},
{
job_id: 'job-2',
original_filename: 'another-mod.zip',
status: 'processing',
created_at: '2026-02-18T00:37:10.000Z',
created_at: '2020-01-01T00:37:20.000Z', // Fixed past date for consistency
file_size: 1024 * 1024, // 1MB
},
{
job_id: 'job-2',
original_filename: 'another-mod.zip',
status: 'processing',
created_at: '2020-01-01T00:37:10.000Z',

Copilot uses AI. Check for mistakes.
},
];

beforeEach(() => {
vi.clearAllMocks();
localStorageMock.clear();
// Setup initial localStorage data
localStorageMock.setItem(
'modporter_conversion_history',
JSON.stringify(mockHistoryItems)
);
});

it('renders history items from localStorage', async () => {
render(<ConversionHistory />);

// Check items loaded
await waitFor(() => {
expect(screen.getByText('test-mod.jar')).toBeInTheDocument();
expect(screen.getByText('another-mod.zip')).toBeInTheDocument();
});

// Check status icons/text
expect(screen.getByText('Completed')).toBeInTheDocument();
expect(screen.getByText('Processing')).toBeInTheDocument();
});

it('handles item selection', async () => {
render(<ConversionHistory />);

await waitFor(() => {
expect(screen.getByText('test-mod.jar')).toBeInTheDocument();
});

const checkbox = screen.getByLabelText('Select test-mod.jar');
fireEvent.click(checkbox);

// Check if delete selected button appears
expect(screen.getByText(/Delete Selected/i)).toBeInTheDocument();
});

it('deletes an item', async () => {
render(<ConversionHistory />);

await waitFor(() => {
expect(screen.getByText('test-mod.jar')).toBeInTheDocument();
});

const deleteButtons = screen.getAllByTitle('Remove from history');
fireEvent.click(deleteButtons[0]);

await waitFor(() => {
expect(screen.queryByText('test-mod.jar')).not.toBeInTheDocument();
});

// Verify localStorage update
expect(localStorageMock.setItem).toHaveBeenCalled();
});
});
Comment on lines +31 to +103
Copy link

Copilot AI Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage is incomplete for the performance optimization changes. Missing tests for: 1) Verifying that ConversionHistoryItem doesn't re-render when other items change (the core optimization), 2) Testing the clearAllHistory function, 3) Testing the deleteSelected function (bulk delete), 4) Testing the download functionality, 5) Testing error states. Add tests that verify the memoization is working correctly by checking render counts.

Copilot uses AI. Check for mistakes.
Loading
Loading