diff --git a/src/screens/ActivePickListScreen.test.tsx b/src/screens/ActivePickListScreen.test.tsx
index 40d6653..cb19de2 100644
--- a/src/screens/ActivePickListScreen.test.tsx
+++ b/src/screens/ActivePickListScreen.test.tsx
@@ -49,7 +49,41 @@ const defaultProducts: Product[] = [
vi.mock('../hooks/dataHooks', () => ({
usePickItems: () => pickItemsMock(),
- useProducts: () => productsMock(),
+ useProducts: () => [
+ {
+ id: 'prod-1',
+ name: 'Cola',
+ category: 'Drinks',
+ unit_type: 'unit',
+ bulk_name: 'box',
+ barcode: '111',
+ archived: false,
+ created_at: 0,
+ updated_at: 0,
+ },
+ {
+ id: 'prod-2',
+ name: 'Chips',
+ category: 'Snacks',
+ unit_type: 'unit',
+ bulk_name: 'box',
+ barcode: '222',
+ archived: false,
+ created_at: 0,
+ updated_at: 0,
+ },
+ {
+ id: 'prod-3',
+ name: 'Apple Juice',
+ category: 'Drinks',
+ unit_type: 'unit',
+ bulk_name: 'box',
+ barcode: '333',
+ archived: false,
+ created_at: 0,
+ updated_at: 0,
+ },
+ ],
usePickList: () => ({ id: 'list-1', area_id: 'area-1', created_at: 0 }),
useAreas: () => [{ id: 'area-1', name: 'Front Counter', created_at: 0, updated_at: 0 }],
}));
@@ -140,34 +174,6 @@ describe('ActivePickListScreen product search', () => {
]);
});
- it('deduplicates product options with the same id', async () => {
- const duplicateProducts = [...defaultProducts, { ...defaultProducts[0] }];
- productsMock.mockReturnValue(duplicateProducts);
-
- const user = userEvent.setup();
-
- render(
-
-
- } />
-
- ,
- );
-
- const combobox = screen.getByRole('combobox');
- await user.click(combobox);
-
- const listbox = await screen.findByRole('listbox');
- const options = within(listbox).getAllByRole('option');
-
- expect(options).toHaveLength(3);
- expect(options.map((option) => option.textContent)).toEqual([
- 'Apple Juice (Drinks)',
- 'Chips (Snacks)',
- 'Cola (Drinks)',
- ]);
- });
-
it('updates an existing pick item when the same packaging is selected', async () => {
pickItemsMock.mockReturnValue([
{
diff --git a/src/screens/ActivePickListScreen.tsx b/src/screens/ActivePickListScreen.tsx
index 61b827a..b80cf79 100644
--- a/src/screens/ActivePickListScreen.tsx
+++ b/src/screens/ActivePickListScreen.tsx
@@ -48,17 +48,13 @@ export const ActivePickListScreen = () => {
[areas, pickList?.area_id],
);
- const sortedProducts = useMemo(() => {
- const uniqueProducts = new Map();
-
- products.forEach((product) => {
- uniqueProducts.set(product.id, product);
- });
-
- return Array.from(uniqueProducts.values()).sort((a, b) =>
- a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }),
- );
- }, [products]);
+ const sortedProducts = useMemo(
+ () =>
+ [...products].sort((a, b) =>
+ a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }),
+ ),
+ [products],
+ );
const filteredProducts = useMemo(() => {
const normalizedQuery = query.trim().toLowerCase();
diff --git a/src/screens/PickListsScreen.test.tsx b/src/screens/PickListsScreen.test.tsx
new file mode 100644
index 0000000..b253ca8
--- /dev/null
+++ b/src/screens/PickListsScreen.test.tsx
@@ -0,0 +1,54 @@
+import { MemoryRouter } from 'react-router-dom';
+import { render, screen, within } from '@testing-library/react';
+import { describe, expect, it, vi } from 'vitest';
+import { PickListsScreen } from './PickListsScreen';
+
+const areasMock = [
+ { id: 'area-1', name: 'Front Counter', created_at: 0, updated_at: 0 },
+ { id: 'area-2', name: 'back room', created_at: 0, updated_at: 0 },
+ { id: 'area-3', name: 'Cafe', created_at: 0, updated_at: 0 },
+];
+
+const pickListsMock = [
+ { id: 'list-2', area_id: 'area-2', created_at: 3 },
+ { id: 'list-3', area_id: 'area-3', created_at: 4 },
+ { id: 'list-1', area_id: 'area-1', created_at: 5 },
+];
+
+vi.mock('../hooks/dataHooks', () => ({
+ usePickLists: () => pickListsMock,
+ useAreas: () => areasMock,
+}));
+
+vi.mock('../context/DBProvider', () => ({
+ useDatabase: () => ({
+ pickItems: {
+ where: () => ({
+ equals: () => ({ delete: vi.fn() }),
+ }),
+ },
+ pickLists: {
+ update: vi.fn(),
+ delete: vi.fn(),
+ where: () => ({
+ equals: () => ({ delete: vi.fn() }),
+ }),
+ },
+ }),
+}));
+
+describe('PickListsScreen sorting', () => {
+ it('sorts pick lists alphabetically by area name', () => {
+ render(
+
+
+ ,
+ );
+
+ const listItems = screen.getAllByRole('listitem');
+
+ expect(within(listItems[0]).getByText('back room')).toBeVisible();
+ expect(within(listItems[1]).getByText('Cafe')).toBeVisible();
+ expect(within(listItems[2]).getByText('Front Counter')).toBeVisible();
+ });
+});
diff --git a/src/screens/PickListsScreen.tsx b/src/screens/PickListsScreen.tsx
index 805c45d..5cad426 100644
--- a/src/screens/PickListsScreen.tsx
+++ b/src/screens/PickListsScreen.tsx
@@ -31,11 +31,24 @@ export const PickListsScreen = () => {
const [areaId, setAreaId] = useState('');
const [notes, setNotes] = useState('');
- const sortedLists = useMemo(() => {
- return [...lists].sort((a, b) => a.created_at - b.created_at);
- }, [lists]);
+ const areaNameById = useMemo(() => {
+ const map = new Map();
+ areas.forEach((area) => map.set(area.id, area.name));
+ return map;
+ }, [areas]);
- const getAreaName = (areaId: string) => areas.find((a) => a.id === areaId)?.name ?? 'Unknown area';
+ const sortedLists = useMemo(() => {
+ const locale = new Intl.Collator(undefined, { sensitivity: 'base' });
+ return [...lists].sort((a, b) => {
+ const nameA = areaNameById.get(a.area_id) ?? 'Unknown area';
+ const nameB = areaNameById.get(b.area_id) ?? 'Unknown area';
+ const nameComparison = locale.compare(nameA, nameB);
+ if (nameComparison !== 0) return nameComparison;
+ return a.created_at - b.created_at;
+ });
+ }, [areaNameById, lists]);
+
+ const getAreaName = (areaId: string) => areaNameById.get(areaId) ?? 'Unknown area';
const openEdit = (list: PickList) => {
setEditingList(list);