-
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.
add metadata endpoint, context and hook (#4)
* add metadata endpoint, context and hook * clean up junk test code
- Loading branch information
1 parent
1d4e9fa
commit 464cc7d
Showing
12 changed files
with
119 additions
and
84 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
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
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 |
---|---|---|
@@ -1,11 +1,26 @@ | ||
import { it, describe } from "vitest"; | ||
import { render } from "@testing-library/react"; | ||
import { it, describe, vi, expect } from "vitest"; | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import App from "./App"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import axios from "axios"; | ||
|
||
vi.mock("axios"); | ||
|
||
describe("App", () => { | ||
it("renders welcome page", () => { | ||
vi.mocked(axios.get).mockImplementation((url) => { | ||
switch (url) { | ||
case "/api/beamlines": | ||
return Promise.resolve({}); | ||
default: | ||
return Promise.resolve({}); | ||
} | ||
}); | ||
|
||
it("renders welcome page", async () => { | ||
render(<App />, { wrapper: BrowserRouter }); | ||
expect( | ||
await screen.findByText(/XAS Standards Database /i) | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
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
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
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,23 @@ | ||
import { createContext } from "react"; | ||
import { AppMetadata } from "../models"; | ||
import useMetadata from "../hooks/useMetadata"; | ||
|
||
const MetadataContext = createContext<AppMetadata>({ | ||
elements: [], | ||
edges: [], | ||
beamlines: [], | ||
licences: [], | ||
}); | ||
|
||
function MetadataProvider(props: { children: React.ReactNode }) { | ||
const { children } = props; | ||
const appMetadata = useMetadata(); | ||
|
||
return ( | ||
<MetadataContext.Provider value={appMetadata}> | ||
{children} | ||
</MetadataContext.Provider> | ||
); | ||
} | ||
|
||
export { MetadataContext, MetadataProvider }; |
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,26 @@ | ||
import { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
|
||
const metadata_url = "/api/metadata"; | ||
|
||
import { AppMetadata } from "../models"; | ||
|
||
function useMetadata(): AppMetadata { | ||
const [appMetadata, setAppMetdata] = useState<AppMetadata>({ | ||
beamlines: [], | ||
elements: [], | ||
edges: [], | ||
licences: [], | ||
}); | ||
|
||
useEffect(() => { | ||
axios.get(metadata_url).then((res) => { | ||
// console.log("BEAMLINE! " + res.data.name); | ||
setAppMetdata(res.data); | ||
}); | ||
}, []); | ||
|
||
return appMetadata; | ||
} | ||
|
||
export default useMetadata; |
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