Merge branch 'main' of https://github.com/beatz174-bit/stockfill
This commit is contained in:
@@ -9,12 +9,17 @@ import { ManageProductsScreen } from './ManageProductsScreen';
|
|||||||
let mockScannedBarcode = '123456';
|
let mockScannedBarcode = '123456';
|
||||||
const mockUseProducts = vi.fn();
|
const mockUseProducts = vi.fn();
|
||||||
const mockUseCategories = vi.fn();
|
const mockUseCategories = vi.fn();
|
||||||
|
const productDeleteMock = vi.fn();
|
||||||
|
const pickItemCountMock = vi.fn();
|
||||||
|
|
||||||
const mockDb = {
|
const mockDb = {
|
||||||
products: {
|
products: {
|
||||||
add: vi.fn(),
|
add: vi.fn(),
|
||||||
update: vi.fn(),
|
update: vi.fn(),
|
||||||
delete: vi.fn(),
|
delete: productDeleteMock,
|
||||||
|
where: vi.fn(),
|
||||||
|
},
|
||||||
|
pickItems: {
|
||||||
where: vi.fn(),
|
where: vi.fn(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -24,9 +29,6 @@ vi.mock('../hooks/dataHooks', () => ({
|
|||||||
useCategories: () => mockUseCategories(),
|
useCategories: () => mockUseCategories(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const productDeleteMock = vi.fn();
|
|
||||||
const pickItemCountMock = vi.fn();
|
|
||||||
|
|
||||||
vi.mock('../context/DBProvider', () => ({
|
vi.mock('../context/DBProvider', () => ({
|
||||||
useDatabase: () => mockDb,
|
useDatabase: () => mockDb,
|
||||||
}));
|
}));
|
||||||
@@ -41,6 +43,23 @@ vi.mock('../components/BarcodeScannerView', () => ({
|
|||||||
|
|
||||||
const server = setupServer();
|
const server = setupServer();
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockUseProducts.mockReset();
|
||||||
|
mockUseCategories.mockReset();
|
||||||
|
mockScannedBarcode = '123456';
|
||||||
|
Object.values(mockDb.products).forEach((fn) => fn.mockReset());
|
||||||
|
mockDb.products.where.mockImplementation(() => ({
|
||||||
|
equals: (value: string) => ({
|
||||||
|
first: () => Promise.resolve(mockUseProducts().find((product: any) => product.barcode === value)),
|
||||||
|
}),
|
||||||
|
}));
|
||||||
|
pickItemCountMock.mockReset();
|
||||||
|
mockDb.pickItems.where.mockReset();
|
||||||
|
mockDb.pickItems.where.mockImplementation(() => ({
|
||||||
|
equals: () => ({ count: pickItemCountMock }),
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
describe('ManageProductsScreen barcode lookup', () => {
|
describe('ManageProductsScreen barcode lookup', () => {
|
||||||
beforeAll(() => server.listen());
|
beforeAll(() => server.listen());
|
||||||
afterEach(() => server.resetHandlers());
|
afterEach(() => server.resetHandlers());
|
||||||
@@ -49,13 +68,6 @@ describe('ManageProductsScreen barcode lookup', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mockUseProducts.mockReturnValue([]);
|
mockUseProducts.mockReturnValue([]);
|
||||||
mockUseCategories.mockReturnValue([{ id: 'cat-1', name: 'Snacks', created_at: 0, updated_at: 0 }]);
|
mockUseCategories.mockReturnValue([{ id: 'cat-1', name: 'Snacks', created_at: 0, updated_at: 0 }]);
|
||||||
mockScannedBarcode = '123456';
|
|
||||||
Object.values(mockDb.products).forEach((fn) => fn.mockReset());
|
|
||||||
mockDb.products.where.mockImplementation(() => ({
|
|
||||||
equals: (value: string) => ({
|
|
||||||
first: () => Promise.resolve(mockUseProducts().find((product: any) => product.barcode === value)),
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('prefills the product name after scanning a barcode', async () => {
|
it('prefills the product name after scanning a barcode', async () => {
|
||||||
@@ -166,6 +178,19 @@ describe('ManageProductsScreen barcode lookup', () => {
|
|||||||
|
|
||||||
describe('ManageProductsScreen deletion safeguards', () => {
|
describe('ManageProductsScreen deletion safeguards', () => {
|
||||||
it('blocks deletion when pick items reference the product', async () => {
|
it('blocks deletion when pick items reference the product', async () => {
|
||||||
|
mockUseProducts.mockReturnValue([
|
||||||
|
{
|
||||||
|
id: 'prod-chips',
|
||||||
|
name: 'Chips',
|
||||||
|
category: 'Snacks',
|
||||||
|
unit_type: 'unit',
|
||||||
|
bulk_name: 'pack',
|
||||||
|
archived: false,
|
||||||
|
created_at: 0,
|
||||||
|
updated_at: 0,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
mockUseCategories.mockReturnValue([{ id: 'cat-1', name: 'Snacks', created_at: 0, updated_at: 0 }]);
|
||||||
pickItemCountMock.mockResolvedValueOnce(2);
|
pickItemCountMock.mockResolvedValueOnce(2);
|
||||||
|
|
||||||
const user = userEvent.setup();
|
const user = userEvent.setup();
|
||||||
|
|||||||
Reference in New Issue
Block a user