Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { TestBed } from "@angular/core/testing";
import { FileSaverService } from "./file-saver.service";

// Stub saveAsFn (the call-out to file-saver) and assert saveAs forwards its arguments through.
describe("FileSaverService", () => {
let service: FileSaverService;
let saveAsFn: ReturnType<typeof vi.fn>;

beforeEach(() => {
TestBed.configureTestingModule({ providers: [FileSaverService] });
service = TestBed.inject(FileSaverService);
saveAsFn = vi.fn();
(service as unknown as { saveAsFn: unknown }).saveAsFn = saveAsFn;
});

it("should be created", () => {
expect(service).toBeTruthy();
});

it("delegates a Blob payload to saveAs with filename and options", () => {
const blob = new Blob(["hello"], { type: "text/plain" });
const options = { autoBom: true };

service.saveAs(blob, "greeting.txt", options);

expect(saveAsFn).toHaveBeenCalledTimes(1);
expect(saveAsFn).toHaveBeenCalledWith(blob, "greeting.txt", options);
});

it("forwards a string payload (e.g. a URL) unchanged", () => {
service.saveAs("https://example.com/file.csv", "file.csv");

expect(saveAsFn).toHaveBeenCalledWith("https://example.com/file.csv", "file.csv", undefined);
});

it("passes undefined through when filename and options are omitted", () => {
const blob = new Blob([]);

service.saveAs(blob);

expect(saveAsFn).toHaveBeenCalledWith(blob, undefined, undefined);
});

it("propagates errors thrown by saveAs instead of swallowing them", () => {
const failure = new Error("save failed");
saveAsFn.mockImplementationOnce(() => {
throw failure;
});

expect(() => service.saveAs(new Blob(["oops"]), "oops.txt")).toThrow(failure);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import * as FileSaver from "file-saver";
providedIn: "root",
})
export class FileSaverService {
// Stubbable delegate to file-saver's saveAs.
protected saveAsFn: typeof FileSaver.saveAs = FileSaver.saveAs;

saveAs(data: Blob | string, filename?: string, options?: FileSaver.FileSaverOptions): void {
FileSaver.saveAs(data, filename, options);
this.saveAsFn(data, filename, options);
}
}
Loading