Back to Repositories

Validating UI Component Rendering and Event Handling in gradio-app

This test suite validates rendering functionality and event handling in the Gradio application, focusing on interactive UI components and real-time updates. The tests cover button interactions, event listeners, and dynamic rendering behaviors.

Test Coverage Overview

The test suite provides comprehensive coverage of rendering and event handling mechanisms.

Key areas tested include:
  • Button click event listeners and state updates
  • Time-based rendering with ‘every=’ functionality
  • Event and selection data processing
  • Example functionality validation
  • Dynamic UI updates and value propagation

Implementation Analysis

The testing approach utilizes Playwright’s page object model for UI interaction simulation. Tests employ async/await patterns for handling asynchronous operations and leverage built-in assertions for validating component states and values.

Key implementation features:
  • Page object selectors for element targeting
  • Timeout handling for time-sensitive tests
  • Value comparison and state validation
  • Click event simulation and response verification

Technical Details

Testing stack components:
  • Playwright for browser automation
  • Custom test utilities from @self/tootils
  • Expect assertions for validation
  • Page object model for element interaction
  • Async/await for handling asynchronous operations

Best Practices Demonstrated

The test suite exhibits strong testing practices through organized, focused test cases that validate specific functionality independently.

Notable practices include:
  • Descriptive test names indicating purpose
  • Isolated test scenarios
  • Proper async/await usage
  • Consistent element selection strategies
  • Clear assertions with specific expectations

gradio-app/gradio

js/spa/test/render_tests.spec.ts

            
import { test, expect } from "@self/tootils";

test("Test button.click listener works in render", async ({ page }) => {
	const slider = page.getByLabel("number input for Box Count");
	await slider.fill("6");
	await page.getByRole("button", { name: "Create Boxes" }).click();
	await expect(page.getByLabel("Count 4")).toHaveValue("3");
});

test("Test every= works in render", async ({ page }) => {
	const timebox = page.getByLabel("Time");
	const box_1 = page.getByLabel("Render 1");
	const slider = page.getByLabel("number input for Slider");
	let timebox_value_start = parseFloat(await timebox.inputValue());
	let box_1_value_start = parseFloat(await box_1.inputValue());

	await page.waitForTimeout(500);
	let timebox_value_end = parseFloat(await timebox.inputValue());
	expect(timebox_value_end).toBeGreaterThan(timebox_value_start);

	await page.waitForTimeout(500);
	await slider.fill("4");
	const box_2 = page.getByLabel("Render 2");
	let box_2_value_start = parseFloat(await box_2.inputValue());
	expect(box_2_value_start).toBeGreaterThan(box_1_value_start);

	await page.waitForTimeout(500);
	let box_2_value_end = parseFloat(await box_2.inputValue());
	expect(box_2_value_end).toBeGreaterThan(box_2_value_start);
});

test("Test event/selection data works in render", async ({ page }) => {
	const selected_button = page.getByLabel("Selected Button");
	const slider = page.getByLabel("number input for Slider");
	await slider.fill("3");
	await page.getByRole("button", { name: "Button 2" }).click();

	await expect(selected_button).toHaveValue("Button 2");

	const selected_chat = page.getByLabel("Selected Chat");
	await page.getByText("Hi").click();
	await expect(selected_chat).toHaveValue("[0, 1]");
});

test("Test event/selection data can trigger render", async ({ page }) => {
	await page.getByText("chat3").click();
	const selected_chat = page.getByLabel("Trigger Index");
	await expect(selected_chat).toHaveValue("[1, 0]");
});

test("Test examples work in render", async ({ page }) => {
	await page.getByRole("button", { name: "test" }).click();
	await expect(page.getByLabel("input", { exact: true })).toHaveValue("test");
	await page.getByRole("button", { name: "def", exact: true }).click();
	await expect(page.getByLabel("little textbox", { exact: true })).toHaveValue(
		"def"
	);
});