-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathupdate-keyframes.test.ts
More file actions
209 lines (187 loc) · 5.49 KB
/
update-keyframes.test.ts
File metadata and controls
209 lines (187 loc) · 5.49 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
import {expect, test} from 'bun:test';
import {
updateEffectKeyframesAst,
updateSequenceKeyframes,
} from '../codemods/update-keyframes/update-keyframes';
import {lineColumnToNodePath} from './test-utils';
const sequenceInput = `import React from 'react';
import {AbsoluteFill, interpolate, useCurrentFrame} from 'remotion';
export const Example: React.FC = () => {
\tconst frame = useCurrentFrame();
\treturn (
\t\t<AbsoluteFill>
\t\t\t<div style={{scale: interpolate(frame, [0, 100], [2, 4])}} />
\t\t\t<div style={{opacity: 0.5}} />
\t\t</AbsoluteFill>
\t);
};
`;
const colorInput = `import React from 'react';
import {Solid, interpolateColors, useCurrentFrame} from 'remotion';
export const Example: React.FC = () => {
\tconst frame = useCurrentFrame();
\treturn (
\t\t<Solid color={interpolateColors(frame, [0, 100], ['red', 'blue'])} width={100} height={100} />
\t);
};
`;
const effectInput = `import {tint} from '@remotion/effects/tint';
import {HtmlInCanvas} from '@remotion/html-in-canvas';
import {interpolate, useCurrentFrame} from 'remotion';
export const Comp = () => {
\tconst frame = useCurrentFrame();
\treturn (
\t\t<HtmlInCanvas
\t\t\teffects={[tint({amount: interpolate(frame, [0, 50, 100], [0.2, 0.5, 0.8])})]}
\t\t>
\t\t\thi
\t\t</HtmlInCanvas>
\t);
};
`;
const getLine = (input: string, needle: string): number => {
const lineIndex = input
.split('\n')
.findIndex((line) => line.includes(needle));
if (lineIndex === -1) {
throw new Error(`Could not find line containing ${needle}`);
}
return lineIndex + 1;
};
test('updateSequenceKeyframes adds a keyframe to an existing interpolation', async () => {
const {output, oldValueStrings} = await updateSequenceKeyframes({
input: sequenceInput,
nodePath: lineColumnToNodePath(
sequenceInput,
getLine(sequenceInput, 'scale'),
),
updates: [
{
key: 'style.scale',
operation: {type: 'add', frame: 50, value: 3},
},
],
});
expect(oldValueStrings).toEqual(['interpolate(frame, [0, 100], [2, 4])']);
expect(output).toContain(
'scale: interpolate(frame, [0, 50, 100], [2, 3, 4])',
);
});
test('updateSequenceKeyframes converts a static value to an interpolation', async () => {
const {output, oldValueStrings} = await updateSequenceKeyframes({
input: sequenceInput,
nodePath: lineColumnToNodePath(
sequenceInput,
getLine(sequenceInput, 'opacity'),
),
updates: [
{
key: 'style.opacity',
operation: {type: 'add', frame: 25, value: 0.75},
},
],
});
expect(oldValueStrings).toEqual(['0.5']);
expect(output).toContain('opacity: interpolate(frame, [0, 25], [0.5, 0.75])');
});
test('updateSequenceKeyframes adds a keyframe to an existing color interpolation', async () => {
const {output, oldValueStrings} = await updateSequenceKeyframes({
input: colorInput,
nodePath: lineColumnToNodePath(colorInput, getLine(colorInput, '<Solid')),
updates: [
{
key: 'color',
operation: {type: 'add', frame: 50, value: '#00ff00'},
},
],
});
expect(oldValueStrings).toEqual([
"interpolateColors(frame, [0, 100], ['red', 'blue'])",
]);
expect(output).toContain(
"color={interpolateColors(frame, [0, 50, 100], ['red', '#00ff00', 'blue'])}",
);
});
test('updateSequenceKeyframes converts a static string value to a color interpolation', async () => {
const input = colorInput.replace(
"interpolateColors(frame, [0, 100], ['red', 'blue'])",
"'red'",
);
const {output, oldValueStrings} = await updateSequenceKeyframes({
input,
nodePath: lineColumnToNodePath(input, getLine(input, '<Solid')),
updates: [
{
key: 'color',
operation: {type: 'add', frame: 50, value: 'blue'},
},
],
});
expect(oldValueStrings).toEqual(["'red'"]);
expect(output).toContain(
"color={interpolateColors(frame, [0, 50], ['red', 'blue'])}",
);
});
test('updateSequenceKeyframes collapses to a static value when one keyframe remains', async () => {
const {output, oldValueStrings} = await updateSequenceKeyframes({
input: sequenceInput,
nodePath: lineColumnToNodePath(
sequenceInput,
getLine(sequenceInput, 'scale'),
),
updates: [
{
key: 'style.scale',
operation: {type: 'remove', frame: 0},
},
],
});
expect(oldValueStrings).toEqual(['interpolate(frame, [0, 100], [2, 4])']);
expect(output).toContain('style={{scale: 4}}');
expect(output).not.toContain('scale: interpolate');
});
test('updateEffectKeyframes removes a keyframe from an effect prop interpolation', () => {
const {serialized, oldValueStrings, effectCallee} = updateEffectKeyframesAst({
input: effectInput,
sequenceNodePath: lineColumnToNodePath(
effectInput,
getLine(effectInput, '<HtmlInCanvas'),
),
effectIndex: 0,
updates: [
{
key: 'amount',
operation: {type: 'remove', frame: 50},
},
],
});
expect(effectCallee).toBe('tint');
expect(oldValueStrings).toEqual([
'interpolate(frame, [0, 50, 100], [0.2, 0.5, 0.8])',
]);
expect(serialized).toContain(
'amount: interpolate(frame, [0, 100], [0.2, 0.8])',
);
});
test('updateEffectKeyframes collapses an effect prop to a static value', () => {
const input = effectInput.replace(
'interpolate(frame, [0, 50, 100], [0.2, 0.5, 0.8])',
'interpolate(frame, [0, 100], [0.2, 0.8])',
);
const {serialized} = updateEffectKeyframesAst({
input,
sequenceNodePath: lineColumnToNodePath(
input,
getLine(input, '<HtmlInCanvas'),
),
effectIndex: 0,
updates: [
{
key: 'amount',
operation: {type: 'remove', frame: 100},
},
],
});
expect(serialized).toContain('amount: 0.2');
expect(serialized).not.toContain('amount: interpolate');
});