@constela/ui 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +160 -0
- package/components/alert/alert.constela.json +22 -0
- package/components/alert/alert.styles.json +14 -0
- package/components/alert/alert.test.ts +173 -0
- package/components/avatar/avatar.constela.json +32 -0
- package/components/avatar/avatar.styles.json +15 -0
- package/components/avatar/avatar.test.ts +197 -0
- package/components/badge/badge.constela.json +19 -0
- package/components/badge/badge.styles.json +16 -0
- package/components/badge/badge.test.ts +135 -0
- package/components/breadcrumb/breadcrumb.constela.json +17 -0
- package/components/breadcrumb/breadcrumb.styles.json +5 -0
- package/components/breadcrumb/breadcrumb.test.ts +149 -0
- package/components/button/README.md +164 -0
- package/components/button/button.constela.json +27 -0
- package/components/button/button.styles.json +25 -0
- package/components/button/button.test.ts +233 -0
- package/components/card/card.constela.json +21 -0
- package/components/card/card.styles.json +14 -0
- package/components/card/card.test.ts +154 -0
- package/components/checkbox/checkbox.constela.json +33 -0
- package/components/checkbox/checkbox.styles.json +15 -0
- package/components/checkbox/checkbox.test.ts +275 -0
- package/components/container/container.constela.json +21 -0
- package/components/container/container.styles.json +18 -0
- package/components/container/container.test.ts +164 -0
- package/components/dialog/dialog.constela.json +19 -0
- package/components/dialog/dialog.styles.json +5 -0
- package/components/dialog/dialog.test.ts +139 -0
- package/components/grid/grid.constela.json +23 -0
- package/components/grid/grid.styles.json +25 -0
- package/components/grid/grid.test.ts +193 -0
- package/components/input/input.constela.json +34 -0
- package/components/input/input.styles.json +19 -0
- package/components/input/input.test.ts +301 -0
- package/components/pagination/pagination.constela.json +22 -0
- package/components/pagination/pagination.styles.json +15 -0
- package/components/pagination/pagination.test.ts +170 -0
- package/components/popover/popover.constela.json +20 -0
- package/components/popover/popover.styles.json +16 -0
- package/components/popover/popover.test.ts +165 -0
- package/components/radio/radio.constela.json +31 -0
- package/components/radio/radio.styles.json +15 -0
- package/components/radio/radio.test.ts +253 -0
- package/components/select/select.constela.json +32 -0
- package/components/select/select.styles.json +15 -0
- package/components/select/select.test.ts +257 -0
- package/components/skeleton/skeleton.constela.json +24 -0
- package/components/skeleton/skeleton.styles.json +5 -0
- package/components/skeleton/skeleton.test.ts +164 -0
- package/components/stack/stack.constela.json +27 -0
- package/components/stack/stack.styles.json +33 -0
- package/components/stack/stack.test.ts +261 -0
- package/components/switch/switch.constela.json +29 -0
- package/components/switch/switch.styles.json +15 -0
- package/components/switch/switch.test.ts +224 -0
- package/components/tabs/tabs.constela.json +21 -0
- package/components/tabs/tabs.styles.json +14 -0
- package/components/tabs/tabs.test.ts +163 -0
- package/components/textarea/textarea.constela.json +34 -0
- package/components/textarea/textarea.styles.json +15 -0
- package/components/textarea/textarea.test.ts +290 -0
- package/components/toast/toast.constela.json +23 -0
- package/components/toast/toast.styles.json +17 -0
- package/components/toast/toast.test.ts +183 -0
- package/components/tooltip/tooltip.constela.json +21 -0
- package/components/tooltip/tooltip.styles.json +16 -0
- package/components/tooltip/tooltip.test.ts +164 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +83 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constela Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# @constela/ui
|
|
2
|
+
|
|
3
|
+
Copy-paste UI components for Constela - a shadcn/ui style component library.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides a collection of pre-built, accessible UI components written in Constela JSON DSL. Each component is designed to be copied directly into your project and customized as needed.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @constela/ui
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Copy Component Files
|
|
18
|
+
|
|
19
|
+
Components are located in the `components/` directory. Copy the component files you need into your project:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
components/
|
|
23
|
+
├── button/
|
|
24
|
+
│ ├── button.constela.json # Component definition
|
|
25
|
+
│ ├── button.styles.json # Style presets
|
|
26
|
+
│ └── README.md # Usage documentation
|
|
27
|
+
├── input/
|
|
28
|
+
├── select/
|
|
29
|
+
└── ...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Example: Using Button Component
|
|
33
|
+
|
|
34
|
+
1. Copy `components/button/button.constela.json` to your project
|
|
35
|
+
2. Import styles from `components/button/button.styles.json`
|
|
36
|
+
3. Use the component in your Constela program:
|
|
37
|
+
|
|
38
|
+
```json
|
|
39
|
+
{
|
|
40
|
+
"kind": "component",
|
|
41
|
+
"name": "Button",
|
|
42
|
+
"props": {
|
|
43
|
+
"variant": { "expr": "lit", "value": "default" },
|
|
44
|
+
"size": { "expr": "lit", "value": "default" }
|
|
45
|
+
},
|
|
46
|
+
"children": [
|
|
47
|
+
{ "kind": "text", "value": { "expr": "lit", "value": "Click me" } }
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Available Components
|
|
53
|
+
|
|
54
|
+
### Basic (7)
|
|
55
|
+
- **Button** - Clickable button with variants and sizes
|
|
56
|
+
- **Input** - Text input field
|
|
57
|
+
- **Select** - Dropdown select
|
|
58
|
+
- **Checkbox** - Checkbox input
|
|
59
|
+
- **Radio** - Radio button
|
|
60
|
+
- **Switch** - Toggle switch
|
|
61
|
+
- **Textarea** - Multi-line text input
|
|
62
|
+
|
|
63
|
+
### Feedback (6)
|
|
64
|
+
- **Alert** - Alert messages
|
|
65
|
+
- **Toast** - Toast notifications
|
|
66
|
+
- **Dialog** - Modal dialog
|
|
67
|
+
- **Modal** - Modal overlay
|
|
68
|
+
- **Tooltip** - Hover tooltip
|
|
69
|
+
- **Popover** - Click-triggered popover
|
|
70
|
+
|
|
71
|
+
### Data Display (5)
|
|
72
|
+
- **Table** - Data table
|
|
73
|
+
- **Card** - Card container
|
|
74
|
+
- **Badge** - Inline badge
|
|
75
|
+
- **Avatar** - User avatar
|
|
76
|
+
- **Skeleton** - Loading placeholder
|
|
77
|
+
|
|
78
|
+
### Navigation (3)
|
|
79
|
+
- **Tabs** - Tab navigation
|
|
80
|
+
- **Breadcrumb** - Breadcrumb navigation
|
|
81
|
+
- **Pagination** - Page pagination
|
|
82
|
+
|
|
83
|
+
### Layout (3)
|
|
84
|
+
- **Container** - Max-width container
|
|
85
|
+
- **Grid** - CSS Grid layout
|
|
86
|
+
- **Stack** - Flexbox stack
|
|
87
|
+
|
|
88
|
+
## Style System
|
|
89
|
+
|
|
90
|
+
Each component uses the CVA-like style system with:
|
|
91
|
+
|
|
92
|
+
- **base** - Common styles applied to all variants
|
|
93
|
+
- **variants** - Named variant options (e.g., "variant", "size")
|
|
94
|
+
- **defaultVariants** - Default values when not specified
|
|
95
|
+
|
|
96
|
+
### Example Style Preset
|
|
97
|
+
|
|
98
|
+
```json
|
|
99
|
+
{
|
|
100
|
+
"button": {
|
|
101
|
+
"base": "inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 disabled:pointer-events-none disabled:opacity-50",
|
|
102
|
+
"variants": {
|
|
103
|
+
"variant": {
|
|
104
|
+
"default": "bg-primary text-primary-foreground hover:bg-primary/90",
|
|
105
|
+
"destructive": "bg-destructive text-destructive-foreground hover:bg-destructive/90",
|
|
106
|
+
"outline": "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
|
|
107
|
+
"secondary": "bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
108
|
+
"ghost": "hover:bg-accent hover:text-accent-foreground",
|
|
109
|
+
"link": "text-primary underline-offset-4 hover:underline"
|
|
110
|
+
},
|
|
111
|
+
"size": {
|
|
112
|
+
"default": "h-10 px-4 py-2",
|
|
113
|
+
"sm": "h-9 rounded-md px-3",
|
|
114
|
+
"lg": "h-11 rounded-md px-8",
|
|
115
|
+
"icon": "h-10 w-10"
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
"defaultVariants": {
|
|
119
|
+
"variant": "default",
|
|
120
|
+
"size": "default"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Accessibility
|
|
127
|
+
|
|
128
|
+
All components include proper ARIA attributes:
|
|
129
|
+
|
|
130
|
+
- `aria-label` - Descriptive labels
|
|
131
|
+
- `aria-disabled` - Disabled state indication
|
|
132
|
+
- `role` - Semantic roles for interactive elements
|
|
133
|
+
- `aria-live` - Live region announcements
|
|
134
|
+
|
|
135
|
+
## API Reference
|
|
136
|
+
|
|
137
|
+
### validateComponent
|
|
138
|
+
|
|
139
|
+
Validate a Constela component definition:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { validateComponent } from '@constela/ui';
|
|
143
|
+
|
|
144
|
+
const result = validateComponent(componentJson);
|
|
145
|
+
if (result.valid) {
|
|
146
|
+
console.log('Component is valid');
|
|
147
|
+
} else {
|
|
148
|
+
console.error('Validation errors:', result.errors);
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Component Types
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
import type { ButtonComponent, InputComponent } from '@constela/ui';
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## License
|
|
159
|
+
|
|
160
|
+
MIT
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"params": {
|
|
3
|
+
"variant": { "type": "string", "required": false },
|
|
4
|
+
"title": { "type": "string", "required": false }
|
|
5
|
+
},
|
|
6
|
+
"view": {
|
|
7
|
+
"kind": "element",
|
|
8
|
+
"tag": "div",
|
|
9
|
+
"props": {
|
|
10
|
+
"role": { "expr": "lit", "value": "alert" },
|
|
11
|
+
"aria-live": { "expr": "lit", "value": "assertive" },
|
|
12
|
+
"className": {
|
|
13
|
+
"expr": "style",
|
|
14
|
+
"preset": "alertStyles",
|
|
15
|
+
"props": {
|
|
16
|
+
"variant": { "expr": "param", "name": "variant" }
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"children": [{ "kind": "slot" }]
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"alertStyles": {
|
|
3
|
+
"base": "relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",
|
|
4
|
+
"variants": {
|
|
5
|
+
"variant": {
|
|
6
|
+
"default": "bg-background text-foreground",
|
|
7
|
+
"destructive": "border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"defaultVariants": {
|
|
11
|
+
"variant": "default"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test suite for Alert component
|
|
3
|
+
*
|
|
4
|
+
* @constela/ui Alert component tests following TDD methodology.
|
|
5
|
+
* These tests verify the Alert component structure, params, styles, and accessibility.
|
|
6
|
+
*
|
|
7
|
+
* Coverage:
|
|
8
|
+
* - Component structure validation
|
|
9
|
+
* - Params definition validation
|
|
10
|
+
* - Style preset validation
|
|
11
|
+
* - Accessibility attributes
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, beforeAll } from 'vitest';
|
|
15
|
+
import {
|
|
16
|
+
loadComponentForTesting,
|
|
17
|
+
assertValidComponent,
|
|
18
|
+
assertValidStylePreset,
|
|
19
|
+
hasParams,
|
|
20
|
+
isOptionalParam,
|
|
21
|
+
hasParamType,
|
|
22
|
+
getRootTag,
|
|
23
|
+
hasVariants,
|
|
24
|
+
hasVariantOptions,
|
|
25
|
+
hasDefaultVariants,
|
|
26
|
+
hasSlot,
|
|
27
|
+
findPropInView,
|
|
28
|
+
hasRole,
|
|
29
|
+
type ComponentTestContext,
|
|
30
|
+
} from '../../tests/helpers/test-utils.js';
|
|
31
|
+
|
|
32
|
+
describe('Alert Component', () => {
|
|
33
|
+
let ctx: ComponentTestContext;
|
|
34
|
+
|
|
35
|
+
beforeAll(() => {
|
|
36
|
+
ctx = loadComponentForTesting('alert');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// ==================== Component Structure Tests ====================
|
|
40
|
+
|
|
41
|
+
describe('Component Structure', () => {
|
|
42
|
+
it('should have valid component structure', () => {
|
|
43
|
+
assertValidComponent(ctx.component);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should have div as root element', () => {
|
|
47
|
+
const rootTag = getRootTag(ctx.component);
|
|
48
|
+
expect(rootTag).toBe('div');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should have role="alert" attribute', () => {
|
|
52
|
+
expect(hasRole(ctx.component.view, 'alert')).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should contain a slot for alert content', () => {
|
|
56
|
+
expect(hasSlot(ctx.component.view)).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should have className using StyleExpr', () => {
|
|
60
|
+
const className = findPropInView(ctx.component.view, 'className');
|
|
61
|
+
expect(className).not.toBeNull();
|
|
62
|
+
// StyleExpr should have expr: 'style' and preset reference
|
|
63
|
+
expect(className).toMatchObject({
|
|
64
|
+
expr: 'style',
|
|
65
|
+
preset: 'alertStyles',
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// ==================== Params Validation Tests ====================
|
|
71
|
+
|
|
72
|
+
describe('Params Validation', () => {
|
|
73
|
+
const expectedParams = ['variant', 'title'];
|
|
74
|
+
|
|
75
|
+
it('should have all expected params', () => {
|
|
76
|
+
expect(hasParams(ctx.component, expectedParams)).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('param: variant', () => {
|
|
80
|
+
it('should be optional', () => {
|
|
81
|
+
expect(isOptionalParam(ctx.component, 'variant')).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should have type string', () => {
|
|
85
|
+
expect(hasParamType(ctx.component, 'variant', 'string')).toBe(true);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('param: title', () => {
|
|
90
|
+
it('should be optional', () => {
|
|
91
|
+
expect(isOptionalParam(ctx.component, 'title')).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should have type string', () => {
|
|
95
|
+
expect(hasParamType(ctx.component, 'title', 'string')).toBe(true);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// ==================== Style Preset Tests ====================
|
|
101
|
+
|
|
102
|
+
describe('Style Preset', () => {
|
|
103
|
+
it('should have valid style preset structure', () => {
|
|
104
|
+
const alertStyles = ctx.styles['alertStyles'];
|
|
105
|
+
expect(alertStyles).toBeDefined();
|
|
106
|
+
assertValidStylePreset(alertStyles);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should have base classes', () => {
|
|
110
|
+
const alertStyles = ctx.styles['alertStyles'];
|
|
111
|
+
expect(alertStyles.base).toBeDefined();
|
|
112
|
+
expect(typeof alertStyles.base).toBe('string');
|
|
113
|
+
expect(alertStyles.base.length).toBeGreaterThan(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('variant options', () => {
|
|
117
|
+
const variantOptions = ['default', 'destructive'];
|
|
118
|
+
|
|
119
|
+
it('should have variant variants', () => {
|
|
120
|
+
const alertStyles = ctx.styles['alertStyles'];
|
|
121
|
+
expect(hasVariants(alertStyles, ['variant'])).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it.each(variantOptions)('should have %s variant option', (option) => {
|
|
125
|
+
const alertStyles = ctx.styles['alertStyles'];
|
|
126
|
+
expect(hasVariantOptions(alertStyles, 'variant', [option])).toBe(true);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('default variants', () => {
|
|
131
|
+
it('should have default variant set to default', () => {
|
|
132
|
+
const alertStyles = ctx.styles['alertStyles'];
|
|
133
|
+
expect(hasDefaultVariants(alertStyles, { variant: 'default' })).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// ==================== Accessibility Tests ====================
|
|
139
|
+
|
|
140
|
+
describe('Accessibility', () => {
|
|
141
|
+
it('should have role="alert" for screen readers', () => {
|
|
142
|
+
const role = findPropInView(ctx.component.view, 'role');
|
|
143
|
+
expect(role).not.toBeNull();
|
|
144
|
+
expect(role).toMatchObject({
|
|
145
|
+
expr: 'lit',
|
|
146
|
+
value: 'alert',
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should have aria-live="assertive" for important alerts', () => {
|
|
151
|
+
const ariaLive = findPropInView(ctx.component.view, 'aria-live');
|
|
152
|
+
expect(ariaLive).not.toBeNull();
|
|
153
|
+
expect(ariaLive).toMatchObject({
|
|
154
|
+
expr: 'lit',
|
|
155
|
+
value: 'assertive',
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// ==================== View Props Tests ====================
|
|
161
|
+
|
|
162
|
+
describe('View Props', () => {
|
|
163
|
+
it('should pass variant to StyleExpr', () => {
|
|
164
|
+
const className = findPropInView(ctx.component.view, 'className');
|
|
165
|
+
expect(className).toMatchObject({
|
|
166
|
+
expr: 'style',
|
|
167
|
+
props: expect.objectContaining({
|
|
168
|
+
variant: expect.objectContaining({ expr: 'param', name: 'variant' }),
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"params": {
|
|
3
|
+
"src": { "type": "string", "required": false },
|
|
4
|
+
"alt": { "type": "string", "required": false },
|
|
5
|
+
"fallback": { "type": "string", "required": false },
|
|
6
|
+
"size": { "type": "string", "required": false }
|
|
7
|
+
},
|
|
8
|
+
"view": {
|
|
9
|
+
"kind": "element",
|
|
10
|
+
"tag": "div",
|
|
11
|
+
"props": {
|
|
12
|
+
"className": {
|
|
13
|
+
"expr": "style",
|
|
14
|
+
"preset": "avatarStyles",
|
|
15
|
+
"props": {
|
|
16
|
+
"size": { "expr": "param", "name": "size" }
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"children": [
|
|
21
|
+
{
|
|
22
|
+
"kind": "element",
|
|
23
|
+
"tag": "img",
|
|
24
|
+
"props": {
|
|
25
|
+
"src": { "expr": "param", "name": "src" },
|
|
26
|
+
"alt": { "expr": "param", "name": "alt" },
|
|
27
|
+
"className": { "expr": "lit", "value": "aspect-square h-full w-full" }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test suite for Avatar component
|
|
3
|
+
*
|
|
4
|
+
* @constela/ui Avatar component tests following TDD methodology.
|
|
5
|
+
* These tests verify the Avatar component structure, params, styles, and accessibility.
|
|
6
|
+
*
|
|
7
|
+
* Coverage:
|
|
8
|
+
* - Component structure validation
|
|
9
|
+
* - Params definition validation
|
|
10
|
+
* - Style preset validation
|
|
11
|
+
* - Accessibility attributes (alt for image)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, it, expect, beforeAll } from 'vitest';
|
|
15
|
+
import {
|
|
16
|
+
loadComponentForTesting,
|
|
17
|
+
assertValidComponent,
|
|
18
|
+
assertValidStylePreset,
|
|
19
|
+
hasParams,
|
|
20
|
+
isOptionalParam,
|
|
21
|
+
hasParamType,
|
|
22
|
+
getRootTag,
|
|
23
|
+
hasVariants,
|
|
24
|
+
hasVariantOptions,
|
|
25
|
+
hasDefaultVariants,
|
|
26
|
+
hasSlot,
|
|
27
|
+
findPropInView,
|
|
28
|
+
type ComponentTestContext,
|
|
29
|
+
} from '../../tests/helpers/test-utils.js';
|
|
30
|
+
|
|
31
|
+
describe('Avatar Component', () => {
|
|
32
|
+
let ctx: ComponentTestContext;
|
|
33
|
+
|
|
34
|
+
beforeAll(() => {
|
|
35
|
+
ctx = loadComponentForTesting('avatar');
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// ==================== Component Structure Tests ====================
|
|
39
|
+
|
|
40
|
+
describe('Component Structure', () => {
|
|
41
|
+
it('should have valid component structure', () => {
|
|
42
|
+
assertValidComponent(ctx.component);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should have div as root element (container)', () => {
|
|
46
|
+
const rootTag = getRootTag(ctx.component);
|
|
47
|
+
expect(rootTag).toBe('div');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should NOT contain a slot (avatar displays image or fallback)', () => {
|
|
51
|
+
expect(hasSlot(ctx.component.view)).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should have className using StyleExpr with avatarStyles preset', () => {
|
|
55
|
+
const className = findPropInView(ctx.component.view, 'className');
|
|
56
|
+
expect(className).not.toBeNull();
|
|
57
|
+
// StyleExpr should have expr: 'style' and preset reference
|
|
58
|
+
expect(className).toMatchObject({
|
|
59
|
+
expr: 'style',
|
|
60
|
+
preset: 'avatarStyles',
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ==================== Params Validation Tests ====================
|
|
66
|
+
|
|
67
|
+
describe('Params Validation', () => {
|
|
68
|
+
const expectedParams = ['src', 'alt', 'fallback', 'size'];
|
|
69
|
+
|
|
70
|
+
it('should have all expected params', () => {
|
|
71
|
+
expect(hasParams(ctx.component, expectedParams)).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('param: src', () => {
|
|
75
|
+
it('should be optional', () => {
|
|
76
|
+
expect(isOptionalParam(ctx.component, 'src')).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should have type string', () => {
|
|
80
|
+
expect(hasParamType(ctx.component, 'src', 'string')).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('param: alt', () => {
|
|
85
|
+
it('should be optional', () => {
|
|
86
|
+
expect(isOptionalParam(ctx.component, 'alt')).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should have type string', () => {
|
|
90
|
+
expect(hasParamType(ctx.component, 'alt', 'string')).toBe(true);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('param: fallback', () => {
|
|
95
|
+
it('should be optional', () => {
|
|
96
|
+
expect(isOptionalParam(ctx.component, 'fallback')).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should have type string', () => {
|
|
100
|
+
expect(hasParamType(ctx.component, 'fallback', 'string')).toBe(true);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('param: size', () => {
|
|
105
|
+
it('should be optional', () => {
|
|
106
|
+
expect(isOptionalParam(ctx.component, 'size')).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should have type string', () => {
|
|
110
|
+
expect(hasParamType(ctx.component, 'size', 'string')).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// ==================== Style Preset Tests ====================
|
|
116
|
+
|
|
117
|
+
describe('Style Preset', () => {
|
|
118
|
+
it('should have valid style preset structure', () => {
|
|
119
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
120
|
+
expect(avatarStyles).toBeDefined();
|
|
121
|
+
assertValidStylePreset(avatarStyles);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should have base classes', () => {
|
|
125
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
126
|
+
expect(avatarStyles.base).toBeDefined();
|
|
127
|
+
expect(typeof avatarStyles.base).toBe('string');
|
|
128
|
+
expect(avatarStyles.base.length).toBeGreaterThan(0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should include avatar-specific base classes', () => {
|
|
132
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
133
|
+
// Avatar should have relative, rounded-full, overflow-hidden
|
|
134
|
+
expect(avatarStyles.base).toContain('relative');
|
|
135
|
+
expect(avatarStyles.base).toContain('rounded-full');
|
|
136
|
+
expect(avatarStyles.base).toContain('overflow-hidden');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('size options', () => {
|
|
140
|
+
const sizeOptions = ['default', 'sm', 'lg'];
|
|
141
|
+
|
|
142
|
+
it('should have size variants', () => {
|
|
143
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
144
|
+
expect(hasVariants(avatarStyles, ['size'])).toBe(true);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it.each(sizeOptions)('should have %s size option', (option) => {
|
|
148
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
149
|
+
expect(hasVariantOptions(avatarStyles, 'size', [option])).toBe(true);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe('default variants', () => {
|
|
154
|
+
it('should have default size set to default', () => {
|
|
155
|
+
const avatarStyles = ctx.styles['avatarStyles'];
|
|
156
|
+
expect(hasDefaultVariants(avatarStyles, { size: 'default' })).toBe(true);
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ==================== Accessibility Tests ====================
|
|
162
|
+
|
|
163
|
+
describe('Accessibility', () => {
|
|
164
|
+
it('should support alt attribute for image', () => {
|
|
165
|
+
const alt = findPropInView(ctx.component.view, 'alt');
|
|
166
|
+
expect(alt).not.toBeNull();
|
|
167
|
+
// Should reference the alt param
|
|
168
|
+
expect(alt).toMatchObject({
|
|
169
|
+
expr: 'param',
|
|
170
|
+
name: 'alt',
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// ==================== View Props Tests ====================
|
|
176
|
+
|
|
177
|
+
describe('View Props', () => {
|
|
178
|
+
it('should pass size to StyleExpr', () => {
|
|
179
|
+
const className = findPropInView(ctx.component.view, 'className');
|
|
180
|
+
expect(className).toMatchObject({
|
|
181
|
+
expr: 'style',
|
|
182
|
+
props: expect.objectContaining({
|
|
183
|
+
size: expect.objectContaining({ expr: 'param', name: 'size' }),
|
|
184
|
+
}),
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should pass src to image element', () => {
|
|
189
|
+
const src = findPropInView(ctx.component.view, 'src');
|
|
190
|
+
expect(src).not.toBeNull();
|
|
191
|
+
expect(src).toMatchObject({
|
|
192
|
+
expr: 'param',
|
|
193
|
+
name: 'src',
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"params": {
|
|
3
|
+
"variant": { "type": "string", "required": false }
|
|
4
|
+
},
|
|
5
|
+
"view": {
|
|
6
|
+
"kind": "element",
|
|
7
|
+
"tag": "div",
|
|
8
|
+
"props": {
|
|
9
|
+
"className": {
|
|
10
|
+
"expr": "style",
|
|
11
|
+
"preset": "badgeStyles",
|
|
12
|
+
"props": {
|
|
13
|
+
"variant": { "expr": "param", "name": "variant" }
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"children": [{ "kind": "slot" }]
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"badgeStyles": {
|
|
3
|
+
"base": "inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
|
|
4
|
+
"variants": {
|
|
5
|
+
"variant": {
|
|
6
|
+
"default": "border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
|
|
7
|
+
"secondary": "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
|
|
8
|
+
"destructive": "border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
|
|
9
|
+
"outline": "text-foreground"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"defaultVariants": {
|
|
13
|
+
"variant": "default"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|