Skip to content

Commit 6cac765

Browse files
committed
fix(component editor): Use the common fixatures and mock a bit less
1 parent d121259 commit 6cac765

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

tests/test_frontend_tkinter_component_editor_base.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import tkinter as tk
1414
from argparse import ArgumentParser
15+
from collections.abc import Generator
1516
from tkinter import ttk
1617
from typing import Union, get_args, get_origin
1718
from unittest.mock import MagicMock, patch
@@ -34,13 +35,13 @@
3435
# pylint: disable=protected-access, too-many-lines, redefined-outer-name, unused-argument, too-few-public-methods
3536

3637

37-
def setup_common_editor_mocks(editor) -> ComponentEditorWindowBase:
38+
def setup_common_editor_mocks(editor, root=None) -> ComponentEditorWindowBase:
3839
"""Set up common mock attributes and methods for editor fixtures."""
3940
# Set up all required attributes manually
40-
editor.root = MagicMock()
41-
editor.main_frame = MagicMock()
41+
editor.root = root if root is not None else MagicMock()
42+
editor.main_frame = ttk.Frame(editor.root) if root is not None else MagicMock()
4243
editor.scroll_frame = MagicMock()
43-
editor.scroll_frame.view_port = MagicMock()
44+
editor.scroll_frame.view_port = ttk.Frame(editor.root) if root is not None else MagicMock()
4445
editor.version = "1.0.0"
4546

4647
# Mock filesystem and methods with proper schema loading
@@ -125,14 +126,14 @@ def test_argument_parser_with_skip_component_editor(self) -> None:
125126

126127

127128
@pytest.fixture
128-
def editor_with_mocked_root() -> ComponentEditorWindowBase:
129+
def editor_with_mocked_root(root) -> Generator[ComponentEditorWindowBase, None, None]:
129130
"""Create a mock ComponentEditorWindowBase for testing."""
130131
# Create the class without initialization
131132
with patch.object(ComponentEditorWindowBase, "__init__", return_value=None):
132133
editor = ComponentEditorWindowBase() # pylint: disable=no-value-for-parameter
133134

134-
# Set up common mocks and helper methods
135-
setup_common_editor_mocks(editor)
135+
# Set up common mocks and helper methods with real root
136+
setup_common_editor_mocks(editor, root)
136137
add_editor_helper_methods(editor)
137138

138139
yield editor
@@ -1046,7 +1047,6 @@ def test_user_sees_introduction_frame_with_explanations(
10461047
mock_intro_frame = MagicMock()
10471048
mock_frame_class.return_value = mock_intro_frame
10481049

1049-
# Mock the methods that would be called
10501050
editor_for_ui_tests._add_explanation_text = MagicMock()
10511051
editor_for_ui_tests._add_vehicle_image = MagicMock()
10521052

tests/test_frontend_tkinter_component_editor_integration.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,18 @@ def test_editor_initialization_process(self, temp_vehicle_dir, root) -> None:
284284
save_component_to_system_templates=False,
285285
)
286286

287-
# Mock only the UI rendering parts, use the real root from conftest.py
287+
# Mock tkinter creation to use the provided session root instead
288288
with (
289289
patch.object(ComponentEditorWindow, "_create_scroll_frame") as mock_scroll_frame,
290290
patch.object(ComponentEditorWindow, "_create_intro_frame") as mock_intro_frame,
291291
patch.object(ComponentEditorWindow, "_create_save_frame") as mock_save_frame,
292292
patch("tkinter.PhotoImage"),
293293
patch("PIL.ImageTk.PhotoImage"),
294+
patch("tkinter.Tk", return_value=root), # Use the session root instead of creating new one
294295
):
295296
# Create the editor with real filesystem and data model
296297
editor = ComponentEditorWindow("1.0.0", filesystem)
297298

298-
# Use the real root from conftest.py
299-
editor.root = root
300-
301299
# Check editor has been properly initialized
302300
assert editor.version == "1.0.0"
303301
assert editor.local_filesystem == filesystem
@@ -342,11 +340,11 @@ def test_editor_with_real_data_model(self, temp_vehicle_dir, root) -> None:
342340
patch.object(ComponentEditorWindow, "_initialize_ui"),
343341
patch("tkinter.PhotoImage"),
344342
patch("PIL.ImageTk.PhotoImage"),
343+
patch("tkinter.Tk", return_value=root), # Use the session root instead of creating new one
345344
):
346345
# Initialize with existing data_model using dependency injection
347346
editor = ComponentEditorWindow("1.0.0", filesystem)
348347
editor.data_model = data_model # Override the data model
349-
editor.root = root # Use the real root from conftest.py
350348

351349
# Test data model integration
352350
assert editor.data_model is data_model

0 commit comments

Comments
 (0)