From ca9517508df72b2df28f9e1e779aa84f09d37feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Bregar?= Date: Sat, 11 Apr 2026 02:33:47 +0200 Subject: [PATCH] Remove redundant `stockType` field mapping, refactor location handling in identifier mapper, and update related tests. --- .../labels/identifier-lookup-mapper.js | 11 ++++----- .../labels/identifier-lookup-mapper.test.js | 24 +++++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/features/labels/identifier-lookup-mapper.js b/src/features/labels/identifier-lookup-mapper.js index 5a61248..01731b0 100644 --- a/src/features/labels/identifier-lookup-mapper.js +++ b/src/features/labels/identifier-lookup-mapper.js @@ -118,7 +118,6 @@ export function mapLookupItemToForm({ setField('identifierCode', textValue('identifier_code')); setField('name', textValue('name')); setField('description', textValue('description')); - setField('stockType', textValue('stock_type')); setField('level', textValue('level')); setField('quantity', numberValue('quantity_initial')); setField('uom', textValue('uom_symbol')); @@ -138,14 +137,14 @@ export function mapLookupItemToForm({ setField('expirationDate', ''); } - const canSetLocation = !String(nextForm.locationId || '').trim(); const locationUuid = nonEmptyText(lookupItem?.location_initial_uuid_b64); - if (canSetLocation && locationUuid) { + if (locationUuid) { const matchingLocation = locations.find((entry) => entry.uuid_b64 === locationUuid); if (matchingLocation) { - nextForm.locationId = String(matchingLocation.id); - nextLocationSearch = matchingLocation.name; - didUpdate = true; + setField('locationId', String(matchingLocation.id)); + if (nextLocationSearch !== matchingLocation.name) { + nextLocationSearch = matchingLocation.name; + } } } diff --git a/tests/features/labels/identifier-lookup-mapper.test.js b/tests/features/labels/identifier-lookup-mapper.test.js index 229ea6a..1e14fcd 100644 --- a/tests/features/labels/identifier-lookup-mapper.test.js +++ b/tests/features/labels/identifier-lookup-mapper.test.js @@ -65,7 +65,7 @@ describe('identifier lookup form mapper', () => { expect(result.form.name).toBe('Yogurt'); expect(result.form.search).toBe('Yogurt'); expect(result.form.description).toBe('Plain yogurt'); - expect(result.form.stockType).toBe('measured'); + expect(result.form.stockType).toBe('binary'); expect(result.form.level).toBe('low'); expect(result.form.quantity).toBe('0'); expect(result.form.uom).toBe('ml'); @@ -124,7 +124,7 @@ describe('identifier lookup form mapper', () => { expect(result.form.name).toBe(''); expect(result.form.search).toBe(''); expect(result.form.description).toBe(''); - expect(result.form.stockType).toBe(''); + expect(result.form.stockType).toBe('descriptive'); expect(result.form.level).toBe(''); expect(result.form.quantity).toBe(''); expect(result.form.uom).toBe(''); @@ -146,7 +146,7 @@ describe('identifier lookup form mapper', () => { expect(days).toBe(10); }); - it('updates location only when current form location is empty', () => { + it('updates location when lookup provides one', () => { const lookupItem = { location_initial_uuid_b64: 'loc-fridge', }; @@ -165,9 +165,23 @@ describe('identifier lookup form mapper', () => { locations, }); - expect(withExistingLocation.form.locationId).toBe('8'); - expect(withExistingLocation.locationSearch).toBeNull(); + expect(withExistingLocation.form.locationId).toBe('5'); + expect(withExistingLocation.locationSearch).toBe('Fridge'); expect(withoutLocation.form.locationId).toBe('5'); expect(withoutLocation.locationSearch).toBe('Fridge'); }); + + it('keeps location unchanged when lookup location is null', () => { + const form = createForm({ locationId: '9' }); + const result = mapLookupItemToForm({ + form, + lookupItem: { + location_initial_uuid_b64: null, + }, + locations: [{ id: 5, uuid_b64: 'loc-fridge', name: 'Fridge' }], + }); + + expect(result.form.locationId).toBe('9'); + expect(result.locationSearch).toBeNull(); + }); });