import { download, getCalendarFile } from "./add-to-calendar-helper";

// Create a mock function
const mockDownload = jest.fn((filename, fileBody) => {
    // Simulate creating an anchor element
    const mockElement = {
        setAttribute: jest.fn(),
        style: { display: "none" },
        click: jest.fn(),
    };
    // Mock the document.createElement method
    document.createElement = jest.fn(() => mockElement);
    // Mock the document.body.appendChild method
    document.body.appendChild = jest.fn();
    // Mock the document.body.removeChild method
    document.body.removeChild = jest.fn();

    // Call the original function with the mock element
    download(filename, fileBody);

    // Return the mock element for further assertions
    return mockElement;
});

// Use the mock function in a test
test("download function", () => {
    // Call the mock function with some arguments
    const mockElement = mockDownload("test.txt", "Hello world");

    // Make assertions about the mock function and the mock element
    expect(mockDownload).toHaveBeenCalledTimes(1);
    expect(mockDownload).toHaveBeenCalledWith("test.txt", "Hello world");
    expect(mockElement.setAttribute).toHaveBeenCalledTimes(2);
    expect(mockElement.setAttribute).toHaveBeenCalledWith(
        "href",
        "data:text/plain;charset=utf-8," + encodeURIComponent("Hello world")
    );
    expect(mockElement.setAttribute).toHaveBeenCalledWith("download", "test.txt");
    expect(mockElement.style.display).toBe("none");
    expect(mockElement.click).toHaveBeenCalledTimes(1);
    expect(document.body.appendChild).toHaveBeenCalledTimes(1);
    expect(document.body.appendChild).toHaveBeenCalledWith(mockElement);
    expect(document.body.removeChild).toHaveBeenCalledTimes(1);
    expect(document.body.removeChild).toHaveBeenCalledWith(mockElement);
});

// Use the mock function in a test
test("getCalendarFile function", () => {
    // Create a mock calFile object
    const mockCalFile = {
        UniqueId: "123456",
        Status: "Busy",
        StartDate: new Date(2023, 9, 11, 12, 0, 0),
        EndDate: new Date(2023, 9, 11, 13, 0, 0),
        Subject: "Meeting with John",
        Location: "Conference Room",
        Body: "Discuss the project progress",
        TimeStamp: "20231011T113118",
    };

    // Call the mock function with the mock calFile object
    const mockCalendarFile = getCalendarFile(mockCalFile);

    // Make assertions about the mock function and the mock calendar file
    expect(mockCalendarFile).toContain("BEGIN:VCALENDAR");
    expect(mockCalendarFile).toContain("VERSION:2.0");
    expect(mockCalendarFile).toContain("BEGIN:VEVENT");
    expect(mockCalendarFile).toContain("DTSTAMP:20231011T113118");
    expect(mockCalendarFile).toContain("UID:123456@safelite.com");
    expect(mockCalendarFile).toContain("PRODID:noreply@safelite.com");
    expect(mockCalendarFile).toContain("DTSTART:20231011T120000");
    expect(mockCalendarFile).toContain("DTEND:20231011T130000");
    expect(mockCalendarFile).toContain("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
    expect(mockCalendarFile).toContain("SUMMARY:Meeting with John");
    expect(mockCalendarFile).toContain("LOCATION:Conference Room");
    expect(mockCalendarFile).toContain("DESCRIPTION:Discuss the project progress");
    expect(mockCalendarFile).toContain("END:VEVENT");
    expect(mockCalendarFile).toContain("END:VCALENDAR");
});
