-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathanimation.test.ts
More file actions
265 lines (239 loc) · 9.6 KB
/
Copy pathanimation.test.ts
File metadata and controls
265 lines (239 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { describe, it, expect } from 'vitest';
import {
TransitionPresetSchema,
EasingFunctionSchema,
TransitionConfigSchema,
AnimationTriggerSchema,
ComponentAnimationSchema,
PageTransitionSchema,
MotionConfigSchema,
type TransitionPreset,
type EasingFunction,
type TransitionConfig,
type AnimationTrigger,
type ComponentAnimation,
type PageTransition,
type MotionConfig,
} from './animation.zod';
describe('TransitionPresetSchema', () => {
it('should accept all valid presets', () => {
const presets = ['fade', 'slide_up', 'slide_down', 'slide_left', 'slide_right', 'scale', 'rotate', 'flip', 'none'] as const;
presets.forEach(preset => {
expect(() => TransitionPresetSchema.parse(preset)).not.toThrow();
});
});
it('should reject invalid presets', () => {
expect(() => TransitionPresetSchema.parse('dissolve')).toThrow();
expect(() => TransitionPresetSchema.parse('')).toThrow();
});
});
describe('EasingFunctionSchema', () => {
it('should accept all valid easing functions', () => {
const easings = ['linear', 'ease', 'ease_in', 'ease_out', 'ease_in_out', 'spring'] as const;
easings.forEach(easing => {
expect(() => EasingFunctionSchema.parse(easing)).not.toThrow();
});
});
it('should reject invalid easing functions', () => {
expect(() => EasingFunctionSchema.parse('bounce')).toThrow();
expect(() => EasingFunctionSchema.parse('')).toThrow();
});
});
describe('TransitionConfigSchema', () => {
it('should accept empty config', () => {
const result = TransitionConfigSchema.parse({});
expect(result).toEqual({});
});
it('should accept full config with all fields', () => {
const config: TransitionConfig = {
preset: 'fade',
duration: 200,
easing: 'ease_in_out',
delay: 50,
customKeyframes: 'bounce-in',
};
const result = TransitionConfigSchema.parse(config);
expect(result.preset).toBe('fade');
expect(result.duration).toBe(200);
expect(result.easing).toBe('ease_in_out');
expect(result.delay).toBe(50);
expect(result.customKeyframes).toBe('bounce-in');
});
it('should leave optional fields undefined when not provided', () => {
const result = TransitionConfigSchema.parse({ duration: 100 });
expect(result.duration).toBe(100);
expect(result.preset).toBeUndefined();
expect(result.easing).toBeUndefined();
expect(result.delay).toBeUndefined();
expect(result.customKeyframes).toBeUndefined();
});
});
describe('AnimationTriggerSchema', () => {
it('should accept all valid triggers', () => {
const triggers = ['on_mount', 'on_unmount', 'on_hover', 'on_focus', 'on_click', 'on_scroll', 'on_visible'] as const;
triggers.forEach(trigger => {
expect(() => AnimationTriggerSchema.parse(trigger)).not.toThrow();
});
});
it('should reject invalid triggers', () => {
expect(() => AnimationTriggerSchema.parse('on_drag')).toThrow();
expect(() => AnimationTriggerSchema.parse('')).toThrow();
});
});
describe('ComponentAnimationSchema', () => {
it('should apply default reducedMotion for empty config', () => {
const result = ComponentAnimationSchema.parse({});
expect(result.reducedMotion).toBe('respect');
});
it('should accept full config with enter/exit/hover/trigger/reducedMotion', () => {
const config: ComponentAnimation = {
enter: { preset: 'slide_up', duration: 300, easing: 'ease_out' },
exit: { preset: 'fade', duration: 200 },
hover: { preset: 'scale', duration: 150 },
trigger: 'on_visible',
reducedMotion: 'alternative',
};
const result = ComponentAnimationSchema.parse(config);
expect(result.enter?.preset).toBe('slide_up');
expect(result.exit?.preset).toBe('fade');
expect(result.hover?.preset).toBe('scale');
expect(result.trigger).toBe('on_visible');
expect(result.reducedMotion).toBe('alternative');
});
it('should accept disable for reducedMotion', () => {
const result = ComponentAnimationSchema.parse({ reducedMotion: 'disable' });
expect(result.reducedMotion).toBe('disable');
});
});
describe('PageTransitionSchema', () => {
it('should apply defaults for empty config', () => {
const result = PageTransitionSchema.parse({});
expect(result.type).toBe('fade');
expect(result.duration).toBe(300);
expect(result.easing).toBe('ease_in_out');
expect(result.crossFade).toBe(false);
});
it('should accept full config overriding defaults', () => {
const config: PageTransition = {
type: 'slide_left',
duration: 500,
easing: 'spring',
crossFade: true,
};
const result = PageTransitionSchema.parse(config);
expect(result.type).toBe('slide_left');
expect(result.duration).toBe(500);
expect(result.easing).toBe('spring');
expect(result.crossFade).toBe(true);
});
});
describe('MotionConfigSchema', () => {
it('should apply defaults for empty config', () => {
const result = MotionConfigSchema.parse({});
expect(result.enabled).toBe(true);
expect(result.reducedMotion).toBe(false);
});
it('should accept full config with componentAnimations record', () => {
const config: MotionConfig = {
defaultTransition: { preset: 'fade', duration: 250, easing: 'ease' },
pageTransitions: { type: 'slide_right', duration: 400, easing: 'ease_in_out', crossFade: false },
componentAnimations: {
card: { enter: { preset: 'scale', duration: 200 }, reducedMotion: 'respect' },
modal: { enter: { preset: 'slide_up' }, exit: { preset: 'fade' }, reducedMotion: 'disable' },
},
reducedMotion: true,
enabled: false,
};
const result = MotionConfigSchema.parse(config);
expect(result.defaultTransition?.preset).toBe('fade');
expect(result.pageTransitions?.type).toBe('slide_right');
expect(result.componentAnimations?.card.enter?.preset).toBe('scale');
expect(result.componentAnimations?.modal.reducedMotion).toBe('disable');
expect(result.reducedMotion).toBe(true);
expect(result.enabled).toBe(false);
});
it('should leave optional fields undefined when not provided', () => {
const result = MotionConfigSchema.parse({});
expect(result.defaultTransition).toBeUndefined();
expect(result.pageTransitions).toBeUndefined();
expect(result.componentAnimations).toBeUndefined();
});
});
describe('Type exports', () => {
it('should have valid type exports', () => {
const preset: TransitionPreset = 'fade';
const easing: EasingFunction = 'linear';
const transition: TransitionConfig = {};
const trigger: AnimationTrigger = 'on_mount';
const component: ComponentAnimation = { reducedMotion: 'respect' };
const page: PageTransition = { type: 'fade', duration: 300, easing: 'ease_in_out', crossFade: false };
const motion: MotionConfig = { reducedMotion: false, enabled: true };
expect(preset).toBeDefined();
expect(easing).toBeDefined();
expect(transition).toBeDefined();
expect(trigger).toBeDefined();
expect(component).toBeDefined();
expect(page).toBeDefined();
expect(motion).toBeDefined();
});
});
describe('I18n and ARIA integration', () => {
it('should accept I18n label on ComponentAnimationSchema', () => {
const result = ComponentAnimationSchema.parse({
label: { key: 'animations.card_enter', defaultValue: 'Card Enter' },
});
expect(result.label).toEqual({ key: 'animations.card_enter', defaultValue: 'Card Enter' });
});
it('should accept plain string label on ComponentAnimationSchema', () => {
const result = ComponentAnimationSchema.parse({ label: 'Slide In' });
expect(result.label).toBe('Slide In');
});
it('should accept ARIA props on ComponentAnimationSchema', () => {
const result = ComponentAnimationSchema.parse({
ariaLabel: 'Animated card',
ariaDescribedBy: 'card-desc',
role: 'presentation',
});
expect(result.ariaLabel).toBe('Animated card');
expect(result.ariaDescribedBy).toBe('card-desc');
expect(result.role).toBe('presentation');
});
it('should accept I18n label on MotionConfigSchema', () => {
const result = MotionConfigSchema.parse({
label: { key: 'motion.global', defaultValue: 'Global Motion Config' },
});
expect(result.label).toEqual({ key: 'motion.global', defaultValue: 'Global Motion Config' });
});
it('should leave I18n/ARIA fields undefined when not provided', () => {
const result = ComponentAnimationSchema.parse({});
expect(result.label).toBeUndefined();
expect(result.ariaLabel).toBeUndefined();
expect(result.ariaDescribedBy).toBeUndefined();
expect(result.role).toBeUndefined();
});
});
// ============================================================================
// Issue #6: TransitionConfigSchema themeToken support
// ============================================================================
describe('TransitionConfigSchema - themeToken', () => {
it('should accept transition with themeToken reference', () => {
const result = TransitionConfigSchema.parse({
themeToken: 'animation.duration.fast',
});
expect(result.themeToken).toBe('animation.duration.fast');
});
it('should accept transition combining themeToken with explicit values', () => {
const result = TransitionConfigSchema.parse({
preset: 'fade',
duration: 200,
easing: 'ease_in_out',
themeToken: 'animation.timing.ease_in_out',
});
expect(result.themeToken).toBe('animation.timing.ease_in_out');
expect(result.duration).toBe(200);
});
it('should leave themeToken undefined when not provided', () => {
const result = TransitionConfigSchema.parse({ duration: 300 });
expect(result.themeToken).toBeUndefined();
});
});