2Tests for the <chives-image> web component.
6from pathlib import Path
8from playwright.sync_api import expect, Page
11from chives.media import create_image_entity
14@pytest.fixture(autouse=True)
15def setup_chives_image_component(page: Page) -> None:
17 Intercept the page, open a blank state, and load the chives-image
20 page.goto("about:blank")
22 with open("javascript/chives-image.js") as f:
23 component_js = f.read()
25 page.evaluate(component_js)
28def test_render_basic_image(page: Page) -> None:
30 The <chives-image> component can render a basic image entity.
32 e = create_image_entity(path="tests/fixtures/media/470906.png")
35 const elem = document.createElement("chives-image");
36 elem.imageEntity = %s;
37 document.body.appendChild(elem);
42 chives_elem = page.locator("chives-image")
44 img_elem = chives_elem.locator("img")
45 expect(img_elem).to_be_visible()
46 assert (img_elem).get_attribute("src") == "tests/fixtures/media/470906.png"
47 assert (img_elem).get_attribute("alt") is None
50def test_render_thumbnail(page: Page, tmp_path: Path) -> None:
52 If you select the `thumbnail` variant, the <chives-image> component
53 uses the thumbnail URL.
55 e = create_image_entity(
56 path="tests/fixtures/media/470906.png",
64 const elem = document.createElement("chives-image");
65 elem.imageEntity = %s;
66 elem.variant = 'thumbnail';
67 document.body.appendChild(elem);
72 chives_elem = page.locator("chives-image")
74 img_elem = chives_elem.locator("img")
75 expect(img_elem).to_be_visible()
76 assert (img_elem).get_attribute("src") == e["thumbnail_path"]
79def test_skips_thumbnail_if_unavailable(page: Page) -> None:
81 If you select the `thumbnail` variant but there's no thumbnail path
82 available, the component falls back to the full-sized path.
84 e = create_image_entity(path="tests/fixtures/media/470906.png")
87 const elem = document.createElement("chives-image");
88 elem.imageEntity = %s;
89 elem.variant = 'thumbnail';
90 document.body.appendChild(elem);
95 chives_elem = page.locator("chives-image")
97 img_elem = chives_elem.locator("img")
98 expect(img_elem).to_be_visible()
99 assert (img_elem).get_attribute("src") == "tests/fixtures/media/470906.png"
102def test_set_thumbnail_path(page: Page) -> None:
104 If you set an explicit thumbnailPath, the <chives-image> component
105 uses the thumbnail URL.
107 e = create_image_entity(path="tests/fixtures/media/470906.png")
110 const elem = document.createElement("chives-image");
111 elem.imageEntity = %s;
112 elem.thumbnailPath = 'https://example.com/thumbnail.png';
113 document.body.appendChild(elem);
118 chives_elem = page.locator("chives-image")
120 img_elem = chives_elem.locator("img")
121 expect(img_elem).to_be_visible()
122 assert (img_elem).get_attribute("src") == "https://example.com/thumbnail.png"
125def test_set_href(page: Page) -> None:
127 If you set an href, the <chives-image> component links to that URL.
129 e = create_image_entity(path="tests/fixtures/media/470906.png")
132 const elem = document.createElement("chives-image");
133 elem.imageEntity = %s;
134 elem.href = 'https://example.com/';
135 document.body.appendChild(elem);
140 chives_elem = page.locator("chives-image")
142 a_elem = chives_elem.locator("a")
143 expect(a_elem).to_be_visible()
144 assert (a_elem).get_attribute("href") == "https://example.com/"
147def test_sets_alt_text(page: Page) -> None:
149 If the image entity has alt text, it's added as an `alt` attribute
150 on the `<img>` element.
152 e = create_image_entity(
153 path="tests/fixtures/media/470906.png", alt_text="Some coloured red squares"
157 const elem = document.createElement("chives-image");
158 elem.imageEntity = %s;
159 document.body.appendChild(elem);
164 chives_elem = page.locator("chives-image")
166 img_elem = chives_elem.locator("img")
167 expect(img_elem).to_be_visible()
168 assert (img_elem).get_attribute("alt") == "Some coloured red squares"
171@pytest.mark.parametrize(
174 ("tests/fixtures/media/470906.png", "square"),
175 ("tests/fixtures/media/Landscape_0.jpg", "landscape"),
176 ("tests/fixtures/media/Portrait_0.jpg", "portrait"),
179def test_sets_orientation(page: Page, path: str, orientation: str) -> None:
181 The <chives-image> element sets a data-orientation attribute.
183 e = create_image_entity(path)
186 const elem = document.createElement("chives-image");
187 elem.imageEntity = %s;
188 document.body.appendChild(elem);
193 chives_elem = page.locator("chives-image")
195 wrapper_elem = chives_elem.locator("div")
196 expect(wrapper_elem).to_be_visible()
197 assert (wrapper_elem).get_attribute("data-orientation") == orientation
200def test_animated_gif(page: Page) -> None:
202 The <chives-image> element can render an animated GIF.
204 e = create_image_entity("tests/fixtures/media/electric_field.gif")
207 const elem = document.createElement("chives-image");
208 elem.imageEntity = %s;
209 document.body.appendChild(elem);
214 chives_elem = page.locator("chives-image")
216 img_elem = chives_elem.locator("img")
217 expect(img_elem).to_be_visible()
218 assert (img_elem).get_attribute("src") == e["path"]
221def test_animated_gif_with_video_thumbnail(page: Page, tmp_path: Path) -> None:
223 The <chives-image> element can render an animated GIF which has
226 e = create_image_entity(
227 "tests/fixtures/media/electric_field.gif",
235 const elem = document.createElement("chives-image");
236 elem.imageEntity = %s;
237 elem.variant = 'thumbnail';
238 document.body.appendChild(elem);
243 chives_elem = page.locator("chives-image")
245 video_elem = chives_elem.locator("video")
246 expect(video_elem).to_be_visible()
247 assert (video_elem).get_attribute("src") == e["thumbnail_path"]