@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
134 lines (105 loc) • 3.64 kB
JavaScript
import {
time,
timeEnd,
timers,
} from "@applicaster/zapp-react-native-utils/reactHooks/debugging";
describe("Debug utils", () => {
let performanceNowMock;
beforeEach(() => {
jest.useFakeTimers();
// Clear the timers object
Object.keys(timers).forEach((key) => delete timers[key]);
// Mock performance.now()
// eslint-disable-next-line no-undef
performanceNowMock = jest.spyOn(performance, "now");
performanceNowMock.mockReturnValue(0); // Initial value
});
afterEach(() => {
jest.useRealTimers();
jest.restoreAllMocks();
performanceNowMock.mockRestore();
});
describe("Unit Test: time", () => {
it("should create new timer if timer with a label does not exist", () => {
performanceNowMock.mockReturnValue(1000);
time("timer1");
expect(timers["timer1"]).toBeDefined();
expect(timers["timer1"].startTime).toBe(1000);
});
it("should update start time of timer if timer with a label exists", () => {
// First call to time()
performanceNowMock.mockReturnValue(1000);
time("timer1");
const previousStartTime = timers["timer1"].startTime;
// Second call to time()
performanceNowMock.mockReturnValue(2000);
time("timer1");
const currentStartTime = timers["timer1"].startTime;
expect(previousStartTime).toBe(1000);
expect(currentStartTime).toBe(2000);
});
});
describe("Unit Test: timeEnd", () => {
beforeEach(() => {
jest.spyOn(console, "log").mockImplementation(() => {});
});
it("should log elapsed time", () => {
const logSpy = jest.spyOn(console, "log");
performanceNowMock.mockReturnValue(1000);
time("timer1");
performanceNowMock.mockReturnValue(2000);
timeEnd("timer1");
expect(logSpy).toHaveBeenCalledWith(
"timer1: 1000.000ms 2",
expect.any(Object)
);
});
it("should not log elapsed time if accumulative: true in settings", () => {
const logSpy = jest.spyOn(console, "log");
performanceNowMock.mockReturnValue(1000);
time("timer1");
performanceNowMock.mockReturnValue(2000);
timeEnd("timer1", { accumulative: true });
expect(logSpy).not.toHaveBeenCalled();
});
});
describe("Integration Test: time and timeEnd", () => {
beforeEach(() => {
jest.spyOn(console, "log").mockImplementation(() => {});
});
it("should log elapsed time on timeEnd call", () => {
const logSpy = jest.spyOn(console, "log");
performanceNowMock.mockReturnValue(1000);
time("timer1");
performanceNowMock.mockReturnValue(2000);
timeEnd("timer1");
expect(logSpy).toHaveBeenCalledWith(
"timer1: 1000.000ms 2",
expect.objectContaining({
label: "timer1",
settings: undefined,
})
);
});
it("should log total and average time when accumulative timing is complete", () => {
const logSpy = jest.spyOn(console, "log");
// First timing
performanceNowMock.mockReturnValue(1000);
time("timer2");
performanceNowMock.mockReturnValue(2000);
timeEnd("timer2", { accumulative: true });
// Second timing
performanceNowMock.mockReturnValue(3000);
time("timer2");
performanceNowMock.mockReturnValue(5000);
timeEnd("timer2"); // This will log because timer is accumulative (callCount > 1)
expect(logSpy).toHaveBeenCalledWith(
"timer2: 2000.000ms 2",
expect.objectContaining({
label: "timer2",
settings: undefined,
})
);
});
});
});