gradio/ui/packages/app/test/blocks-basic.spec.ts
pngwn 2b0898b9a2
Scroll to output (#1077)
* implement loader + scroll into view

* changes

* refactor components to use Block inside the app

* cleanup

* cleanup

* implement all changes for all changes for all relevant components

* fix formatting

* demos

* add status tracker to every component

* fix tests

* fix unti test flake: randInt

* cleanup CI

* fix CI

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
2022-04-26 15:48:39 +01:00

74 lines
1.8 KiB
TypeScript

import { test, expect, Page } from "@playwright/test";
function mock_demo(page: Page, demo: string) {
return page.route("**/config", (route) => {
return route.fulfill({
headers: {
"Access-Control-Allow-Origin": "*"
},
path: `../../../demo/${demo}/config.json`
});
});
}
function mock_api(page: Page, body: Array<unknown>) {
return page.route("**/api/predict/", (route) => {
const id = JSON.parse(route.request().postData()!).fn_index;
return route.fulfill({
headers: {
"Access-Control-Allow-Origin": "*"
},
body: JSON.stringify({
data: body[id]
})
});
});
}
test("renders the correct elements", async ({ page }) => {
await mock_demo(page, "blocks_xray");
await page.goto("http://localhost:3000");
const description = await page.locator(".output-markdown");
await expect(description).toContainText("Detect Disease From Scan");
const checkboxes = await page.locator("data-testid=checkbox-group");
await expect(checkboxes).toContainText("Covid Malaria Lung Cancer");
const tabs = await page.locator("button", { hasText: /X-ray|CT Scan/ });
await expect(tabs).toHaveCount(2);
});
test("can run an api request and display the data", async ({ page }) => {
await mock_demo(page, "blocks_xray");
await mock_api(page, [
[
{
Covid: 0.75,
"Lung Cancer": 0.25
}
],
[
{
Covid: 0.75,
"Lung Cancer": 0.25
}
]
]);
await page.goto("http://localhost:3000");
await page.check("label:has-text('Covid')");
await page.check("label:has-text('Lung Cancer')");
const run_button = await page.locator("button", { hasText: /Run/ });
await Promise.all([
run_button.click(),
page.waitForResponse("**/api/predict/")
]);
const json = await page.locator("data-testid=json");
await expect(json).toContainText(`Covid: 0.75, Lung Cancer: 0.25`);
});