Fix the frontend test for gallery

This commit is contained in:
Yuichiro Tachibana (Tsuchiya) 2023-11-21 15:57:58 +09:00
parent 4e2aa3fff1
commit 007caa23e7
2 changed files with 16 additions and 11 deletions

View File

@ -3,7 +3,7 @@
from __future__ import annotations
from pathlib import Path
from typing import Any, Callable, List, Literal, Optional
from typing import Any, Callable, List, Literal, Optional, Tuple, Union
from urllib.parse import urlparse
import numpy as np
@ -28,6 +28,10 @@ class GalleryData(GradioRootModel):
root: List[GalleryImage]
GalleryImageType = Union[np.ndarray, _Image.Image, Path, str]
CaptionedGalleryImageType = Tuple[GalleryImageType, str]
@document()
class Gallery(Component):
"""
@ -126,9 +130,7 @@ class Gallery(Component):
def postprocess(
self,
value: list[np.ndarray | _Image.Image | str]
| list[tuple[np.ndarray | _Image.Image | str, str]]
| None,
value: list[GalleryImageType | CaptionedGalleryImageType] | None,
) -> GalleryData:
"""
Parameters:

View File

@ -1,24 +1,27 @@
import { test, expect } from "@gradio/tootils";
const regexLocalHostFileURL = (basename: string) =>
new RegExp(`^http://localhost:7860/(\\w+/)*file=(.*\\/)?${basename}$`);
test("Gallery preview mode displays all images correctly.", async ({
page
}) => {
await page.getByRole("button", { name: "Run" }).click();
await page.getByLabel("Thumbnail 2 of 3").click();
await expect(
await page.getByTestId("detailed-image").getAttribute("src")
).toEqual("https://gradio-builds.s3.amazonaws.com/assets/lite-logo.png");
expect(await page.getByTestId("detailed-image").getAttribute("src")).toMatch(
regexLocalHostFileURL("lite-logo.png")
);
await expect(
await page.getByTestId("thumbnail 1").getAttribute("src")
).toEqual("https://gradio-builds.s3.amazonaws.com/assets/cheetah-003.jpg");
expect(await page.getByTestId("thumbnail 1").getAttribute("src")).toMatch(
regexLocalHostFileURL("cheetah-003.jpg")
);
});
test("Gallery select event returns the right value", async ({ page }) => {
await page.getByRole("button", { name: "Run" }).click();
await page.getByLabel("Thumbnail 2 of 3").click();
await expect(page.getByLabel("Select Data")).toHaveValue(
"https://gradio-builds.s3.amazonaws.com/assets/lite-logo.png"
regexLocalHostFileURL("lite-logo.png")
);
});