Files
calendar/packages/features/bookings/Booker/utils/lastBookingResponse.test.ts
T
8194ffcd07 feat: Autofill feature (#15437)
* feat: Autofill feature

* Revert "feat: Autofill feature"

This reverts commit ac94257cbb516e140ed550ae804e725ca5d036ca.

* feat: store prev response in localStorage

* review fixes

* add tests

* remove unused import

---------

Co-authored-by: Hariom Balhara <hariombalhara@gmail.com>
Co-authored-by: unknown <adhabal2002@gmail.com>
2024-11-18 20:04:02 +05:30

66 lines
2.0 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
import { localStorage } from "@calcom/lib/webstorage";
import { setLastBookingResponse, getLastBookingResponse } from "./lastBookingResponse";
vi.mock("@calcom/lib/webstorage", () => ({
localStorage: {
getItem: vi.fn(),
setItem: vi.fn(),
},
}));
describe("Last Booking Response", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("setLastBookingResponse", () => {
it("should not store anything if responses is null", () => {
setLastBookingResponse(null);
expect(localStorage.setItem).not.toHaveBeenCalled();
});
it("should store only allowed responses in localStorage", () => {
const responses = {
email: "test@example.com",
name: "John Doe",
age: 30,
};
setLastBookingResponse(responses);
expect(localStorage.setItem).toHaveBeenCalledWith(
"lastBookingResponse",
JSON.stringify({ email: "test@example.com", name: "John Doe" })
);
});
});
describe("getLastBookingResponse", () => {
it("should return an empty object if there is no last booking response", () => {
(localStorage.getItem as jest.Mock).mockReturnValue(null);
const result = getLastBookingResponse();
expect(result).toEqual({});
});
it("should return parsed response if valid JSON is present", () => {
const mockResponse = JSON.stringify({ email: "test@example.com", name: "John Doe" });
(localStorage.getItem as jest.Mock).mockReturnValue(mockResponse);
const result = getLastBookingResponse();
expect(result).toEqual({ email: "test@example.com", name: "John Doe" });
});
it("should return an empty object if JSON is invalid", () => {
const invalidJson = "{ email: 'test@example.com' ";
(localStorage.getItem as jest.Mock).mockReturnValue(invalidJson);
const result = getLastBookingResponse();
expect(result).toEqual({});
});
});
});