Skip to content

Commit

Permalink
Add tests for vanilla Django RawID fields
Browse files Browse the repository at this point in the history
Since we overwrite the Django popup lookup JS, we need to make sure we don't break the vanilla experience.
  • Loading branch information
bartTC committed Apr 21, 2024
1 parent 200823e commit 6f128e5
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions dynamic_raw_id/tests/test_django_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Test vanilla Django raw id fields since we overwrite it's Javascript
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest
from selenium.webdriver.common.by import By

if TYPE_CHECKING:
from selenium.webdriver.firefox.webdriver import WebDriver


@pytest.mark.usefixtures("_add_page", "_sample_primary_keys")
def test_widgets(selenium: WebDriver) -> None:
"""
Test the variations of the Dynamic Raw ID Widget.
The tests select the "Lookup" icon, choose the first element in the result set,
and test that the selected element name appears next to the field.
Doing this in a all-in-one testcase, rather a parameterized test, so it's much
faster.
"""

# Input Field ID, Select Value, Test Value
tests = (
("rawid_fk", "user1", "user1"),
("rawid_fk_limited", "admin", "admin"),
("rawid_many", "user1", None),
("rawid_many", "user2", None), # Second click on Many2Many
)

for id_value, select_value, _ in tests:
# Select the "Lookup" icon next to the field
selenium.find_element(By.ID, f"lookup_id_{id_value}").click()

# Activate the popup window with the `window.name = <window_id>`
selenium.switch_to.window(selenium.window_handles[1])

# Select the result element matching the link text
selenium.find_element(By.LINK_TEXT, select_value).click()

# Activate the default window
selenium.switch_to.window(selenium.window_handles[0])

# Click the save button and wait for page reload
selenium.find_element(By.NAME, "_continue").click()
selenium.implicitly_wait(0.5) # Wait a bit for page reload

# Test that all selected ForeignKeys are now displayed after reload.
for id_value, _, test_value in tests:
# ManyToMany fields don't display a label in Vanilla Django raw id fields.
if test_value is None:
continue

label = selenium.find_element(By.CSS_SELECTOR, f"#id_{id_value} ~ strong")
assert label.text == test_value

0 comments on commit 6f128e5

Please sign in to comment.