Skip to content

Commit

Permalink
Fix File updating (#3375)
Browse files Browse the repository at this point in the history
* Fix

* Remove line

* Add unit test

* CHANGELOG

* Remove comment
  • Loading branch information
freddyaboulton authored and dawoodkhan82 committed Mar 6, 2023
1 parent 7c86604 commit b1b5d5b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ By [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3297](https://git
- Ensure that the initial empty value for `gr.Dropdown(Multiselect=True)` is an empty list and the initial value for `gr.Dropdown(Multiselect=False)` is an empty string by [@pngwn](https://github.com/pngwn) in [PR 3338](https://github.com/gradio-app/gradio/pull/3338)
- Ensure uploaded images respect the shape property when the canvas is also enabled by [@pngwn](https://github.com/pngwn) in [PR 3351](https://github.com/gradio-app/gradio/pull/3351)
- Ensure that Google Analytics works correctly when gradio apps are created with `analytics_enabled=True` by [@abidlabs](https://github.com/abidlabs) in [PR 3349](https://github.com/gradio-app/gradio/pull/3349)
- Fix bug where files were being re-uploaded after updates by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 3375](https://github.com/gradio-app/gradio/pull/3375)
- Fix error when using backen_fn and custom js at the same time by [@jialeicui](https://github.com/jialeicui) in [PR 3358](https://github.com/gradio-app/gradio/pull/3358)
- Support new embeds for huggingface spaces subdomains by [@pngwn](https://github.com/pngwn) in [PR 3367](https://github.com/gradio-app/gradio/pull/3367)

Expand Down
6 changes: 6 additions & 0 deletions ui/packages/app/src/components/File/File.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
if (_value === null) {
dispatch("change");
pending_upload = false;
} else if (
!(Array.isArray(_value) ? _value : [_value]).every(
(file_data) => file_data.blob
)
) {
pending_upload = false;
} else if (mode === "dynamic") {
let files = (Array.isArray(_value) ? _value : [_value]).map(
(file_data) => file_data.blob!
Expand Down
64 changes: 64 additions & 0 deletions ui/packages/app/src/components/File/File.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { test, describe, assert, expect, afterEach, vi } from "vitest";
import { cleanup, render } from "@gradio/tootils";

import File from "./File.svelte";
import type { LoadingStatus } from "../StatusTracker/types";
import { upload_files } from "../../api";

const loading_status = {
eta: 0,
queue_position: 1,
queue_size: 1,
status: "complete" as LoadingStatus["status"],
scroll_to_output: false,
visible: true,
fn_index: 0
};

describe("File", () => {
afterEach(() => {
cleanup();
vi.restoreAllMocks();
});

test("Uploads with blob", async () => {
vi.mock("../../api", async () => {
return {
upload_files: vi.fn((f) => new Promise((res) => res({})))
};
});

const api = await import("../../api");

render(File, {
loading_status,
label: "file",
// @ts-ignore
value: { name: "freddy.json", data: "{'name': 'freddy'}", blob: vi.fn() },
show_label: true,
mode: "dynamic",
root: "http://localhost:7860",
file_count: "1",
root_url: null
});

expect(api.upload_files).toHaveBeenCalled();
});

test("Does not upload without blob", () => {
const spy = vi.fn(upload_files);

render(File, {
loading_status,
label: "file",
value: { name: "freddy.json", data: "{'name': 'freddy'}" },
show_label: true,
mode: "dynamic",
root: "http://localhost:7860",
file_count: "1",
root_url: null
});

expect(spy).not.toHaveBeenCalled();
});
});

0 comments on commit b1b5d5b

Please sign in to comment.