|
17 | 17 | * under the License. |
18 | 18 | */ |
19 | 19 |
|
| 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"; |
20 | 24 | import { HuggingFaceAudioUploadComponent } from "./hugging-face-audio-upload.component"; |
21 | 25 |
|
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 | + |
23 | 55 | 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(""); |
25 | 89 | }); |
26 | 90 | }); |
0 commit comments