Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an argument to expand moreInfo of an input #441

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pytest_splunk_addon_ui_smartx/components/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,15 +487,19 @@ def get_count_number(self):
row_count = self.get_count_title()
return int(re.search(r"\d+", row_count).group())

def get_more_info(self, name, cancel=True):
def get_more_info(self, name, cancel=True, expand_row=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the point of this flag. It seems like leaking abstraction for the method that should return the content of _More Info`. Why should caller of this method know about if it is open or close?

Here is my suggestion

  1. check if the row is expanded with attribute aria-expanded.
  2. if the value is 'false', click on the arrow
  3. if there is a Loading inside, wait it disappear (in case the custom row is used), it loads in runtime

Here is how I would write in UI unit tests

    const arrow = within(inputRow).getByRole('cell', { name: /expandable/i });
    const isExpanded = arrow.getAttribute('aria-expanded');
    if (isExpanded === 'false') {
        await userEvent.click(arrow);
    }
    await waitFor(() => expect(arrow.getAttribute('aria-expanded')).not.toBe('false'));
    const loading = screen.queryByText('Loading...');
    if (loading) {
        await waitForElementToBeRemoved(loading);
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We added the flag to ensure that the moreInfo section remains expanded based on the use case. This allows any changes made to the input's row to be immediately reflected in the custom row section without collapsing and re-expanding the moreInfo section.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have written the unit test case in my PR itself, so we don't need this change in smartx. Hence, we can close this PR.

"""
Returns the text from the more info field within a tables row
:param name: Str row name
:param cancel: Bool Whether or not to click cancel after getting the info
:param expand_row: Bool which specifies whether to expand `more_info` or not.
:return: Dict The information found when opening the info table on a row in the table
"""
_row = self._get_row(name)
_row.find_element(*list(self.elements["more_info"]._asdict().values())).click()
if expand_row:
_row.find_element(
*list(self.elements["more_info"]._asdict().values())
).click()
keys = self.more_info_row.find_elements(
*list(self.elements["more_info_key"]._asdict().values())
)
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
from unittest.mock import MagicMock, patch
from selenium.webdriver.common.by import By
from pytest_splunk_addon_ui_smartx.components.table import Table


@pytest.fixture
def mock_table():
mock_browser = MagicMock()

# Mocking container with a `select` attribute
mock_container = MagicMock()
mock_container.select = "#test-container"
table = Table(mock_browser, mock_container)
return table


@patch("pytest_splunk_addon_ui_smartx.components.table.Table._get_row")
def test_get_more_info_expand_row_false(mock_get_row, mock_table):
mock_row = MagicMock()
mock_get_row.return_value = mock_row

mock_more_info_element = MagicMock()
mock_row.find_element.return_value = mock_more_info_element

# Calling the function with expand_row=False and cancel=False as we are testing for expand_row parameter
mock_table.get_more_info(name="Test Row", cancel=False, expand_row=False)

mock_get_row.assert_called_with("Test Row")
mock_row.find_element.assert_not_called()
mock_more_info_element.click.assert_not_called()


@patch("pytest_splunk_addon_ui_smartx.components.table.Table._get_row")
def test_get_more_info_expand_row_true(mock_get_row, mock_table):
mock_row = MagicMock()
mock_get_row.return_value = mock_row

mock_more_info_element = MagicMock()
mock_row.find_element.return_value = mock_more_info_element

mock_key1 = MagicMock()
mock_key1.get_attribute.return_value = " Key1 "
mock_key2 = MagicMock()
mock_key2.get_attribute.return_value = " Key2 "

mock_value1 = MagicMock()
mock_value1.get_attribute.return_value = " Value1 "
mock_value2 = MagicMock()
mock_value2.get_attribute.return_value = " Value2 "

mock_table.more_info_row.find_elements.side_effect = [
[mock_key1, mock_key2],
[mock_value1, mock_value2],
]

# Call the function with expand_row=True and cancel=False as we are testing for expand_row parameter
result = mock_table.get_more_info(name="Test Row", cancel=False, expand_row=True)

mock_get_row.assert_called_with("Test Row")
mock_more_info_element.click.assert_called_once()
assert result == {"Key1": "Value1", "Key2": "Value2"}
Loading