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
188 changes: 188 additions & 0 deletions frontend/src/pages/Inventory.editor-mode.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { MemoryRouter } from 'react-router-dom';
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
import InventoryPage from './Inventory';
import { locationCache } from '../services/locationCache';
import type { LocationRecord } from '../services/location.service';

const mockUpdateItem = jest.fn();
const mockGetInventory = jest.fn();
const mockCreateItem = jest.fn();
const mockCreateOrgItem = jest.fn();
const mockSearchItems = jest.fn();

jest.mock('../services/inventory.service', () => ({
inventoryService: {
Expand All @@ -13,6 +18,8 @@ jest.mock('../services/inventory.service', () => ({
getOrgInventory: jest.fn(),
updateItem: (...args: unknown[]) => mockUpdateItem(...args),
updateOrgItem: jest.fn(),
createItem: (...args: unknown[]) => mockCreateItem(...args),
createOrgItem: (...args: unknown[]) => mockCreateOrgItem(...args),
shareItem: jest.fn(),
unshareItem: jest.fn(),
},
Expand All @@ -26,6 +33,12 @@ jest.mock('../services/locationCache', () => ({
},
}));

jest.mock('../services/uex.service', () => ({
uexService: {
searchItems: (...args: unknown[]) => mockSearchItems(...args),
getStarSystems: jest.fn(),
},
}));
const mockItem = {
id: 'item-1',
userId: 1,
Expand All @@ -48,6 +61,31 @@ describe('Inventory editor mode inline controls', () => {
jest.resetAllMocks();
mockGetInventory.mockResolvedValue({ items: [mockItem], total: 1, limit: 25, offset: 0 });
mockUpdateItem.mockResolvedValue({});
mockCreateItem.mockResolvedValue({});
mockSearchItems.mockResolvedValue({
items: [
{
id: 101,
uexId: 300,
name: 'New Catalog Item',
categoryName: 'Category',
},
],
total: 1,
limit: 20,
offset: 0,
});
const mockedLocationCache = locationCache as jest.Mocked<typeof locationCache>;
const mockLocation: LocationRecord = {
id: '200',
gameId: 1,
locationType: 'city',
displayName: 'Test Location',
shortName: 'Test Loc',
isAvailable: true,
hierarchyPath: {},
};
mockedLocationCache.getAllLocations.mockResolvedValue([mockLocation]);
// minimal profile fetch
global.fetch = jest.fn().mockResolvedValue({
ok: true,
Expand Down Expand Up @@ -82,4 +120,154 @@ describe('Inventory editor mode inline controls', () => {
quantity: 5,
});
});

it('allows creating a new inventory item from the pinned row', async () => {
render(
<MemoryRouter initialEntries={['/inventory']}>
<InventoryPage />
</MemoryRouter>,
);

await waitFor(() => expect(screen.getByText('Test Item')).toBeInTheDocument());

const viewModeSelect = screen.getByLabelText('View mode');
fireEvent.mouseDown(viewModeSelect);
const editorOption = await screen.findByText('Editor Mode');
fireEvent.click(editorOption);

const itemInput = await screen.findByTestId('new-row-item-input');
fireEvent.focus(itemInput);
fireEvent.change(itemInput, { target: { value: 'New' } });

const itemOption = await screen.findByText('New Catalog Item');
fireEvent.click(itemOption);

const locationInput = await screen.findByTestId('new-row-location-input');
fireEvent.focus(locationInput);
fireEvent.change(locationInput, { target: { value: 'Test' } });
const locationOption = await screen.findByText('Test Location');
fireEvent.click(locationOption);

const quantityInput = screen.getByTestId('new-row-quantity');
fireEvent.change(quantityInput, { target: { value: '7' } });

const saveButton = screen.getByTestId('new-row-save');
fireEvent.click(saveButton);

await waitFor(() => expect(mockCreateItem).toHaveBeenCalled());
expect(mockCreateItem).toHaveBeenCalledWith({
gameId: 1,
uexItemId: 300,
locationId: 200,
quantity: 7,
});
});

it('surfaces validation errors and focuses the first invalid field for new row', async () => {
render(
<MemoryRouter initialEntries={['/inventory']}>
<InventoryPage />
</MemoryRouter>,
);

await waitFor(() => expect(screen.getByText('Test Item')).toBeInTheDocument());

const viewModeSelect = screen.getByLabelText('View mode');
fireEvent.mouseDown(viewModeSelect);
const editorOption = await screen.findByText('Editor Mode');
fireEvent.click(editorOption);

const saveButton = await screen.findByTestId('new-row-save');
fireEvent.click(saveButton);

await waitFor(() => expect(screen.getByText('Select an item')).toBeInTheDocument());
const itemInput = await screen.findByTestId('new-row-item-input');
await waitFor(() => expect(document.activeElement).toBe(itemInput));
});

it('prevents non-integer quantities and shows error', async () => {
render(
<MemoryRouter initialEntries={['/inventory']}>
<InventoryPage />
</MemoryRouter>,
);

await waitFor(() => expect(screen.getByText('Test Item')).toBeInTheDocument());
const viewModeSelect = screen.getByLabelText('View mode');
fireEvent.mouseDown(viewModeSelect);
const editorOption = await screen.findByText('Editor Mode');
fireEvent.click(editorOption);

const itemInput = await screen.findByTestId('new-row-item-input');
fireEvent.change(itemInput, { target: { value: 'New' } });
fireEvent.click(await screen.findByText('New Catalog Item'));

const locationInput = await screen.findByTestId('new-row-location-input');
fireEvent.change(locationInput, { target: { value: 'Test' } });
fireEvent.click(await screen.findByText('Test Location'));

const quantityInput = screen.getByTestId('new-row-quantity');
fireEvent.change(quantityInput, { target: { value: '7.5' } });
const saveButton = screen.getByTestId('new-row-save');
fireEvent.click(saveButton);

expect(mockCreateItem).not.toHaveBeenCalled();
expect(screen.getByText('Quantity must be an integer greater than 0')).toBeInTheDocument();
});

it('keeps the row dirty and shows retry on API failure', async () => {
mockCreateItem.mockRejectedValueOnce(new Error('fail'));
render(
<MemoryRouter initialEntries={['/inventory']}>
<InventoryPage />
</MemoryRouter>,
);

await waitFor(() => expect(screen.getByText('Test Item')).toBeInTheDocument());
const viewModeSelect = screen.getByLabelText('View mode');
fireEvent.mouseDown(viewModeSelect);
const editorOption = await screen.findByText('Editor Mode');
fireEvent.click(editorOption);

fireEvent.change(await screen.findByTestId('new-row-item-input'), { target: { value: 'New' } });
fireEvent.click(await screen.findByText('New Catalog Item'));

const locationInput = await screen.findByTestId('new-row-location-input');
fireEvent.change(locationInput, { target: { value: 'Test' } });
fireEvent.click(await screen.findByText('Test Location'));

const quantityInput = screen.getByTestId('new-row-quantity');
fireEvent.change(quantityInput, { target: { value: '7' } });

fireEvent.click(screen.getByTestId('new-row-save'));

await waitFor(() =>
expect(screen.getByText('Unable to add item. Please try again.')).toBeInTheDocument(),
);
expect((screen.getByTestId('new-row-quantity') as HTMLInputElement).value).toBe('7');
});

it('shows inline quantity validation error for non-integer input and focuses the field', async () => {
render(
<MemoryRouter initialEntries={['/inventory']}>
<InventoryPage />
</MemoryRouter>,
);

await waitFor(() => expect(screen.getByText('Test Item')).toBeInTheDocument());
const viewModeSelect = screen.getByLabelText('View mode');
fireEvent.mouseDown(viewModeSelect);
const editorOption = await screen.findByText('Editor Mode');
fireEvent.click(editorOption);

const quantityInput = await screen.findByTestId('inline-quantity-item-1');
fireEvent.change(quantityInput, { target: { value: '3.5' } });
fireEvent.click(screen.getByTestId('inline-save-item-1'));

await waitFor(() =>
expect(screen.getByText('Quantity must be an integer greater than 0')).toBeInTheDocument(),
);
await waitFor(() => expect(document.activeElement).toBe(quantityInput));
expect(mockUpdateItem).not.toHaveBeenCalled();
});
});
Loading