-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added a test to ensure the `ThemeProvider` correctly applies the selected `theme` to the document's `root` element. - Mocked `localStorage` to return a predefined `theme` value for testing. - Verified that the appropriate `class` is applied to `document.documentElement` based on the `theme`.
- Loading branch information
1 parent
9250bc9
commit 046a032
Showing
5 changed files
with
378 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,95 @@ | ||
import { fireEvent, render, act } from "@testing-library/react"; | ||
import { useMutation, useQuery } from "@tanstack/react-query"; | ||
import { describe, it, expect, vi } from "vitest"; | ||
import "@testing-library/jest-dom"; | ||
|
||
import QueryProvider from "./query-provider"; | ||
|
||
const DefaultMockComponent = () => { | ||
const { data } = useQuery({ | ||
queryKey: ["testQuery"], | ||
queryFn: async () => await Promise.resolve("Fetched data"), | ||
}); | ||
|
||
return <p>{data}</p>; | ||
}; | ||
|
||
const SuccessMockComponent = () => { | ||
const { mutate } = useMutation({ | ||
mutationKey: ["testQuery"], | ||
mutationFn: async () => await Promise.resolve("Success"), | ||
onSuccess: (data) => console.log(data), | ||
}); | ||
|
||
return <button onClick={() => mutate()} />; | ||
}; | ||
|
||
const ErrorMockComponent = () => { | ||
const { mutate } = useMutation({ | ||
mutationKey: ["testQuery"], | ||
mutationFn: async () => await Promise.reject(new Error("Error")), | ||
onError: ({ message }) => console.log(message), | ||
}); | ||
|
||
return <button onClick={() => mutate()} />; | ||
}; | ||
|
||
describe("Query Provider", () => { | ||
it("renders without crashing", () => { | ||
const label = "Hello world!"; | ||
|
||
const { getByText } = render( | ||
<QueryProvider> | ||
<p>{label}</p> | ||
</QueryProvider> | ||
); | ||
|
||
expect(getByText(label)).toBeInTheDocument(); | ||
}); | ||
|
||
it("should render fetched data", async () => { | ||
const { findByText } = render( | ||
<QueryProvider> | ||
<DefaultMockComponent /> | ||
</QueryProvider> | ||
); | ||
|
||
expect(await findByText("Fetched data")).toBeInTheDocument(); | ||
}); | ||
|
||
it("should logs 'Success' on mutation call", async () => { | ||
const consoleLogSpy = vi.spyOn(console, "log"); | ||
|
||
const { getByRole } = render( | ||
<QueryProvider> | ||
<SuccessMockComponent /> | ||
</QueryProvider> | ||
); | ||
|
||
const button = getByRole("button"); | ||
|
||
await act(async () => { | ||
fireEvent.click(button); | ||
}); | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledWith("Success"); | ||
}); | ||
|
||
it("should logs 'Error' on mutation call", async () => { | ||
const consoleLogSpy = vi.spyOn(console, "log"); | ||
|
||
const { getByRole } = render( | ||
<QueryProvider> | ||
<ErrorMockComponent /> | ||
</QueryProvider> | ||
); | ||
|
||
const button = getByRole("button"); | ||
|
||
await act(async () => { | ||
fireEvent.click(button); | ||
}); | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledWith("Error"); | ||
}); | ||
}); |
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,71 @@ | ||
import { describe, it, expect, vi, beforeAll } from "vitest"; | ||
import { render } from "@testing-library/react"; | ||
import "@testing-library/jest-dom"; | ||
|
||
import { ThemeProvider, ThemeProviderContext } from "./theme-provider"; | ||
|
||
describe("Theme Provider", () => { | ||
beforeAll(() => { | ||
Object.defineProperty(window, "matchMedia", { | ||
writable: true, | ||
value: (query: string) => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addEventListener: () => {}, | ||
removeEventListener: () => {}, | ||
dispatchEvent: () => {}, | ||
}), | ||
}); | ||
}); | ||
|
||
it("should render without any issues", () => { | ||
const label = "Hello world!"; | ||
|
||
const { getByText } = render( | ||
<ThemeProvider> | ||
<p>{label}</p> | ||
</ThemeProvider> | ||
); | ||
|
||
expect(getByText(label)).toBeInTheDocument(); | ||
}); | ||
|
||
it("sets the initial theme from localStorage", () => { | ||
const localStorageMock = { | ||
getItem: vi.fn().mockReturnValue("light"), | ||
setItem: vi.fn(), | ||
}; | ||
|
||
Object.defineProperty(window, "localStorage", { value: localStorageMock }); | ||
|
||
const { getByTestId } = render( | ||
<ThemeProvider> | ||
<ThemeProviderContext.Consumer> | ||
{({ theme }) => <p data-testid="theme">{theme}</p>} | ||
</ThemeProviderContext.Consumer> | ||
</ThemeProvider> | ||
); | ||
|
||
expect(getByTestId("theme")).toHaveTextContent("light"); | ||
}); | ||
|
||
it("applies the theme to the document root", () => { | ||
const localStorageMock = { | ||
getItem: vi.fn().mockReturnValue("dark"), | ||
setItem: vi.fn(), | ||
}; | ||
|
||
Object.defineProperty(window, "localStorage", { value: localStorageMock }); | ||
|
||
render( | ||
<ThemeProvider> | ||
<p>Test</p> | ||
</ThemeProvider> | ||
); | ||
|
||
const root = window.document.documentElement; | ||
|
||
expect(root.classList.contains("dark")).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.