Skip to content
Open
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,64 @@
/**
* 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 { saveAs } from "file-saver";
import { FileSaverService } from "./file-saver.service";

// Replace the file-saver module wholesale (same precedent as menu.component.spec.ts);
// the service is a thin passthrough, so we assert it forwards to saveAs unchanged.
vi.mock("file-saver", () => ({ saveAs: vi.fn() }));

describe("FileSaverService", () => {
let service: FileSaverService;

beforeEach(() => {
vi.mocked(saveAs).mockClear();
TestBed.configureTestingModule({ providers: [FileSaverService] });
service = TestBed.inject(FileSaverService);
});

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(saveAs).toHaveBeenCalledTimes(1);
expect(saveAs).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(saveAs).toHaveBeenCalledWith("https://example.com/file.csv", "file.csv", undefined);
});
Comment on lines +51 to +55

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

service.saveAs(blob);

expect(saveAs).toHaveBeenCalledWith(blob, undefined, undefined);
});
});
Loading