Skip to content

Commit 33ac8e0

Browse files
committed
Setup components tests
1 parent 3fccbfd commit 33ac8e0

File tree

11 files changed

+115
-10
lines changed

11 files changed

+115
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: libs/extractor/src/lib.rs
3+
assertion_line: 2140
4+
expression: "ToBTreeSet::from(extract(\"test.cjs\",\nr#\"'use client'\n\nimport type { Conditional } from 'src/types/utils'\n\nimport { DevupTheme } from '../types/theme'\n\n/**\n * Initialize the theme, if you can't use the `ThemeScript` component\n * e.g. in vite\n * @param auto - Whether to use the system theme\n * @param theme - The theme to use\n */\nexport function initTheme(\n auto?: boolean,\n theme?: Conditional<DevupTheme>,\n): void {\n if (theme) {\n document.documentElement.setAttribute('data-theme', theme)\n } else {\n console.log(\n 'hello',\n typeof process.env.DEVUP_UI_DEFAULT_THEME,\n process.env.DEVUP_UI_DEFAULT_THEME,\n localStorage.getItem('__DF_THEME_SELECTED__') ||\n (auto && window.matchMedia('(prefers-color-scheme:dark)').matches\n ? 'dark'\n : (process.env.DEVUP_UI_DEFAULT_THEME ?? 'default')),\n )\n document.documentElement.setAttribute(\n 'data-theme',\n localStorage.getItem('__DF_THEME_SELECTED__') ||\n (auto && window.matchMedia('(prefers-color-scheme:dark)').matches\n ? 'dark'\n : (process.env.DEVUP_UI_DEFAULT_THEME ?? 'default')),\n )\n }\n}\n\"#,\nExtractOption\n{ package: \"@devup-ui/react\".to_string(), css_file: None }).unwrap())"
5+
---
6+
ToBTreeSet {
7+
styles: {},
8+
code: "'use client'\n\nimport type { Conditional } from 'src/types/utils'\n\nimport { DevupTheme } from '../types/theme'\n\n/**\n * Initialize the theme, if you can't use the `ThemeScript` component\n * e.g. in vite\n * @param auto - Whether to use the system theme\n * @param theme - The theme to use\n */\nexport function initTheme(\n auto?: boolean,\n theme?: Conditional<DevupTheme>,\n): void {\n if (theme) {\n document.documentElement.setAttribute('data-theme', theme)\n } else {\n console.log(\n 'hello',\n typeof process.env.DEVUP_UI_DEFAULT_THEME,\n process.env.DEVUP_UI_DEFAULT_THEME,\n localStorage.getItem('__DF_THEME_SELECTED__') ||\n (auto && window.matchMedia('(prefers-color-scheme:dark)').matches\n ? 'dark'\n : (process.env.DEVUP_UI_DEFAULT_THEME ?? 'default')),\n )\n document.documentElement.setAttribute(\n 'data-theme',\n localStorage.getItem('__DF_THEME_SELECTED__') ||\n (auto && window.matchMedia('(prefers-color-scheme:dark)').matches\n ? 'dark'\n : (process.env.DEVUP_UI_DEFAULT_THEME ?? 'default')),\n )\n }\n}\n",
9+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"@changesets/cli": "^2.29.5",
2020
"@types/node": "^24.0.7",
2121
"happy-dom": "^18.0.1",
22-
"@testing-library/react": "^16.3.0"
22+
"@testing-library/react": "^16.3.0",
23+
"@testing-library/jest-dom": "^6.6.3"
2324
},
2425
"author": "devfive",
2526
"packageManager": "[email protected]",

packages/components/setupTests.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@testing-library/jest-dom/vitest'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
describe('export', () => {
2+
it('should export components', async () => {
3+
const index = await import('../index')
4+
expect({ ...index }).toEqual({
5+
Button: expect.any(Function),
6+
})
7+
})
8+
})
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Button > should disable 1`] = `
4+
<div>
5+
<button
6+
aria-disabled="true"
7+
aria-label="button"
8+
class="d16 d17 d18 d19 d20 d21 d1 d2 d3 d22 d23 typo-buttonxs d24 d25 d26 d27 d28 d7 d8 d9 d10 d69 d70 d71 d72 d73 d74 d75 d76 d77 d78 d79 d80 d81 d82 d83 d84 d85 d86 "
9+
disabled=""
10+
type="button"
11+
>
12+
<div
13+
class="d65 d66 d67 d68"
14+
>
15+
<div
16+
class=" d62 d63 "
17+
>
18+
Click me
19+
</div>
20+
</div>
21+
</button>
22+
</div>
23+
`;
24+
25+
exports[`Button > should render 1`] = `
26+
<div>
27+
<button
28+
aria-label="button"
29+
class="d16 d17 d18 d19 d20 d21 d1 d2 d3 d22 d23 typo-buttonxs d24 d25 d26 d27 d28 d7 d8 d9 d10 d69 d70 d71 d72 d73 d74 d75 d76 d77 d78 d79 d80 d81 d82 d83 d84 d85 d86 "
30+
type="button"
31+
>
32+
<div
33+
class="d65 d66 d67 d68"
34+
>
35+
<div
36+
class=" d62 d63 "
37+
>
38+
Click me
39+
</div>
40+
</div>
41+
</button>
42+
</div>
43+
`;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { render } from '@testing-library/react'
2+
import { expect } from 'vitest'
3+
4+
import { Button } from '../index'
5+
6+
describe('Button', () => {
7+
it('should render', () => {
8+
const { container } = render(<Button>Click me</Button>)
9+
expect(container).toMatchSnapshot()
10+
})
11+
12+
it('should disable', () => {
13+
const { container } = render(<Button disabled>Click me</Button>)
14+
expect(container).toMatchSnapshot()
15+
expect(container.querySelector('button')).toHaveAttribute('disabled')
16+
})
17+
})

packages/components/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"types": ["vite/client", "vitest/importMeta", "vitest/globals"],
3+
"types": ["vite/client", "vitest/importMeta", "vitest/globals", "@testing-library/jest-dom"],
44
"strict": true,
55
"target": "ESNext",
66
"declaration": true,
@@ -23,5 +23,5 @@
2323
"baseUrl": ".",
2424
"jsx": "react-jsx"
2525
},
26-
"include": ["src/**/*", ".storybook/**/*"]
26+
"include": ["src/**/*", ".storybook/**/*", "./setupTests.ts"]
2727
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { DevupUI } from '@devup-ui/vite-plugin'
2+
import { defineProject } from 'vitest/config'
3+
4+
export default defineProject({
5+
test: {
6+
environment: 'happy-dom',
7+
globals: true,
8+
setupFiles: ['./setupTests.ts'],
9+
},
10+
plugins: [DevupUI()],
11+
})

packages/vite-plugin/src/plugin.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,18 @@ export function DevupUI({
7373
return {
7474
name: 'devup-ui',
7575
config() {
76+
const theme = getDefaultTheme()
77+
const define: Record<string, string> = {}
78+
if (theme) {
79+
define['process.env.DEVUP_UI_DEFAULT_THEME'] = JSON.stringify(theme)
80+
}
7681
const ret: Omit<UserConfig, 'plugins'> = {
7782
server: {
7883
watch: {
7984
ignored: [`!${devupPath}`],
8085
},
8186
},
82-
define: {
83-
'process.env.DEVUP_UI_DEFAULT_THEME':
84-
JSON.stringify(getDefaultTheme()),
85-
},
87+
define,
8688
optimizeDeps: {
8789
exclude: include,
8890
},

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)