Skip to content

Commit 10fe55c

Browse files
Merge pull request #1815 from github/robertbrignull/view_paths
Add theme provider when rendering code flows
2 parents 125867d + adea0b4 commit 10fe55c

4 files changed

Lines changed: 91 additions & 17 deletions

File tree

extensions/ql-vscode/src/view/common/CodePaths/CodePaths.tsx

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useRef, useState } from "react";
33
import styled from "styled-components";
44
import { VSCodeLink } from "@vscode/webview-ui-toolkit/react";
55

6-
import { Overlay } from "@primer/react";
6+
import { Overlay, ThemeProvider } from "@primer/react";
77

88
import {
99
AnalysisMessage,
@@ -16,7 +16,7 @@ const ShowPathsLink = styled(VSCodeLink)`
1616
cursor: pointer;
1717
`;
1818

19-
type Props = {
19+
export type CodePathsProps = {
2020
codeFlows: CodeFlow[];
2121
ruleDescription: string;
2222
message: AnalysisMessage;
@@ -28,7 +28,7 @@ export const CodePaths = ({
2828
ruleDescription,
2929
message,
3030
severity,
31-
}: Props) => {
31+
}: CodePathsProps) => {
3232
const [isOpen, setIsOpen] = useState(false);
3333

3434
const linkRef = useRef<HTMLAnchorElement>(null);
@@ -41,20 +41,22 @@ export const CodePaths = ({
4141
Show paths
4242
</ShowPathsLink>
4343
{isOpen && (
44-
<Overlay
45-
returnFocusRef={linkRef}
46-
onEscape={closeOverlay}
47-
onClickOutside={closeOverlay}
48-
anchorSide="outside-top"
49-
>
50-
<CodePathsOverlay
51-
codeFlows={codeFlows}
52-
ruleDescription={ruleDescription}
53-
message={message}
54-
severity={severity}
55-
onClose={closeOverlay}
56-
/>
57-
</Overlay>
44+
<ThemeProvider colorMode="auto">
45+
<Overlay
46+
returnFocusRef={linkRef}
47+
onEscape={closeOverlay}
48+
onClickOutside={closeOverlay}
49+
anchorSide="outside-top"
50+
>
51+
<CodePathsOverlay
52+
codeFlows={codeFlows}
53+
ruleDescription={ruleDescription}
54+
message={message}
55+
severity={severity}
56+
onClose={closeOverlay}
57+
/>
58+
</Overlay>
59+
</ThemeProvider>
5860
)}
5961
</>
6062
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from "react";
2+
import { render as reactRender, screen } from "@testing-library/react";
3+
import userEvent from "@testing-library/user-event";
4+
import { CodePaths, CodePathsProps } from "../CodePaths";
5+
6+
import { createMockCodeFlows } from "../../../../vscode-tests/factories/remote-queries/shared/CodeFlow";
7+
import { createMockAnalysisMessage } from "../../../../vscode-tests/factories/remote-queries/shared/AnalysisMessage";
8+
9+
describe(CodePaths.name, () => {
10+
const render = (props?: CodePathsProps) =>
11+
reactRender(
12+
<CodePaths
13+
codeFlows={createMockCodeFlows()}
14+
ruleDescription="Rule description"
15+
message={createMockAnalysisMessage()}
16+
severity="Recommendation"
17+
{...props}
18+
/>,
19+
);
20+
21+
it("renders correctly when unexpanded", () => {
22+
render();
23+
24+
expect(screen.getByText("Show paths")).toBeInTheDocument();
25+
expect(screen.queryByText("Code snippet text")).not.toBeInTheDocument();
26+
expect(screen.queryByText("Rule description")).not.toBeInTheDocument();
27+
});
28+
29+
it("renders correctly when expanded", async () => {
30+
render();
31+
32+
await userEvent.click(screen.getByText("Show paths"));
33+
34+
expect(screen.getByText("Code snippet text")).toBeInTheDocument();
35+
expect(screen.getByText("Rule description")).toBeInTheDocument();
36+
});
37+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { AnalysisMessage } from "../../../../remote-queries/shared/analysis-result";
2+
3+
export function createMockAnalysisMessage(): AnalysisMessage {
4+
return {
5+
tokens: [
6+
{
7+
t: "text",
8+
text: "Token text",
9+
},
10+
],
11+
};
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { CodeFlow } from "../../../../remote-queries/shared/analysis-result";
2+
import { createMockAnalysisMessage } from "./AnalysisMessage";
3+
4+
export function createMockCodeFlows(): CodeFlow[] {
5+
return [
6+
{
7+
threadFlows: [
8+
{
9+
fileLink: {
10+
fileLinkPrefix: "/prefix",
11+
filePath: "filePath",
12+
},
13+
codeSnippet: {
14+
startLine: 123,
15+
endLine: 456,
16+
text: "Code snippet text",
17+
},
18+
message: createMockAnalysisMessage(),
19+
},
20+
],
21+
},
22+
];
23+
}

0 commit comments

Comments
 (0)