Files
lonc/tests/features/labels/upsert-submit.test.js
T
bblaz e1383c4d56
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
Add label printing functionality and error handling in stock and label flows
2026-04-10 22:08:01 +02:00

134 lines
4.0 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
const applyItemUpsertMock = vi.fn();
const previewItemUpsertMock = vi.fn();
const printItemLabelMock = vi.fn();
vi.mock('../../../src/api/stock.js', () => ({
applyItemUpsert: (...args) => applyItemUpsertMock(...args),
previewItemUpsert: (...args) => previewItemUpsertMock(...args),
searchItemDefinitions: vi.fn(async () => []),
}));
vi.mock('../../../src/api/labels.js', () => ({
previewLabel: vi.fn(async () => ({ objectUrl: 'blob:preview' })),
printItemLabel: (...args) => printItemLabelMock(...args),
formatPrintErrorMessage: (error) => error?.message || 'Printing failed.',
}));
vi.mock('../../../src/api/locations.js', () => ({
fetchLocations: vi.fn(async () => ({ flat: [], tree: [] })),
}));
const { labelCreatePageData } = await import('../../../src/features/labels/label-create-page.js');
describe('label create upsert-first submit', () => {
it('defaults print checkbox to enabled', () => {
const data = labelCreatePageData({
isConnected: false,
activeKitchen: { id: 1 },
addAlert: vi.fn(),
});
expect(data.printLabelOnSave).toBe(true);
});
it('builds upsert payload with selected template uuid', () => {
const store = {
isConnected: false,
activeKitchen: { id: 7 },
addAlert: vi.fn(),
};
const data = labelCreatePageData(store);
data.form = {
...data.form,
itemUuidB64: 'uuid-template-1',
name: 'Beans',
description: 'Dry beans',
stockType: 'measured',
quantity: '2',
uom: 'kg',
level: '',
productionDate: '2026-04-10',
expirationDate: '2026-08-10',
locationId: '',
identifierCode: '12345',
};
const payload = data.buildUpsertPayload();
expect(payload.uuid_b64).toBe('uuid-template-1');
expect(payload.identifier_code).toBe('12345');
expect(payload.item.name).toBe('Beans');
expect(payload.item.quantity_initial).toBe(2);
});
it('create uses applyItemUpsert and sets operation-aware success message', async () => {
applyItemUpsertMock.mockResolvedValueOnce({
operation: 'update',
item: { name: 'Rice', uuid_b64: 'uuid-rice-1' },
});
printItemLabelMock.mockResolvedValueOnce(null);
const addAlert = vi.fn();
const store = {
isConnected: false,
activeKitchen: { id: 3 },
addAlert,
};
const data = labelCreatePageData(store);
data.validateBeforeSubmit = () => true;
data.form = {
...data.form,
name: 'Rice',
stockType: 'binary',
locationId: '',
productionDate: '2026-04-10',
itemUuidB64: 'uuid-rice-1',
};
await data.create();
expect(applyItemUpsertMock).toHaveBeenCalledTimes(1);
expect(applyItemUpsertMock.mock.calls[0][1].uuid_b64).toBe('uuid-rice-1');
expect(printItemLabelMock).toHaveBeenCalledWith(store, 'uuid-rice-1');
expect(data.successMessage).toBe('Rice was updated successfully.');
expect(addAlert).toHaveBeenCalledWith({
type: 'success',
message: 'Rice was updated successfully.',
});
});
it('create shows parsed print issue warning when printing fails', async () => {
applyItemUpsertMock.mockResolvedValueOnce({
operation: 'create',
item: { name: 'Beans', uuid_b64: 'uuid-beans-1' },
});
printItemLabelMock.mockRejectedValueOnce(new Error('Printer is unavailable.'));
const addAlert = vi.fn();
const store = {
isConnected: false,
activeKitchen: { id: 3 },
addAlert,
};
const data = labelCreatePageData(store);
data.validateBeforeSubmit = () => true;
data.form = {
...data.form,
name: 'Beans',
stockType: 'binary',
locationId: '',
productionDate: '2026-04-10',
itemUuidB64: '',
};
await data.create();
expect(data.printIssue).toBe('Printer is unavailable.');
expect(addAlert).toHaveBeenCalledWith({
type: 'warning',
message: 'Beans was created, but printing has an issue: Printer is unavailable.',
});
});
});