diff --git a/ui/src/Components/Theme/index.js b/ui/src/Components/Theme/index.js
index 9617189a3..e5d0702a7 100644
--- a/ui/src/Components/Theme/index.js
+++ b/ui/src/Components/Theme/index.js
@@ -1,4 +1,4 @@
-import React, { Component } from "react";
+import React, { useEffect } from "react";
import ReactDOM from "react-dom";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
@@ -40,28 +40,19 @@ const Placeholder = () => {
const ThemeContext = React.createContext();
-class BodyTheme extends Component {
- onToggleBodyClass = (isDark) => {
- document.body.classList.toggle("theme-dark", isDark);
- document.body.classList.toggle("theme-light", !isDark);
- };
+const BodyTheme = () => {
+ const context = React.useContext(ThemeContext);
- componentDidMount() {
- this.onToggleBodyClass(this.context.isDark);
- }
+ useEffect(() => {
+ document.body.classList.toggle("theme-dark", context.isDark);
+ document.body.classList.toggle("theme-light", !context.isDark);
+ }, [context.isDark]);
- componentDidUpdate() {
- this.onToggleBodyClass(this.context.isDark);
- }
-
- render() {
- return (
- }>
- {this.context.isDark ? : }
-
- );
- }
-}
-BodyTheme.contextType = ThemeContext;
+ return (
+ }>
+ {context.isDark ? : }
+
+ );
+};
export { BodyTheme, ThemeContext };
diff --git a/ui/src/Components/Theme/index.test.js b/ui/src/Components/Theme/index.test.js
index f08338103..6a56ae67e 100644
--- a/ui/src/Components/Theme/index.test.js
+++ b/ui/src/Components/Theme/index.test.js
@@ -1,43 +1,49 @@
-import * as React from "react";
+import React from "react";
import { mount } from "enzyme";
-import { BodyTheme, ThemeContext } from ".";
+import { BodyTheme } from ".";
+
+const context = {
+ isDark: true,
+};
beforeEach(() => {
document.body.classList.remove("theme-light");
document.body.classList.remove("theme-dark");
+
+ jest.spyOn(React, "useContext").mockImplementation(() => {
+ return context;
+ });
+});
+
+afterEach(() => {
+ jest.resetAllMocks();
});
describe("", () => {
it("uses light theme when ThemeContext->isDark is false", () => {
- mount(, {
- wrappingComponent: ThemeContext.Provider,
- wrappingComponentProps: { value: { isDark: false } },
- });
+ context.isDark = false;
+ mount();
expect(document.body.classList.contains("theme-light")).toEqual(true);
});
it("uses dark theme when ThemeContext->isDark is true", () => {
- mount(, {
- wrappingComponent: ThemeContext.Provider,
- wrappingComponentProps: { value: { isDark: true } },
- });
+ context.isDark = true;
+ mount();
expect(document.body.classList.contains("theme-dark")).toEqual(true);
});
it("updates theme when ThemeContext->isDark is updated", () => {
- const tree = mount(, {
- wrappingComponent: ThemeContext.Provider,
- wrappingComponentProps: { value: { isDark: true } },
- });
+ context.isDark = true;
+ const tree = mount();
expect(document.body.classList.contains("theme-dark")).toEqual(true);
document.body.classList.remove("theme-light");
document.body.classList.remove("theme-dark");
- const provider = tree.getWrappingComponent();
- provider.setProps({ value: { isDark: false } });
+ context.isDark = false;
+ tree.setProps({});
expect(document.body.classList.contains("theme-light")).toEqual(true);
});