|
1
|
import React from "react" |
|
2
|
import { describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
import { CodeBlockProps } from "../../CodeBlock" |
|
5
|
import { InlineCodeProps } from "../../InlineCode" |
|
6
|
|
|
7
|
// mock data |
|
8
|
const mockSource = "console.log('Hello, world!')" |
|
9
|
|
|
10
|
// mock components |
|
11
|
vi.mock("@/components/CodeBlock", () => ({ |
|
12
|
CodeBlock: ({ source, ...codeBlockProps }: CodeBlockProps) => ( |
|
13
|
<div data-testid="code-block"> |
|
14
|
<div data-testid="code-block-source">{source}</div> |
|
15
|
<div data-testid="code-block-props"> |
|
16
|
{JSON.stringify(codeBlockProps as Record<string, unknown>)} |
|
17
|
</div> |
|
18
|
</div> |
|
19
|
), |
|
20
|
})) |
|
21
|
vi.mock("@/components/InlineCode", () => ({ |
|
22
|
InlineCode: ({ children, ...inlineCodeProps }: InlineCodeProps) => ( |
|
23
|
<div data-testid="inline-code"> |
|
24
|
<div data-testid="inline-code-children">{children}</div> |
|
25
|
<div data-testid="inline-code-props"> |
|
26
|
{JSON.stringify(inlineCodeProps)} |
|
27
|
</div> |
|
28
|
</div> |
|
29
|
), |
|
30
|
})) |
|
31
|
vi.mock("@/components/MermaidDiagram", () => ({ |
|
32
|
MermaidDiagram: ({ diagramContent }: { diagramContent: string }) => ( |
|
33
|
<div data-testid="mermaid-diagram">{diagramContent}</div> |
|
34
|
), |
|
35
|
})) |
|
36
|
vi.mock("@/components/Npm2YarnCode", () => ({ |
|
37
|
Npm2YarnCode: ({ npmCode }: { npmCode: string }) => ( |
|
38
|
<div data-testid="npm2yarn-code">{npmCode}</div> |
|
39
|
), |
|
40
|
})) |
|
41
|
vi.mock("@/components/Npx2YarnCode", () => ({ |
|
42
|
Npx2YarnCode: ({ npxCode, isExecutable }: { npxCode: string; isExecutable?: boolean }) => ( |
|
43
|
<div data-testid="npx2yarn-code" data-is-executable={isExecutable}> |
|
44
|
{npxCode} |
|
45
|
</div> |
|
46
|
), |
|
47
|
})) |
|
48
|
|
|
49
|
import { CodeMdx } from "../../CodeMdx" |
|
50
|
|
|
51
|
describe("render", () => { |
|
52
|
test("renders without children", () => { |
|
53
|
const { container } = render(<CodeMdx />) |
|
54
|
expect(container).toBeInTheDocument() |
|
55
|
expect(container).toBeEmptyDOMElement() |
|
56
|
}) |
|
57
|
|
|
58
|
test("renders with children", () => { |
|
59
|
const { container } = render(<CodeMdx>{mockSource}</CodeMdx>) |
|
60
|
expect(container).toBeInTheDocument() |
|
61
|
const inlineCodeChildren = container.querySelector( |
|
62
|
"[data-testid='inline-code-children']" |
|
63
|
) |
|
64
|
expect(inlineCodeChildren).toBeInTheDocument() |
|
65
|
expect(inlineCodeChildren).toHaveTextContent(mockSource) |
|
66
|
}) |
|
67
|
|
|
68
|
test("renders with className", () => { |
|
69
|
const { container } = render( |
|
70
|
<CodeMdx className="language-javascript">{mockSource}</CodeMdx> |
|
71
|
) |
|
72
|
expect(container).toBeInTheDocument() |
|
73
|
const codeBlock = container.querySelector("[data-testid='code-block']") |
|
74
|
expect(codeBlock).toBeInTheDocument() |
|
75
|
const codeBlockSource = container.querySelector( |
|
76
|
"[data-testid='code-block-source']" |
|
77
|
) |
|
78
|
expect(codeBlockSource).toBeInTheDocument() |
|
79
|
expect(codeBlockSource).toHaveTextContent(mockSource) |
|
80
|
}) |
|
81
|
|
|
82
|
test("renders with npm2yarn", () => { |
|
83
|
const { container } = render( |
|
84
|
<CodeMdx npm2yarn={true} className="language-bash"> |
|
85
|
{mockSource} |
|
86
|
</CodeMdx> |
|
87
|
) |
|
88
|
expect(container).toBeInTheDocument() |
|
89
|
const npm2yarnCode = container.querySelector( |
|
90
|
"[data-testid='npm2yarn-code']" |
|
91
|
) |
|
92
|
expect(npm2yarnCode).toBeInTheDocument() |
|
93
|
expect(npm2yarnCode).toHaveTextContent(mockSource) |
|
94
|
}) |
|
95
|
|
|
96
|
test("renders with npx2yarn", () => { |
|
97
|
const { container } = render( |
|
98
|
<CodeMdx npx2yarn={true} className="language-bash"> |
|
99
|
{mockSource} |
|
100
|
</CodeMdx> |
|
101
|
) |
|
102
|
expect(container).toBeInTheDocument() |
|
103
|
const npx2yarnCode = container.querySelector( |
|
104
|
"[data-testid='npx2yarn-code']" |
|
105
|
) |
|
106
|
expect(npx2yarnCode).toBeInTheDocument() |
|
107
|
expect(npx2yarnCode).toHaveTextContent(mockSource) |
|
108
|
expect(npx2yarnCode).toHaveAttribute("data-is-executable", "false") |
|
109
|
}) |
|
110
|
|
|
111
|
test("renders with npx2yarnExec", () => { |
|
112
|
const { container } = render( |
|
113
|
<CodeMdx npx2yarnExec={true} className="language-bash"> |
|
114
|
{mockSource} |
|
115
|
</CodeMdx> |
|
116
|
) |
|
117
|
expect(container).toBeInTheDocument() |
|
118
|
const npx2yarnCode = container.querySelector( |
|
119
|
"[data-testid='npx2yarn-code']" |
|
120
|
) |
|
121
|
expect(npx2yarnCode).toBeInTheDocument() |
|
122
|
expect(npx2yarnCode).toHaveTextContent(mockSource) |
|
123
|
expect(npx2yarnCode).toHaveAttribute("data-is-executable", "true") |
|
124
|
}) |
|
125
|
|
|
126
|
test("renders with npx2yarnExec taking precedence over npx2yarn", () => { |
|
127
|
const { container } = render( |
|
128
|
<CodeMdx npx2yarn={true} npx2yarnExec={true} className="language-bash"> |
|
129
|
{mockSource} |
|
130
|
</CodeMdx> |
|
131
|
) |
|
132
|
expect(container).toBeInTheDocument() |
|
133
|
const npx2yarnCode = container.querySelector( |
|
134
|
"[data-testid='npx2yarn-code']" |
|
135
|
) |
|
136
|
expect(npx2yarnCode).toBeInTheDocument() |
|
137
|
expect(npx2yarnCode).toHaveAttribute("data-is-executable", "true") |
|
138
|
}) |
|
139
|
|
|
140
|
test("renders with mermaid", () => { |
|
141
|
const { container } = render( |
|
142
|
<CodeMdx className="language-mermaid">{mockSource}</CodeMdx> |
|
143
|
) |
|
144
|
expect(container).toBeInTheDocument() |
|
145
|
const mermaidDiagram = container.querySelector( |
|
146
|
"[data-testid='mermaid-diagram']" |
|
147
|
) |
|
148
|
expect(mermaidDiagram).toBeInTheDocument() |
|
149
|
expect(mermaidDiagram).toHaveTextContent(mockSource) |
|
150
|
}) |
|
151
|
|
|
152
|
test("renders with codeBlockProps", () => { |
|
153
|
const codeBlockProps = { className: "language-javascript" } |
|
154
|
const { container } = render( |
|
155
|
<CodeMdx codeBlockProps={codeBlockProps} className="language-javascript"> |
|
156
|
{mockSource} |
|
157
|
</CodeMdx> |
|
158
|
) |
|
159
|
expect(container).toBeInTheDocument() |
|
160
|
const codeBlockPropsElement = container.querySelector( |
|
161
|
"[data-testid='code-block-props']" |
|
162
|
) |
|
163
|
expect(codeBlockPropsElement).toBeInTheDocument() |
|
164
|
const parsedContent = JSON.parse(codeBlockPropsElement!.textContent || "{}") |
|
165
|
expect(parsedContent).toEqual({ |
|
166
|
...codeBlockProps, |
|
167
|
lang: "javascript", |
|
168
|
}) |
|
169
|
}) |
|
170
|
|
|
171
|
test("renders with inlineCodeProps", () => { |
|
172
|
const inlineCodeProps: Partial<InlineCodeProps> = { variant: "grey-bg" } |
|
173
|
const { container } = render( |
|
174
|
<CodeMdx inlineCodeProps={inlineCodeProps}>{mockSource}</CodeMdx> |
|
175
|
) |
|
176
|
expect(container).toBeInTheDocument() |
|
177
|
const inlineCodePropsElement = container.querySelector( |
|
178
|
"[data-testid='inline-code-props']" |
|
179
|
) |
|
180
|
expect(inlineCodePropsElement).toBeInTheDocument() |
|
181
|
expect(inlineCodePropsElement).toHaveTextContent( |
|
182
|
JSON.stringify(inlineCodeProps) |
|
183
|
) |
|
184
|
}) |
|
185
|
}) |