From 3c61132b86e0a728a2a6be859bc09504a7e10364 Mon Sep 17 00:00:00 2001 From: edescalona Date: Wed, 25 Dec 2024 23:39:42 -0500 Subject: [PATCH] [IMP] Add tests --- .../static/src/views/kanban_renderer.esm.js | 51 ++++++++++--------- stock_barcodes_camera/tests/common.py | 1 + .../tests/test_stock_barcodes.py | 42 +++++++++++++++ 3 files changed, 70 insertions(+), 24 deletions(-) diff --git a/stock_barcodes/static/src/views/kanban_renderer.esm.js b/stock_barcodes/static/src/views/kanban_renderer.esm.js index 452eea6cb72..e93160f2457 100644 --- a/stock_barcodes/static/src/views/kanban_renderer.esm.js +++ b/stock_barcodes/static/src/views/kanban_renderer.esm.js @@ -64,6 +64,32 @@ patch(KanbanRenderer.prototype, "add hotkey", { } } }, + getNextCard(direction, iCard, cards, iGroup, isGrouped) { + let nextCard = null; + switch (direction) { + case "down": + nextCard = iCard < cards[iGroup].length - 1 && cards[iGroup][iCard + 1]; + break; + case "up": + nextCard = iCard > 0 && cards[iGroup][iCard - 1]; + break; + case "right": + if (isGrouped) { + nextCard = iGroup < cards.length - 1 && cards[iGroup + 1][0]; + } else { + nextCard = iCard < cards[0].length - 1 && cards[0][iCard + 1]; + } + break; + case "left": + if (isGrouped) { + nextCard = iGroup > 0 && cards[iGroup - 1][0]; + } else { + nextCard = iCard > 0 && cards[0][iCard - 1]; + } + break; + } + return nextCard; + }, // eslint-disable-next-line complexity // This is copied from the base kanban_renderer. @@ -118,30 +144,7 @@ patch(KanbanRenderer.prototype, "add hotkey", { iGroup = 0; } // Find next card to focus - let nextCard = null; - switch (direction) { - case "down": - nextCard = iCard < cards[iGroup].length - 1 && cards[iGroup][iCard + 1]; - break; - case "up": - nextCard = iCard > 0 && cards[iGroup][iCard - 1]; - break; - case "right": - if (isGrouped) { - nextCard = iGroup < cards.length - 1 && cards[iGroup + 1][0]; - } else { - nextCard = iCard < cards[0].length - 1 && cards[0][iCard + 1]; - } - break; - case "left": - if (isGrouped) { - nextCard = iGroup > 0 && cards[iGroup - 1][0]; - } else { - nextCard = iCard > 0 && cards[0][iCard - 1]; - } - break; - } - + const nextCard = this.getNextCard(direction, iCard, cards, iGroup, isGrouped); if (nextCard && nextCard instanceof HTMLElement) { nextCard.focus(); return true; diff --git a/stock_barcodes_camera/tests/common.py b/stock_barcodes_camera/tests/common.py index 4104e2eb9b4..dce82d4de21 100644 --- a/stock_barcodes_camera/tests/common.py +++ b/stock_barcodes_camera/tests/common.py @@ -11,6 +11,7 @@ def setUpClass(cls): cls.location_hash = ( "#id=17&model=wiz.stock.barcodes.read.inventory&view_type=form" ) + cls.stock_barcodes_action = cls.env["stock.barcodes.action"] def camera_barcode_scanner(self, location_hash=""): if location_hash: diff --git a/stock_barcodes_camera/tests/test_stock_barcodes.py b/stock_barcodes_camera/tests/test_stock_barcodes.py index 26d8a900716..609bf3325ad 100644 --- a/stock_barcodes_camera/tests/test_stock_barcodes.py +++ b/stock_barcodes_camera/tests/test_stock_barcodes.py @@ -18,3 +18,45 @@ def test_location_hash(self): with self.assertRaises(IndexError): invalid_loc_hash = self.location_hash.replace("model=", "modell=") self.camera_barcode_scanner(invalid_loc_hash) + + def test_open_inventory_action_valid_context(self): + test_context = {"default_model": "stock.barcodes.action"} + action = self.stock_barcodes_action.open_inventory_action(test_context) + self.assertIn("res_id", action, "The action should contain a 'res_id'.") + self.assertIn("context", action, "The action should contain a 'context'.") + self.assertEqual( + action["context"], + test_context, + "The context should match the provided one.", + ) + + def test_open_inventory_action_missing_option_group(self): + option_group = self.env.ref( + "stock_barcodes.stock_barcodes_option_group_inventory" + ) + if option_group: + option_group.unlink() + with self.assertRaises( + ValueError, msg="Expected exception for missing option group." + ): + self.stock_barcodes_action.open_inventory_action({}) + + def test_open_inventory_action_camera_barcode_scanner_disabled(self): + self.env["ir.config_parameter"].sudo().set_param( + "stock_barcodes.enable_camera_barcode_scanner", False + ) + action = self.stock_barcodes_action.open_inventory_action({}) + res_id = action.get("res_id") + wiz = self.env["wiz.stock.barcodes.read.inventory"].browse(res_id) + self.assertFalse( + wiz.show_barcode_scanner, "Barcode scanner should be disabled." + ) + + def test_open_inventory_action_camera_barcode_scanner_enabled(self): + self.env["ir.config_parameter"].sudo().set_param( + "stock_barcodes.enable_camera_barcode_scanner", True + ) + action = self.stock_barcodes_action.open_inventory_action({}) + res_id = action.get("res_id") + wiz = self.env["wiz.stock.barcodes.read.inventory"].browse(res_id) + self.assertTrue(wiz.show_barcode_scanner, "Barcode scanner should be enabled.")