|
1 | 1 | import {test, expect, describe} from 'vitest' |
2 | 2 | import {isValidSuitCssProperty, isValidCustomProperty} from './validate.js' |
3 | 3 |
|
4 | | -describe('valid patterns', () => { |
5 | | - test('basic properties', () => { |
6 | | - expect(isValidSuitCssProperty('--Button-color', 'Button')).toBe(true) |
7 | | - expect(isValidSuitCssProperty('--Button-backgroundColor', 'Button')).toBe( |
8 | | - true, |
9 | | - ) |
| 4 | +describe('isValidCustomProperty (default)', () => { |
| 5 | + test('accepts valid component names with any suffix', () => { |
| 6 | + expect(isValidCustomProperty('--Button-color', 'Button')).toBe(true) |
| 7 | + expect(isValidCustomProperty('--Button-Color', 'Button')).toBe(true) |
| 8 | + expect(isValidCustomProperty('--Button-123', 'Button')).toBe(true) |
| 9 | + expect(isValidCustomProperty('--Button', 'Button')).toBe(true) |
| 10 | + expect( |
| 11 | + isValidCustomProperty('--UserProfile-avatar-size', 'UserProfile'), |
| 12 | + ).toBe(true) |
10 | 13 | }) |
11 | 14 |
|
12 | | - test('descendants', () => { |
13 | | - expect(isValidSuitCssProperty('--Button-icon-size', 'Button')).toBe(true) |
| 15 | + test('rejects wrong component names', () => { |
| 16 | + expect(isValidCustomProperty('--Wrong-color', 'Button')).toBe(false) |
| 17 | + expect(isValidCustomProperty('--button-color', 'Button')).toBe(false) |
14 | 18 | }) |
| 19 | +}) |
15 | 20 |
|
16 | | - test('modifiers', () => { |
| 21 | +describe('isValidSuitCssProperty (strict SUIT CSS)', () => { |
| 22 | + test('accepts basic valid patterns', () => { |
| 23 | + expect(isValidSuitCssProperty('--Button-color', 'Button')).toBe(true) |
| 24 | + expect(isValidSuitCssProperty('--Button-icon-size', 'Button')).toBe(true) |
17 | 25 | expect(isValidSuitCssProperty('--Button--primary-color', 'Button')).toBe( |
18 | 26 | true, |
19 | 27 | ) |
20 | | - }) |
21 | | - |
22 | | - test('states', () => { |
23 | 28 | expect(isValidSuitCssProperty('--Button-onHover-color', 'Button')).toBe( |
24 | 29 | true, |
25 | 30 | ) |
26 | 31 | }) |
27 | 32 |
|
28 | | - test('complex combinations', () => { |
29 | | - expect( |
30 | | - isValidSuitCssProperty('--Button-icon-onHover-color', 'Button'), |
31 | | - ).toBe(true) |
32 | | - expect( |
33 | | - isValidSuitCssProperty( |
34 | | - '--Button--primary-onHover-backgroundColor', |
35 | | - 'Button', |
36 | | - ), |
37 | | - ).toBe(true) |
38 | | - }) |
39 | | -}) |
40 | | - |
41 | | -describe('invalid patterns', () => { |
42 | | - test('wrong component name', () => { |
43 | | - expect(isValidSuitCssProperty('--Wrong-color', 'Button')).toBe(false) |
44 | | - }) |
45 | | - |
46 | | - test('malformed properties', () => { |
| 33 | + test('rejects what default function accepts', () => { |
47 | 34 | expect(isValidSuitCssProperty('--Button', 'Button')).toBe(false) |
48 | | - expect(isValidSuitCssProperty('--Button-', 'Button')).toBe(false) |
49 | | - expect(isValidSuitCssProperty('--ButtonColor', 'Button')).toBe(false) |
50 | | - }) |
51 | | - |
52 | | - test('case violations', () => { |
53 | 35 | expect(isValidSuitCssProperty('--Button-Color', 'Button')).toBe(false) |
54 | 36 | expect(isValidSuitCssProperty('--Button-Icon-size', 'Button')).toBe(false) |
55 | | - }) |
56 | | - |
57 | | - test('invalid syntax', () => { |
| 37 | + expect(isValidSuitCssProperty('--Button-123', 'Button')).toBe(false) |
58 | 38 | expect(isValidSuitCssProperty('--Button---primary-color', 'Button')).toBe( |
59 | 39 | false, |
60 | 40 | ) |
61 | | - }) |
62 | | -}) |
63 | | - |
64 | | -describe('different component names', () => { |
65 | | - test('validates component name matching', () => { |
66 | | - expect( |
67 | | - isValidSuitCssProperty('--UserProfile-avatar-size', 'UserProfile'), |
68 | | - ).toBe(true) |
69 | | - expect(isValidSuitCssProperty('--Button-color', 'UserProfile')).toBe(false) |
| 41 | + expect(isValidSuitCssProperty('--Button--', 'Button')).toBe(false) |
70 | 42 | }) |
71 | 43 | }) |
72 | 44 |
|
73 | 45 | describe('custom configuration', () => { |
74 | 46 | test('accepts custom regex pattern', () => { |
75 | 47 | const underscorePattern = /^_[a-z][a-zA-Z]*$/ |
76 | | - |
77 | 48 | expect( |
78 | 49 | isValidCustomProperty('--Button_color', 'Button', { |
79 | 50 | pattern: underscorePattern, |
80 | 51 | }), |
81 | 52 | ).toBe(true) |
82 | 53 | }) |
| 54 | + |
| 55 | + test('rejects custom pattern when it does not match', () => { |
| 56 | + const underscorePattern = /^_[a-z][a-zA-Z]*$/ |
| 57 | + expect( |
| 58 | + isValidCustomProperty('--Button-color', 'Button', { |
| 59 | + pattern: underscorePattern, |
| 60 | + }), |
| 61 | + ).toBe(false) |
| 62 | + }) |
83 | 63 | }) |
0 commit comments