|
1
|
import React from "react" |
|
2
|
import { describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
// mock functions |
|
6
|
const npxToYarnMock = vi.fn((code: string, packageManager: "yarn" | "pnpm", isExecutable?: boolean) => code) |
|
7
|
|
|
8
|
// mock components |
|
9
|
vi.mock("@/components/CodeTabs", () => ({ |
|
10
|
CodeTabs: ({ |
|
11
|
children, |
|
12
|
group, |
|
13
|
}: { |
|
14
|
children: React.ReactNode |
|
15
|
group: string |
|
16
|
}) => ( |
|
17
|
<div data-testid="code-tabs" data-group={group}> |
|
18
|
{children} |
|
19
|
</div> |
|
20
|
), |
|
21
|
})) |
|
22
|
|
|
23
|
vi.mock("@/components/CodeTabs/Item", () => ({ |
|
24
|
CodeTab: ({ |
|
25
|
children, |
|
26
|
label, |
|
27
|
value, |
|
28
|
}: { |
|
29
|
children: React.ReactNode |
|
30
|
label: string |
|
31
|
value: string |
|
32
|
}) => ( |
|
33
|
<div data-testid="code-tab" data-label={label} data-value={value}> |
|
34
|
{children} |
|
35
|
</div> |
|
36
|
), |
|
37
|
})) |
|
38
|
|
|
39
|
vi.mock("@/components/CodeBlock", () => ({ |
|
40
|
CodeBlock: ({ |
|
41
|
source, |
|
42
|
lang, |
|
43
|
title, |
|
44
|
}: { |
|
45
|
source: string |
|
46
|
lang: string |
|
47
|
title?: string |
|
48
|
}) => ( |
|
49
|
<div data-testid="code-block" data-lang={lang} data-title={title}> |
|
50
|
{source} |
|
51
|
</div> |
|
52
|
), |
|
53
|
})) |
|
54
|
|
|
55
|
vi.mock("@/utils/npx-to-yarn", () => ({ |
|
56
|
npxToYarn: (code: string, packageManager: "yarn" | "pnpm", isExecutable?: boolean) => |
|
57
|
npxToYarnMock(code, packageManager, isExecutable), |
|
58
|
})) |
|
59
|
|
|
60
|
import { Npx2YarnCode } from "../index" |
|
61
|
|
|
62
|
describe("render", () => { |
|
63
|
test("renders npm2yarn code", () => { |
|
64
|
const { container } = render( |
|
65
|
<Npx2YarnCode npxCode="npx medusa db:migrate" /> |
|
66
|
) |
|
67
|
expect(npxToYarnMock).toHaveBeenCalledTimes(2) |
|
68
|
expect(container).toBeInTheDocument() |
|
69
|
const codeTabs = container.querySelector("[data-testid='code-tabs']") |
|
70
|
expect(codeTabs).toBeInTheDocument() |
|
71
|
expect(codeTabs).toHaveAttribute("data-group", "npm2yarn") |
|
72
|
const codeTabsChildren = codeTabs?.querySelectorAll( |
|
73
|
"[data-testid='code-tab']" |
|
74
|
) |
|
75
|
expect(codeTabsChildren).toHaveLength(3) |
|
76
|
expect(codeTabsChildren![0]).toHaveAttribute("data-label", "yarn") |
|
77
|
expect(codeTabsChildren![0]).toHaveAttribute("data-value", "yarn") |
|
78
|
const yarnCodeBlock = codeTabsChildren![0].querySelector( |
|
79
|
"[data-testid='code-block']" |
|
80
|
) |
|
81
|
expect(yarnCodeBlock).toBeInTheDocument() |
|
82
|
expect(yarnCodeBlock).toHaveAttribute("data-lang", "bash") |
|
83
|
expect(codeTabsChildren![1]).toHaveAttribute("data-label", "pnpm") |
|
84
|
expect(codeTabsChildren![1]).toHaveAttribute("data-value", "pnpm") |
|
85
|
const pnpmCodeBlock = codeTabsChildren![1].querySelector( |
|
86
|
"[data-testid='code-block']" |
|
87
|
) |
|
88
|
expect(pnpmCodeBlock).toBeInTheDocument() |
|
89
|
expect(pnpmCodeBlock).toHaveAttribute("data-lang", "bash") |
|
90
|
expect(codeTabsChildren![2]).toHaveAttribute("data-label", "npx") |
|
91
|
expect(codeTabsChildren![2]).toHaveAttribute("data-value", "npm") |
|
92
|
const npxCodeBlock = codeTabsChildren![2].querySelector( |
|
93
|
"[data-testid='code-block']" |
|
94
|
) |
|
95
|
expect(npxCodeBlock).toBeInTheDocument() |
|
96
|
expect(npxCodeBlock).toHaveAttribute("data-lang", "bash") |
|
97
|
expect(npxCodeBlock).toHaveTextContent("npx medusa db:migrate") |
|
98
|
}) |
|
99
|
|
|
100
|
test("renders npm2yarn code with custom code options", () => { |
|
101
|
const { container } = render( |
|
102
|
<Npx2YarnCode |
|
103
|
npxCode="npx medusa db:migrate" |
|
104
|
title="Custom Title" |
|
105
|
/> |
|
106
|
) |
|
107
|
expect(container).toBeInTheDocument() |
|
108
|
const codeBlock = container.querySelector("[data-testid='code-block']") |
|
109
|
expect(codeBlock).toBeInTheDocument() |
|
110
|
expect(codeBlock).toHaveAttribute("data-title", "Custom Title") |
|
111
|
}) |
|
112
|
|
|
113
|
test("calls npxToYarn with isExecutable=false by default", () => { |
|
114
|
npxToYarnMock.mockClear() |
|
115
|
render(<Npx2YarnCode npxCode="npx medusa db:migrate" />) |
|
116
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx medusa db:migrate", "yarn", false) |
|
117
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx medusa db:migrate", "pnpm", false) |
|
118
|
}) |
|
119
|
|
|
120
|
test("calls npxToYarn with isExecutable=true when specified", () => { |
|
121
|
npxToYarnMock.mockClear() |
|
122
|
render(<Npx2YarnCode npxCode="npx create-medusa-app@latest" isExecutable={true} />) |
|
123
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx create-medusa-app@latest", "yarn", true) |
|
124
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx create-medusa-app@latest", "pnpm", true) |
|
125
|
}) |
|
126
|
|
|
127
|
test("calls npxToYarn with isExecutable=false when explicitly set", () => { |
|
128
|
npxToYarnMock.mockClear() |
|
129
|
render(<Npx2YarnCode npxCode="npx medusa db:migrate" isExecutable={false} />) |
|
130
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx medusa db:migrate", "yarn", false) |
|
131
|
expect(npxToYarnMock).toHaveBeenCalledWith("npx medusa db:migrate", "pnpm", false) |
|
132
|
}) |
|
133
|
}) |