1 import React from "react"
2 import { describe, expect, test, vi } from "vitest"
3 import { render } from "@testing-library/react"
4
5 // mock components
6 vi.mock("@/components/CodeTabs", () => ({
7 CodeTabs: ({
8 children,
9 group,
10 }: {
11 children: React.ReactNode
12 group: string
13 }) => (
14 <div data-testid="code-tabs" data-group={group}>
15 {children}
16 </div>
17 ),
18 }))
19
20 vi.mock("@/components/CodeTabs/Item", () => ({
21 CodeTab: ({
22 children,
23 label,
24 value,
25 }: {
26 children: React.ReactNode
27 label: string
28 value: string
29 }) => (
30 <div data-testid="code-tab" data-label={label} data-value={value}>
31 {children}
32 </div>
33 ),
34 }))
35
36 vi.mock("@/components/CodeBlock", () => ({
37 CodeBlock: ({
38 source,
39 lang,
40 title,
41 }: {
42 source: string
43 lang: string
44 title?: string
45 }) => (
46 <div data-testid="code-block" data-lang={lang} data-title={title}>
47 {source}
48 </div>
49 ),
50 }))
51
52 import { Npm2YarnCode } from "../index"
53
54 describe("render", () => {
55 test("renders npm2yarn code", () => {
56 const { container } = render(
57 <Npm2YarnCode npmCode="npm install @medusajs/medusa" />
58 )
59 expect(container).toBeInTheDocument()
60 const codeTabs = container.querySelector("[data-testid='code-tabs']")
61 expect(codeTabs).toBeInTheDocument()
62 expect(codeTabs).toHaveAttribute("data-group", "npm2yarn")
63 const codeTabsChildren = codeTabs?.querySelectorAll(
64 "[data-testid='code-tab']"
65 )
66 expect(codeTabsChildren).toHaveLength(3)
67 expect(codeTabsChildren![0]).toHaveAttribute("data-label", "yarn")
68 expect(codeTabsChildren![0]).toHaveAttribute("data-value", "yarn")
69 const yarnCodeBlock = codeTabsChildren![0].querySelector(
70 "[data-testid='code-block']"
71 )
72 expect(yarnCodeBlock).toBeInTheDocument()
73 expect(yarnCodeBlock).toHaveAttribute("data-lang", "bash")
74 expect(yarnCodeBlock).toHaveTextContent("yarn add @medusajs/medusa")
75 expect(codeTabsChildren![1]).toHaveAttribute("data-label", "pnpm")
76 expect(codeTabsChildren![1]).toHaveAttribute("data-value", "pnpm")
77 const pnpmCodeBlock = codeTabsChildren![1].querySelector(
78 "[data-testid='code-block']"
79 )
80 expect(pnpmCodeBlock).toBeInTheDocument()
81 expect(pnpmCodeBlock).toHaveAttribute("data-lang", "bash")
82 expect(pnpmCodeBlock).toHaveTextContent("pnpm add @medusajs/medusa")
83 expect(codeTabsChildren![2]).toHaveAttribute("data-label", "npm")
84 expect(codeTabsChildren![2]).toHaveAttribute("data-value", "npm")
85 const npmCodeBlock = codeTabsChildren![2].querySelector(
86 "[data-testid='code-block']"
87 )
88 expect(npmCodeBlock).toBeInTheDocument()
89 expect(npmCodeBlock).toHaveAttribute("data-lang", "bash")
90 expect(npmCodeBlock).toHaveTextContent("npm install @medusajs/medusa")
91 })
92
93 test("renders npm2yarn code with custom code options", () => {
94 const { container } = render(
95 <Npm2YarnCode
96 npmCode="npm install @medusajs/medusa"
97 title="Custom Title"
98 />
99 )
100 expect(container).toBeInTheDocument()
101 const codeBlock = container.querySelector("[data-testid='code-block']")
102 expect(codeBlock).toBeInTheDocument()
103 expect(codeBlock).toHaveAttribute("data-title", "Custom Title")
104 })
105 })