diff --git a/src/PageNav.tsx b/src/PageNav.tsx index dd95d9fb45..b34c19af16 100644 --- a/src/PageNav.tsx +++ b/src/PageNav.tsx @@ -1,14 +1,26 @@ -import React from "react"; +import React, { useContext } from "react"; import { Nav, NavItem, NavList, PageSidebar } from "@patternfly/react-core"; import { RealmSelector } from "./components/realm-selector/RealmSelector"; +import { DataLoader } from "./components/data-loader/DataLoader"; +import { HttpClientContext } from "./http-service/HttpClientContext"; +import { Realm } from "./models/Realm"; export const PageNav: React.FunctionComponent = () => { + const httpClient = useContext(HttpClientContext)!; + const realmLoader = async () => { + const response = await httpClient.doGet("/admin/realms"); + return response.data; + }; return ( - + + {(realmList) => ( + + )} + Link 1 diff --git a/src/components/realm-selector/RealmSelector.test.tsx b/src/components/realm-selector/RealmSelector.test.tsx index 370dd67ae9..7397d73f76 100644 --- a/src/components/realm-selector/RealmSelector.test.tsx +++ b/src/components/realm-selector/RealmSelector.test.tsx @@ -1,36 +1,23 @@ import React from "react"; -import { Button, AlertVariant } from "@patternfly/react-core"; import { mount } from "enzyme"; -import EnzymeToJson from "enzyme-to-json"; -import { act } from "react-dom/test-utils"; +import { act } from "@testing-library/react"; import { AlertPanel } from "../alert/AlertPanel"; import { useAlerts } from "../alert/Alerts"; import { RealmSelector } from "./RealmSelector"; -const WithButton = () => { - const [add, alerts, hide] = useAlerts(); - return ( - <> - - - +it("renders realm selector", async () => { + const wrapper = mount( + ); -}; -it("renders realm selector", () => { - const tree = mount(); - const button = tree.find("button"); - expect(button).not.toBeNull(); + expect(wrapper.text()).toBe("test"); + const expandButton = wrapper.find("button"); act(() => { - button!.simulate("click"); + expandButton!.simulate("click"); }); - expect(EnzymeToJson(tree)).toMatchSnapshot(); - act(() => { - jest.runAllTimers(); - }); - expect(EnzymeToJson(tree)).toMatchSnapshot(); + expect(wrapper.html()).toMatchSnapshot(); }); diff --git a/src/components/realm-selector/RealmSelector.tsx b/src/components/realm-selector/RealmSelector.tsx index 3c6a52d702..7c6dabf4ba 100644 --- a/src/components/realm-selector/RealmSelector.tsx +++ b/src/components/realm-selector/RealmSelector.tsx @@ -11,33 +11,24 @@ import { } from "@patternfly/react-core"; import style from "./realm-selector.module.css"; -import { HttpClientContext } from "../../http-service/HttpClientContext"; -export const RealmSelector = () => { +type RealmSelectorProps = { + realm: string; + realmList: Realm[]; +}; + +export const RealmSelector = ({ realm, realmList }: RealmSelectorProps) => { const [open, setOpen] = useState(false); const history = useHistory(); - const httpClient = useContext(HttpClientContext); - const [realms, setRealms] = useState([] as Realm[]); - const [currentRealm, setCurrentRealm] = useState("Master"); + const [currentRealm, setCurrentRealm] = useState(realm); - const getRealms = async () => { - return await httpClient - ?.doGet("/admin/realms") - .then((r) => r.data as Realm[]); - }; - - useEffect(() => { - getRealms().then((result) => { - setRealms(result || []); - }); - }, []); - - const dropdownItems = realms.map((r) => ( + const dropdownItems = realmList.map((r) => ( setCurrentRealm(r.realm)} + onClick={() => { + setCurrentRealm(r.realm); + setOpen(!open); + }} > {r.realm.charAt(0).toUpperCase() + r.realm.slice(1)} diff --git a/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap b/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap index 26559a37da..fbe693e3e7 100644 --- a/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap +++ b/src/components/realm-selector/__snapshots__/RealmSelector.test.tsx.snap @@ -1,587 +1,3 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`renders realm selector 1`] = ` - - , - - - , - ] - } - id="realm-select" - isOpen={false} - toggle={ - - Master - - } - > - , - - - , - ] - } - id="realm-select" - isGrouped={false} - isOpen={false} - isPlain={false} - menuAppendTo="inline" - onSelect={[Function]} - position="left" - toggle={ - - Master - - } - > -
- - - -
, - } - } - > - - - - , - } - } - > - - - - -
-
-
-`; - -exports[`renders realm selector 2`] = ` - - , - - - , - ] - } - id="realm-select" - isOpen={false} - toggle={ - - Master - - } - > - , - - - , - ] - } - id="realm-select" - isGrouped={false} - isOpen={false} - isPlain={false} - menuAppendTo="inline" - onSelect={[Function]} - position="left" - toggle={ - - Master - - } - > -
- - - -
, - } - } - > - - - - , - } - } - > - - - - -
-
-
-`; +exports[`renders realm selector 1`] = `"
"`; diff --git a/src/components/scroll-form/ScrollForm.tsx b/src/components/scroll-form/ScrollForm.tsx index d6a0e81189..1593b14046 100644 --- a/src/components/scroll-form/ScrollForm.tsx +++ b/src/components/scroll-form/ScrollForm.tsx @@ -10,29 +10,29 @@ type ScrollFormProps = { }; export const ScrollForm = ({ sections, children }: ScrollFormProps) => { - const getCurrentSection = () => { - for (let sectionName of sections) { - const section = document.getElementById(sectionName)!; - const startAt = section.offsetTop; - const endAt = startAt + section.offsetHeight; - const currentPosition = - document.documentElement.scrollTop || document.body.scrollTop; - const isInView = currentPosition >= startAt && currentPosition < endAt; - if (isInView) { - return sectionName; - } - } - }; - const [active, setActive] = useState(sections[0]); useEffect(() => { + const getCurrentSection = () => { + for (let sectionName of sections) { + const section = document.getElementById(sectionName)!; + const startAt = section.offsetTop; + const endAt = startAt + section.offsetHeight; + const currentPosition = + document.documentElement.scrollTop || document.body.scrollTop; + const isInView = currentPosition >= startAt && currentPosition < endAt; + if (isInView) { + return sectionName; + } + } + }; + window.addEventListener("scroll", () => { const active = getCurrentSection(); if (active) { setActive(active); } }); - }, [active]); + }, [active, sections]); const Nav = () => (