mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-26 14:16:08 +00:00
fix: update test coverage
This commit is contained in:
@@ -34,6 +34,49 @@ test('transform nested workspace folder for utils, website/src/utils', async ()
|
||||
|
||||
})
|
||||
|
||||
test.each([
|
||||
{
|
||||
name: "bare aliases",
|
||||
aliases: {
|
||||
components: "components",
|
||||
ui: "components/ui",
|
||||
lib: "lib",
|
||||
utils: "lib/utils",
|
||||
},
|
||||
buttonImport: `import { Button } from "components/ui/button"`,
|
||||
utilsImport: `import { cn } from "lib/utils"`,
|
||||
},
|
||||
{
|
||||
name: "path-like aliases",
|
||||
aliases: {
|
||||
components: "website/src/components",
|
||||
ui: "website/src/components/ui",
|
||||
lib: "website/src/lib",
|
||||
utils: "website/src/lib/utils",
|
||||
},
|
||||
buttonImport: `import { Button } from "website/src/components/ui/button"`,
|
||||
utilsImport: `import { cn } from "website/src/lib/utils"`,
|
||||
},
|
||||
])("transform import with non-sigil aliases: $name", async ({
|
||||
aliases,
|
||||
buttonImport,
|
||||
utilsImport,
|
||||
}) => {
|
||||
const result = await transform({
|
||||
filename: "test.ts",
|
||||
raw: `import { Button } from "@/registry/new-york/ui/button"
|
||||
import { cn } from "@/lib/utils"
|
||||
`,
|
||||
config: {
|
||||
tsx: true,
|
||||
aliases,
|
||||
},
|
||||
})
|
||||
|
||||
expect(result).toContain(buttonImport)
|
||||
expect(result).toContain(utilsImport)
|
||||
})
|
||||
|
||||
test("transform import", async () => {
|
||||
expect(
|
||||
await transform({
|
||||
|
||||
18
packages/tests/fixtures/vite-monorepo-imports/apps/web/components.json
vendored
Normal file
18
packages/tests/fixtures/vite-monorepo-imports/apps/web/components.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"style": "new-york",
|
||||
"tailwind": {
|
||||
"config": "",
|
||||
"css": "../../packages/ui/src/styles/globals.css",
|
||||
"baseColor": "zinc",
|
||||
"cssVariables": true
|
||||
},
|
||||
"rsc": false,
|
||||
"tsx": true,
|
||||
"aliases": {
|
||||
"components": "#components",
|
||||
"ui": "@workspace/ui/components",
|
||||
"lib": "#lib",
|
||||
"hooks": "#hooks",
|
||||
"utils": "@workspace/ui/lib/utils"
|
||||
}
|
||||
}
|
||||
12
packages/tests/fixtures/vite-monorepo-imports/apps/web/package.json
vendored
Normal file
12
packages/tests/fixtures/vite-monorepo-imports/apps/web/package.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "web",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"imports": {
|
||||
"#*": "./src/*"
|
||||
},
|
||||
"dependencies": {
|
||||
"@workspace/ui": "workspace:*",
|
||||
"tailwindcss": "^4.2.1"
|
||||
}
|
||||
}
|
||||
1
packages/tests/fixtures/vite-monorepo-imports/apps/web/src/main.tsx
vendored
Normal file
1
packages/tests/fixtures/vite-monorepo-imports/apps/web/src/main.tsx
vendored
Normal file
@@ -0,0 +1 @@
|
||||
console.log("web")
|
||||
8
packages/tests/fixtures/vite-monorepo-imports/apps/web/tsconfig.json
vendored
Normal file
8
packages/tests/fixtures/vite-monorepo-imports/apps/web/tsconfig.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolvePackageJsonImports": true
|
||||
}
|
||||
}
|
||||
3
packages/tests/fixtures/vite-monorepo-imports/apps/web/vite.config.ts
vendored
Normal file
3
packages/tests/fixtures/vite-monorepo-imports/apps/web/vite.config.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
import { defineConfig } from "vite"
|
||||
|
||||
export default defineConfig({})
|
||||
6
packages/tests/fixtures/vite-monorepo-imports/package.json
vendored
Normal file
6
packages/tests/fixtures/vite-monorepo-imports/package.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "vite-monorepo-imports",
|
||||
"private": true,
|
||||
"packageManager": "pnpm@10.0.0",
|
||||
"workspaces": ["apps/*", "packages/*"]
|
||||
}
|
||||
18
packages/tests/fixtures/vite-monorepo-imports/packages/ui/components.json
vendored
Normal file
18
packages/tests/fixtures/vite-monorepo-imports/packages/ui/components.json
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"style": "new-york",
|
||||
"tailwind": {
|
||||
"config": "",
|
||||
"css": "src/styles/globals.css",
|
||||
"baseColor": "zinc",
|
||||
"cssVariables": true
|
||||
},
|
||||
"rsc": false,
|
||||
"tsx": true,
|
||||
"aliases": {
|
||||
"components": "#components",
|
||||
"ui": "#components",
|
||||
"lib": "#lib",
|
||||
"hooks": "#hooks",
|
||||
"utils": "#lib/utils"
|
||||
}
|
||||
}
|
||||
14
packages/tests/fixtures/vite-monorepo-imports/packages/ui/package.json
vendored
Normal file
14
packages/tests/fixtures/vite-monorepo-imports/packages/ui/package.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "@workspace/ui",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"imports": {
|
||||
"#*": "./src/*"
|
||||
},
|
||||
"exports": {
|
||||
"./globals.css": "./src/styles/globals.css",
|
||||
"./components/*": "./src/components/*.tsx",
|
||||
"./lib/*": "./src/lib/*.ts",
|
||||
"./hooks/*": "./src/hooks/*.ts"
|
||||
}
|
||||
}
|
||||
3
packages/tests/fixtures/vite-monorepo-imports/packages/ui/src/lib/utils.ts
vendored
Normal file
3
packages/tests/fixtures/vite-monorepo-imports/packages/ui/src/lib/utils.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export function cn(...classes: Array<string | false | null | undefined>) {
|
||||
return classes.filter(Boolean).join(" ")
|
||||
}
|
||||
1
packages/tests/fixtures/vite-monorepo-imports/packages/ui/src/styles/globals.css
vendored
Normal file
1
packages/tests/fixtures/vite-monorepo-imports/packages/ui/src/styles/globals.css
vendored
Normal file
@@ -0,0 +1 @@
|
||||
@import "tailwindcss";
|
||||
8
packages/tests/fixtures/vite-monorepo-imports/packages/ui/tsconfig.json
vendored
Normal file
8
packages/tests/fixtures/vite-monorepo-imports/packages/ui/tsconfig.json
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"resolvePackageJsonImports": true
|
||||
}
|
||||
}
|
||||
3
packages/tests/fixtures/vite-monorepo-imports/pnpm-workspace.yaml
vendored
Normal file
3
packages/tests/fixtures/vite-monorepo-imports/pnpm-workspace.yaml
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
packages:
|
||||
- apps/*
|
||||
- packages/*
|
||||
@@ -166,6 +166,39 @@ describe("shadcn add", () => {
|
||||
).toBe("Foo Bar")
|
||||
})
|
||||
|
||||
it("should preview add changes without writing files", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("next-app-init")
|
||||
|
||||
const result = await npxShadcn(fixturePath, ["add", "button", "--dry-run"])
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(result.stdout).toContain("shadcn add button (dry run)")
|
||||
expect(result.stdout).toContain("components/ui/button.tsx")
|
||||
expect(result.stdout).toContain("Run without --dry-run to apply.")
|
||||
expect(
|
||||
await fs.pathExists(path.join(fixturePath, "components/ui/button.tsx"))
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it("should show no changes for identical files with diff", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("next-app-init")
|
||||
|
||||
await npxShadcn(fixturePath, ["add", "button", "--yes"])
|
||||
|
||||
const result = await npxShadcn(fixturePath, [
|
||||
"add",
|
||||
"button",
|
||||
"--diff",
|
||||
"button",
|
||||
"--yes",
|
||||
])
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(result.stdout).toContain("shadcn add button (dry run)")
|
||||
expect(result.stdout).toContain("components/ui/button.tsx (skip)")
|
||||
expect(result.stdout).toContain("No changes.")
|
||||
})
|
||||
|
||||
it("should add item with target to src", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("vite-app")
|
||||
await npxShadcn(fixturePath, ["init", "--defaults"])
|
||||
@@ -230,6 +263,97 @@ describe("shadcn add", () => {
|
||||
`)
|
||||
})
|
||||
|
||||
it("should add monorepo components and rewrite app-local imports with package imports", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("vite-monorepo-imports")
|
||||
|
||||
const result = await npxShadcn(
|
||||
fixturePath,
|
||||
["add", "login-03", "-c", "apps/web", "--yes"],
|
||||
{ timeout: 300000 }
|
||||
)
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
|
||||
expect(
|
||||
await fs.pathExists(
|
||||
path.join(fixturePath, "apps/web/src/components/login-form.tsx")
|
||||
)
|
||||
).toBe(true)
|
||||
expect(
|
||||
await fs.pathExists(
|
||||
path.join(fixturePath, "packages/ui/src/components/button.tsx")
|
||||
)
|
||||
).toBe(true)
|
||||
expect(
|
||||
await fs.pathExists(
|
||||
path.join(fixturePath, "apps/web/src/components/ui/button.tsx")
|
||||
)
|
||||
).toBe(false)
|
||||
|
||||
const loginFormContent = await fs.readFile(
|
||||
path.join(fixturePath, "apps/web/src/components/login-form.tsx"),
|
||||
"utf-8"
|
||||
)
|
||||
expect(loginFormContent).toContain(
|
||||
'import { cn } from "@workspace/ui/lib/utils"'
|
||||
)
|
||||
expect(loginFormContent).toContain(
|
||||
'import { Button } from "@workspace/ui/components/button"'
|
||||
)
|
||||
|
||||
const buttonContent = await fs.readFile(
|
||||
path.join(fixturePath, "packages/ui/src/components/button.tsx"),
|
||||
"utf-8"
|
||||
)
|
||||
expect(buttonContent).toContain('import { cn } from "#lib/utils.ts"')
|
||||
}, 300000)
|
||||
|
||||
it("should preview monorepo adds without writing files", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("vite-monorepo-imports")
|
||||
|
||||
const result = await npxShadcn(
|
||||
fixturePath,
|
||||
["add", "login-03", "-c", "apps/web", "--dry-run", "--yes"],
|
||||
{ timeout: 300000 }
|
||||
)
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(result.stdout).toContain("shadcn add login-03 (dry run)")
|
||||
expect(result.stdout).toContain("../../packages/ui/src/components/button.tsx")
|
||||
expect(result.stdout).toContain("src/components/login-form.tsx")
|
||||
expect(
|
||||
await fs.pathExists(
|
||||
path.join(fixturePath, "apps/web/src/components/login-form.tsx")
|
||||
)
|
||||
).toBe(false)
|
||||
expect(
|
||||
await fs.pathExists(
|
||||
path.join(fixturePath, "packages/ui/src/components/button.tsx")
|
||||
)
|
||||
).toBe(false)
|
||||
}, 300000)
|
||||
|
||||
it("should show no changes for identical monorepo files with diff", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("vite-monorepo-imports")
|
||||
|
||||
await npxShadcn(
|
||||
fixturePath,
|
||||
["add", "login-03", "-c", "apps/web", "--yes"],
|
||||
{ timeout: 300000 }
|
||||
)
|
||||
|
||||
const result = await npxShadcn(
|
||||
fixturePath,
|
||||
["add", "login-03", "-c", "apps/web", "--diff", "login-form", "--yes"],
|
||||
{ timeout: 300000 }
|
||||
)
|
||||
|
||||
expect(result.exitCode).toBe(0)
|
||||
expect(result.stdout).toContain("shadcn add login-03 (dry run)")
|
||||
expect(result.stdout).toContain("src/components/login-form.tsx (skip)")
|
||||
expect(result.stdout).toContain("No changes.")
|
||||
}, 300000)
|
||||
|
||||
it("should add NOT update existing envVars", async () => {
|
||||
const fixturePath = await createFixtureTestDirectory("next-app")
|
||||
await npxShadcn(fixturePath, ["init", "--defaults"])
|
||||
|
||||
Reference in New Issue
Block a user