Skip to content

Commit 8618920

Browse files
ELin2025claude
andcommitted
test(frontend): add TestBed-based tests for HuggingFaceAudioUploadComponent
Add meaningful tests: reject non-audio files, successful upload sets formControl value, and concurrent upload guard prevents race conditions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bb90366 commit 8618920

1 file changed

Lines changed: 66 additions & 2 deletions

File tree

frontend/src/app/workspace/component/hugging-face-audio-upload/hugging-face-audio-upload.component.spec.ts

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,74 @@
1717
* under the License.
1818
*/
1919

20+
import { TestBed } from "@angular/core/testing";
21+
import { HttpClientTestingModule, HttpTestingController } from "@angular/common/http/testing";
22+
import { FormControl } from "@angular/forms";
23+
import { FieldTypeConfig } from "@ngx-formly/core";
2024
import { HuggingFaceAudioUploadComponent } from "./hugging-face-audio-upload.component";
2125

22-
describe("HuggingFaceAudioUploadComponent (unit)", () => {
26+
describe("HuggingFaceAudioUploadComponent", () => {
27+
let component: HuggingFaceAudioUploadComponent;
28+
let httpTestingController: HttpTestingController;
29+
let formControl: FormControl;
30+
31+
function makeFileEvent(file: File | null): Event {
32+
const input = document.createElement("input");
33+
if (file) {
34+
Object.defineProperty(input, "files", { value: [file] });
35+
}
36+
return { target: input } as unknown as Event;
37+
}
38+
39+
beforeEach(async () => {
40+
await TestBed.configureTestingModule({
41+
imports: [HuggingFaceAudioUploadComponent, HttpClientTestingModule],
42+
}).compileComponents();
43+
44+
const fixture = TestBed.createComponent(HuggingFaceAudioUploadComponent);
45+
component = fixture.componentInstance;
46+
formControl = new FormControl("");
47+
component.field = { formControl } as FieldTypeConfig;
48+
httpTestingController = TestBed.inject(HttpTestingController);
49+
});
50+
51+
afterEach(() => {
52+
httpTestingController.verify();
53+
});
54+
2355
it("should be defined", () => {
24-
expect(HuggingFaceAudioUploadComponent).toBeDefined();
56+
expect(component).toBeDefined();
57+
});
58+
59+
it("should reject a non-audio file", async () => {
60+
const file = new File(["data"], "doc.pdf", { type: "application/pdf" });
61+
await component.onFileSelected(makeFileEvent(file));
62+
63+
expect(component.errorMessage).toBe("Choose an audio file.");
64+
expect(formControl.value).toBe("");
65+
});
66+
67+
it("should upload an audio file and set formControl value", async () => {
68+
const file = new File(["audio-data"], "clip.wav", { type: "audio/wav" });
69+
const uploadPromise = component.onFileSelected(makeFileEvent(file));
70+
71+
const req = httpTestingController.expectOne(
72+
r => r.method === "POST" && r.url.includes("/huggingface/upload-audio")
73+
);
74+
req.flush({ path: "/tmp/clip.wav", fileName: "clip.wav" });
75+
await uploadPromise;
76+
77+
expect(formControl.value).toBe("/tmp/clip.wav");
78+
expect(component.fileName).toBe("clip.wav");
79+
expect(component.isUploading).toBe(false);
80+
});
81+
82+
it("should guard against concurrent uploads", async () => {
83+
component.isUploading = true;
84+
const file = new File(["audio-data"], "clip.wav", { type: "audio/wav" });
85+
await component.onFileSelected(makeFileEvent(file));
86+
87+
httpTestingController.expectNone(r => r.url.includes("/huggingface/upload-audio"));
88+
expect(formControl.value).toBe("");
2589
});
2690
});

0 commit comments

Comments
 (0)