Skip to content

Commit

Permalink
Add unit test cases for useStylesInternal hook (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
mashazyu authored Oct 29, 2024
1 parent c8d9946 commit f7c74a1
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions __tests__/hooks/internal/useStylesInternal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { renderHook, act } from "@testing-library/react";
import { expect } from "@jest/globals";

import { useStylesInternal } from "../../../src/hooks/internal/useStylesInternal";
import { useStylesContext } from "../../../src/context/StylesContext";
import { parseConfig } from "../../../src/utils/configParser";

// Mock the necessary context and service
jest.mock("../../../src/context/StylesContext");
jest.mock("../../../src/utils/configParser", () => ({
deepClone: jest.fn(),
getCombinedConfig: jest.fn(),
}));

/**
* Test for useStylesInternal hook.
*/
describe("useStylesInternal Hook", () => {
const setStylesMock = jest.fn();
const stylesMock = {
bodyStyle: {}
}

beforeEach(() => {
jest.clearAllMocks();
(useStylesContext as jest.Mock).mockReturnValue({ styles: stylesMock, setStyles: setStylesMock});
})

// Test to ensure initial values (styels and setStyles) are returned correctly from the hook
it("should return initial values from context", () => {
const { result } = renderHook(() => useStylesInternal());

expect(result.current.styles).toEqual(stylesMock);
expect(result.current.replaceStyles).toEqual(expect.any(Function));
expect(result.current.updateStyles).toEqual(expect.any(Function));
});

it("should call setStyles from context, when styles are updated", () => {
const { result } = renderHook(() => useStylesInternal());
const styles = { tooltipStyle: {} };

act(() => {
result.current.updateStyles(styles);
});
// Argument passed to setStyles is not checked, because it is transformed with
// deepClone() and getCombinedConfig()
expect(setStylesMock).toHaveBeenCalled();
});

it("should call setStyles from context with correct value, when styles are replaced", () => {
const { result } = renderHook(() => useStylesInternal());
const styles = { notificationBadgeStyle: {} };

act(() => {
result.current.replaceStyles(styles);
});

expect(setStylesMock).toHaveBeenCalledWith(styles);
});
});

0 comments on commit f7c74a1

Please sign in to comment.