-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test cases for useStylesInternal hook (#250)
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |