|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { validateInteractConfig } from '../../src'; |
| 3 | + |
| 4 | +// CSS property names may be camelCase or kebab-case (Interact normalizes either |
| 5 | +// form); only names that are neither are reported. Source: every trigger rule. |
| 6 | + |
| 7 | +const CODE = 'INVALID_CSS_PROPERTY_NAME'; |
| 8 | + |
| 9 | +function validateKeyframeProperty(property: string, value: string = 'red') { |
| 10 | + return validateInteractConfig({ |
| 11 | + interactions: [ |
| 12 | + { |
| 13 | + key: 'el', |
| 14 | + trigger: 'viewEnter', |
| 15 | + effects: [ |
| 16 | + { |
| 17 | + duration: 400, |
| 18 | + keyframeEffect: { name: 'kf', keyframes: [{ [property]: value }] }, |
| 19 | + }, |
| 20 | + ], |
| 21 | + }, |
| 22 | + ], |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function validateStyleProperty(name: string) { |
| 27 | + return validateInteractConfig({ |
| 28 | + interactions: [ |
| 29 | + { |
| 30 | + key: 'el', |
| 31 | + trigger: 'hover', |
| 32 | + effects: [ |
| 33 | + { |
| 34 | + transition: { duration: 200, styleProperties: [{ name, value: 'red' }] }, |
| 35 | + }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + ], |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe('cssSyntax — INVALID_CSS_PROPERTY_NAME', () => { |
| 43 | + it('warns for a keyframe property that is neither camelCase nor kebab-case', () => { |
| 44 | + const result = validateKeyframeProperty('background-Color'); |
| 45 | + const err = result.errors.find((e) => e.code === CODE); |
| 46 | + |
| 47 | + expect(err).toBeDefined(); |
| 48 | + expect(err?.severity).toBe('warning'); |
| 49 | + expect(err?.path).toEqual([ |
| 50 | + 'interactions', |
| 51 | + 0, |
| 52 | + 'effects', |
| 53 | + 0, |
| 54 | + 'keyframeEffect', |
| 55 | + 'keyframes', |
| 56 | + 0, |
| 57 | + 'background-Color', |
| 58 | + ]); |
| 59 | + expect(err?.message).toContain('background-Color'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('warns for a state-effect style property that is neither casing', () => { |
| 63 | + const result = validateStyleProperty('BackgroundColor'); |
| 64 | + const err = result.errors.find((e) => e.code === CODE); |
| 65 | + |
| 66 | + expect(err).toBeDefined(); |
| 67 | + expect(err?.severity).toBe('warning'); |
| 68 | + expect(err?.path).toEqual([ |
| 69 | + 'interactions', |
| 70 | + 0, |
| 71 | + 'effects', |
| 72 | + 0, |
| 73 | + 'transition', |
| 74 | + 'styleProperties', |
| 75 | + 0, |
| 76 | + 'name', |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('warns for a `transitionProperties` name that is neither casing', () => { |
| 81 | + const result = validateInteractConfig({ |
| 82 | + interactions: [ |
| 83 | + { |
| 84 | + key: 'el', |
| 85 | + trigger: 'hover', |
| 86 | + effects: [ |
| 87 | + { |
| 88 | + transitionProperties: [{ name: 'Background Color', value: 'red', duration: 200 }], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + ], |
| 93 | + }); |
| 94 | + const err = result.errors.find((e) => e.code === CODE); |
| 95 | + |
| 96 | + expect(err).toBeDefined(); |
| 97 | + expect(err?.path).toEqual(['interactions', 0, 'effects', 0, 'transitionProperties', 0, 'name']); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('no warning for the documented valid patterns', () => { |
| 101 | + // `valid` must be asserted alongside the missing warning: a structural (zod) rejection |
| 102 | + // skips the semantic layer entirely, which would also produce zero INVALID_CSS_PROPERTY_NAME |
| 103 | + // issues - and would mean the casing was never accepted in the first place. |
| 104 | + function expectAccepted(result: ReturnType<typeof validateInteractConfig>) { |
| 105 | + expect(result.errors.filter((e) => e.code.startsWith('SCHEMA_'))).toEqual([]); |
| 106 | + expect(result.valid).toBe(true); |
| 107 | + expect(result.errors.filter((e) => e.code === CODE)).toHaveLength(0); |
| 108 | + } |
| 109 | + |
| 110 | + it('does not warn for a camelCase keyframe property', () => { |
| 111 | + expectAccepted(validateKeyframeProperty('backgroundColor')); |
| 112 | + }); |
| 113 | + |
| 114 | + it('does not warn for a kebab-case keyframe property', () => { |
| 115 | + expectAccepted(validateKeyframeProperty('background-color')); |
| 116 | + }); |
| 117 | + |
| 118 | + it('does not warn for a vendor-prefixed keyframe property in either casing', () => { |
| 119 | + expectAccepted(validateKeyframeProperty('-webkit-text-stroke', '1px red')); |
| 120 | + expectAccepted(validateKeyframeProperty('webkitTextStroke', '1px red')); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not warn for WAAPI keyframe keywords', () => { |
| 124 | + expectAccepted( |
| 125 | + validateInteractConfig({ |
| 126 | + interactions: [ |
| 127 | + { |
| 128 | + key: 'el', |
| 129 | + trigger: 'viewEnter', |
| 130 | + effects: [ |
| 131 | + { |
| 132 | + duration: 400, |
| 133 | + keyframeEffect: { |
| 134 | + name: 'kf', |
| 135 | + keyframes: [{ opacity: 0, offset: 0, easing: 'ease-in', composite: 'add' }], |
| 136 | + }, |
| 137 | + }, |
| 138 | + ], |
| 139 | + }, |
| 140 | + ], |
| 141 | + }), |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('does not warn for a CSS custom property (--*) in either shape', () => { |
| 146 | + expectAccepted(validateKeyframeProperty('--my-var', '1')); |
| 147 | + expectAccepted(validateKeyframeProperty('--myVar', '1')); |
| 148 | + expectAccepted(validateStyleProperty('--myVar')); |
| 149 | + }); |
| 150 | + |
| 151 | + it('does not warn for camelCase or kebab-case style properties', () => { |
| 152 | + expectAccepted(validateStyleProperty('backgroundColor')); |
| 153 | + expectAccepted(validateStyleProperty('background-color')); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments