Skip to content

Commit

Permalink
Fix the frontend test for gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed Nov 21, 2023
1 parent ae70275 commit f8fb748
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
10 changes: 6 additions & 4 deletions gradio/components/gallery.py
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down Expand Up @@ -126,9 +130,7 @@ def __init__(

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:
Expand Down
17 changes: 10 additions & 7 deletions js/app/test/gallery_component_events.spec.ts
@@ -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")
);
});

0 comments on commit f8fb748

Please sign in to comment.