Skip to content

Commit 2873da7

Browse files
committed
Fix markdown rendered and test cases
1 parent 38e964b commit 2873da7

File tree

8 files changed

+92
-81
lines changed

8 files changed

+92
-81
lines changed

frontend/jest.config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ const config: Config = {
9191

9292
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
9393
moduleNameMapper: {
94-
'\\.(css)$': '<rootDir>/__mocks__/styleMock.ts',
95-
'\\.(svg)$': '<rootDir>/__mocks__/styleMock.ts',
94+
"\\.(css)$": "<rootDir>/__mocks__/styleMock.ts",
95+
"\\.(svg)$": "<rootDir>/__mocks__/styleMock.ts",
96+
// "@uiw/react-md-editor":
97+
// "<rootDir>/node_modules/@uiw/react-md-editor/esm/index",
9698
},
9799

98100
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader

frontend/package-lock.json

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"@mui/material": "^6.1.0",
2323
"@uiw/react-md-editor": "^4.0.4",
2424
"axios": "^1.7.7",
25-
"markdown-to-jsx": "^7.5.0",
2625
"react": "^18.3.1",
2726
"react-dom": "^18.3.1",
2827
"react-router-dom": "^6.26.2",

frontend/src/components/ProfileSection/ProfileSection.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ describe("Profiles that belong to the current authenticated user", () => {
149149
handleChangePasswordOpen={handleChangePasswordOpen}
150150
/>
151151
);
152-
const editProfileButton = screen.queryByRole("button", {
153-
name: "Edit password",
152+
const editProfileButton = screen.getByRole("button", {
153+
name: "Change password",
154154
});
155155
expect(editProfileButton).toBeInTheDocument();
156156
});

frontend/src/components/QuestionCategoryAutoComplete/QuestionCategoryAutoComplete.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ describe("Question Category Auto Complete", () => {
2828
);
2929

3030
const input = screen.getByLabelText("Category");
31-
fireEvent.change(input, { target: { value: "Strings" } });
31+
fireEvent.change(input, { target: { value: "DFS" } });
3232

33-
expect(screen.getByText("Strings")).toBeInTheDocument();
33+
expect(screen.getByText("DFS")).toBeInTheDocument();
3434
});
3535

3636
it("Adding a new category not from the category list", () => {

frontend/src/components/QuestionDetail/QuestionDetail.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { render, screen } from "@testing-library/react";
22
import "@testing-library/jest-dom";
33
import QuestionDetail from ".";
44

5+
jest.mock("@uiw/react-md-editor", () => ({
6+
Markdown({
7+
source,
8+
}: {
9+
source: string;
10+
components: Partial<React.Component>;
11+
}) {
12+
return <div>{source}</div>;
13+
},
14+
}));
15+
516
describe("Question details", () => {
617
it("Question title is rendered", () => {
718
const title = "Test title";

frontend/src/components/QuestionDetail/index.tsx

Lines changed: 49 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Box, Chip, List, ListItem, Stack, Typography, useTheme } from "@mui/material";
2-
import Markdown from "markdown-to-jsx";
3-
import { grey } from "@mui/material/colors";
1+
import { Box, Chip, List, ListItem, Stack, Typography } from "@mui/material";
2+
import MDEditor from "@uiw/react-md-editor";
43

54
interface QuestionDetailProps {
65
title: string;
@@ -15,8 +14,6 @@ const QuestionDetail: React.FC<QuestionDetailProps> = ({
1514
categories,
1615
description,
1716
}) => {
18-
const theme = useTheme();
19-
2017
return (
2118
<Box
2219
sx={(theme) => ({
@@ -33,7 +30,10 @@ const QuestionDetail: React.FC<QuestionDetailProps> = ({
3330
<Typography component={"h1"} variant="h3">
3431
{title}
3532
</Typography>
36-
<Stack direction={"row"} sx={(theme) => ({ marginTop: theme.spacing(2) })}>
33+
<Stack
34+
direction={"row"}
35+
sx={(theme) => ({ marginTop: theme.spacing(2) })}
36+
>
3737
{complexity && (
3838
<Chip
3939
key={complexity}
@@ -57,68 +57,56 @@ const QuestionDetail: React.FC<QuestionDetailProps> = ({
5757
))}
5858
</Stack>
5959
</Box>
60-
<Markdown
61-
options={{
62-
overrides: {
63-
h1: {
64-
component: Typography,
65-
props: { component: "h1", variant: "h4" },
66-
},
67-
h2: {
68-
component: Typography,
69-
props: { component: "h2", variant: "h5" },
60+
<Stack data-color-mode="light" paddingTop={2}>
61+
<MDEditor.Markdown
62+
source={description}
63+
components={{
64+
h1({ children }) {
65+
return (
66+
<Typography component={"h1"} variant="h4">
67+
{children}
68+
</Typography>
69+
);
7070
},
71-
h3: {
72-
component: Typography,
73-
props: { component: "h3", variant: "h6" },
71+
h2({ children }) {
72+
return (
73+
<Typography component={"h2"} variant="h5">
74+
{children}
75+
</Typography>
76+
);
7477
},
75-
p: {
76-
component: Typography,
78+
h3({ children }) {
79+
return (
80+
<Typography component={"h3"} variant="h6">
81+
{children}
82+
</Typography>
83+
);
7784
},
78-
ol: {
79-
component: List,
80-
props: {
81-
component: "ol",
82-
sx: {
83-
paddingLeft: theme.spacing(4),
84-
listStyleType: "decimal",
85-
},
86-
},
85+
p({ children }) {
86+
return <Typography>{children}</Typography>;
8787
},
88-
ul: {
89-
component: List,
90-
props: {
91-
component: "ul",
92-
sx: {
93-
paddingLeft: theme.spacing(4),
94-
listStyleType: "disc",
95-
},
96-
},
88+
ol({ children }) {
89+
return (
90+
<List component={"ol"} sx={{ listStyleType: "decimal" }}>
91+
{children}
92+
</List>
93+
);
9794
},
98-
li: {
99-
component: ListItem,
100-
props: { sx: { display: "list-item" } },
95+
ul({ children }) {
96+
return (
97+
<List component={"ul"} sx={{ listStyleType: "disc" }}>
98+
{children}
99+
</List>
100+
);
101101
},
102-
code: {
103-
props: {
104-
style: {
105-
backgroundColor: grey[200],
106-
padding: "0.2em",
107-
borderRadius: "0.4em",
108-
},
109-
},
102+
li({ children }) {
103+
return (
104+
<ListItem sx={{ display: "list-item" }}>{children}</ListItem>
105+
);
110106
},
111-
img: {
112-
component: "img",
113-
props: {
114-
style: { height: "300px", width: "auto", objectFit: "contain" },
115-
},
116-
},
117-
},
118-
}}
119-
>
120-
{description}
121-
</Markdown>
107+
}}
108+
/>
109+
</Stack>
122110
</Box>
123111
);
124112
};

frontend/src/components/QuestionImageContainer/QuestionImageContainer.test.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@ jest.mock("../../utils/api", () => ({
1010
}));
1111

1212
describe("Question Image Container", () => {
13+
const mockLocalStorage = (() => {
14+
let store: { [key: string]: string } = { token: "test" };
15+
16+
return {
17+
getItem(key: string) {
18+
return store[key];
19+
},
20+
setItem(key: string, value: string) {
21+
store[key] = value;
22+
},
23+
};
24+
})();
25+
26+
beforeAll(() =>
27+
Object.defineProperty(window, "localStorage", {
28+
value: mockLocalStorage,
29+
writable: true,
30+
})
31+
);
32+
1333
it("Question Image Container is rendered with no uploaded images", () => {
1434
const uploadedImagesUrl: string[] = [];
1535
const setUploadedImagesUrl = jest.fn();
@@ -102,7 +122,10 @@ describe("Question Image Container", () => {
102122
"/images",
103123
expect.any(FormData),
104124
expect.objectContaining({
105-
headers: { "Content-Type": "multipart/form-data" },
125+
headers: {
126+
Authorization: `Bearer ${mockLocalStorage.getItem("token")}`,
127+
"Content-Type": "multipart/form-data",
128+
},
106129
})
107130
);
108131

0 commit comments

Comments
 (0)