@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
51 lines (40 loc) • 1.78 kB
text/typescript
import { act, renderHook } from "@testing-library/react-hooks";
import { useScreenState } from "../index";
describe("useScreenState", () => {
it("should return null when screenId is null or undefined", () => {
const { result } = renderHook(() => useScreenState(null));
expect(result.current).toBeNull();
});
it("should return screen state and setSelectedEntry function when screenId is provided", () => {
const screenId = "testScreenId";
const { result } = renderHook(() => useScreenState(screenId));
expect(result.current).toHaveProperty("screen");
expect(result.current).toHaveProperty("setSelectedEntry");
expect(typeof result.current.setSelectedEntry).toBe("function");
});
describe("setSelectedEntry", () => {
it("should set the selected entry when screenId and entryId are provided", () => {
const screenId = "testScreenId";
const entryId = "testEntryId";
const { result } = renderHook(() => useScreenState(screenId));
act(() => {
result.current.setSelectedEntry(entryId);
});
expect(result.current.screen.selectedEntry.id).toBe(entryId);
});
it("should update the selected entry when setSelectedEntry is called again with the same screenId but different entryId", () => {
const screenId = "testScreenId";
const entryId1 = "testEntryId1";
const entryId2 = "testEntryId2";
const { result } = renderHook(() => useScreenState(screenId));
act(() => {
result.current.setSelectedEntry(entryId1);
});
expect(result.current.screen.selectedEntry.id).toBe(entryId1);
act(() => {
result.current.setSelectedEntry(entryId2);
});
expect(result.current.screen.selectedEntry.id).toBe(entryId2);
});
});
});