-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.test.mjs
More file actions
258 lines (227 loc) · 9.89 KB
/
Copy pathplugin.test.mjs
File metadata and controls
258 lines (227 loc) · 9.89 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
import assert from 'node:assert/strict';
import { mkdtemp, readFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, it } from 'node:test';
import postcss from 'postcss';
import plugin from '../index.js';
const FIXTURE = `
@keyframes fade-up {
from { opacity: 0; transform: translateY(40px); }
to { opacity: 1; transform: translateY(0); }
}
.card {
animation: fade-up 1s linear both;
animation-timeline: view();
animation-range: entry 0% entry 100%;
}
`;
/**
* Run the plugin over some CSS.
*
* @param {string} css Input stylesheet.
* @param {object} [options] Plugin options.
* @returns {Promise<import('postcss').Result>} PostCSS result.
*/
function run (css, options) {
return postcss([plugin(options)]).process(css, { from: undefined });
}
/**
* Pull the manifest out of a PostCSS result's messages.
*
* @param {import('postcss').Result} result PostCSS result.
* @returns {any} Manifest document.
*/
function manifestOf (result) {
const message = result.messages.find((m) => m.type === 'scroll-anim-fallback-manifest');
assert.ok(message, 'expected a manifest message');
return message.manifest;
}
describe('@supports wrapping', () => {
it('wraps scroll-driven rules in a native feature query', async () => {
const { css } = await run(FIXTURE);
assert.match(css, /@supports \(animation-timeline: scroll\(\)\) \{/);
assert.match(css, /@supports \(animation-timeline: scroll\(\)\) \{\s*\.card \{/);
});
it('leaves rules without scroll-driven properties alone', async () => {
const { css } = await run('.plain { color: red; }');
assert.equal(css.trim(), '.plain { color: red; }');
});
it('detects every scroll-driven property family', async () => {
for (const decl of [
'scroll-timeline: --s block',
'scroll-timeline-name: --s',
'scroll-timeline-axis: inline',
'view-timeline: --v y',
'view-timeline-name: --v',
'view-timeline-inset: 10%',
'animation-range-start: entry 20%',
'animation-range-end: exit 80%',
'timeline-scope: --v'
]) {
const { css } = await run(`.x { ${decl}; }`);
assert.match(css, /@supports \(animation-timeline: scroll\(\)\)/, `missed: ${decl}`);
}
});
it('can be disabled with supportsWrap: false', async () => {
const { css } = await run(FIXTURE, { supportsWrap: false });
assert.doesNotMatch(css, /@supports \(animation-timeline: scroll\(\)\)/);
assert.match(css, /@supports not \(animation-timeline: scroll\(\)\)/);
});
it('keeps the unguarded rule with preserveOriginal: true', async () => {
const { css } = await run(FIXTURE, { preserveOriginal: true });
const occurrences = css.split('.card {').length - 1;
assert.ok(occurrences >= 3, `expected original + guarded + fallback copies, saw ${occurrences}`);
});
});
describe('idempotency', () => {
it('does not double-wrap on a second pass', async () => {
const first = await run(FIXTURE);
const second = await run(first.css);
const count = (css) => (css.match(/@supports \(animation-timeline: scroll\(\)\)/g) || []).length;
assert.equal(count(first.css), 1);
assert.equal(count(second.css), 1);
});
it('reuses a single fallback layer across passes', async () => {
const first = await run(FIXTURE);
const second = await run(first.css);
const count = (second.css.match(/@supports not \(animation-timeline: scroll\(\)\)/g) || []).length;
assert.equal(count, 1);
});
it('leaves a hand-written @supports guard untouched', async () => {
const input = `
@keyframes fade-up { from { opacity: 0; } to { opacity: 1; } }
@supports (animation-timeline: view()) {
.card { animation: fade-up 1s linear both; animation-timeline: view(); }
}
`;
const { css } = await run(input);
assert.equal((css.match(/@supports \(animation-timeline: view\(\)\)/g) || []).length, 1);
assert.doesNotMatch(css, /@supports \(animation-timeline: scroll\(\)\)/);
});
});
describe('manifest extraction', () => {
it('records selector, timeline, range and animation metadata', async () => {
const manifest = manifestOf(await run(FIXTURE));
assert.equal(manifest.version, 1);
assert.equal(manifest.classPrefix, 'saf');
assert.equal(manifest.timelines.length, 1);
const [entry] = manifest.timelines;
assert.equal(entry.selector, '.card');
assert.equal(entry.timeline.kind, 'view');
assert.equal(entry.timeline.axis, 'block');
assert.equal(entry.animationName, 'fade-up');
assert.equal(entry.keyframes, 'fade-up');
assert.equal(entry.duration, 1000);
assert.deepEqual(entry.range.start, { name: 'entry', percent: 0 });
assert.deepEqual(entry.range.end, { name: 'entry', percent: 100 });
assert.equal(entry.progressProperty, '--saf-progress');
});
it('parses scroll() timelines including axis and scroller', async () => {
const manifest = manifestOf(await run('.bar { animation: grow 1s; animation-timeline: scroll(root inline); }'));
assert.deepEqual(manifest.timelines[0].timeline, {
kind: 'scroll', name: null, axis: 'inline', scroller: 'root'
});
});
it('parses named timelines', async () => {
const manifest = manifestOf(await run('.bar { animation-timeline: --hero; }'));
assert.equal(manifest.timelines[0].timeline.kind, 'named');
assert.equal(manifest.timelines[0].timeline.name, '--hero');
});
it('defaults an omitted animation-range to the full cover range', async () => {
const manifest = manifestOf(await run('.bar { animation: grow 1s; animation-timeline: view(); }'));
assert.deepEqual(manifest.timelines[0].range, {
start: { name: 'cover', percent: 0 },
end: { name: 'cover', percent: 100 }
});
});
it('expands a single-sided animation-range to both boundaries', async () => {
const manifest = manifestOf(await run('.bar { animation-timeline: view(); animation-range: contain; }'));
assert.deepEqual(manifest.timelines[0].range, {
start: { name: 'contain', percent: 0 },
end: { name: 'contain', percent: 100 }
});
});
it('reads animation-range-start/end longhands', async () => {
const manifest = manifestOf(await run(
'.bar { animation-timeline: view(); animation-range-start: entry 25%; animation-range-end: exit 75%; }'
));
assert.deepEqual(manifest.timelines[0].range, {
start: { name: 'entry', percent: 25 },
end: { name: 'exit', percent: 75 }
});
});
it('assigns stable, unique ids in document order', async () => {
const manifest = manifestOf(await run(
'.a { animation-timeline: view(); } .b { animation-timeline: scroll(); }'
));
assert.deepEqual(manifest.timelines.map((t) => t.id), ['saf-0', 'saf-1']);
});
it('writes the manifest to disk when a path is given', async () => {
const dir = await mkdtemp(join(tmpdir(), 'saf-'));
const path = join(dir, 'nested', 'manifest.json');
await run(FIXTURE, { manifest: path });
const written = JSON.parse(await readFile(path, 'utf8'));
assert.equal(written.timelines[0].selector, '.card');
});
});
describe('keyframe derivation', () => {
it('normalises from/to and percentage selectors to 0–1 offsets', async () => {
const manifest = manifestOf(await run(`
@keyframes pan { from { opacity: 0; } 40% { opacity: 0.5; } to { opacity: 1; } }
.bar { animation: pan 1s; animation-timeline: view(); }
`));
assert.deepEqual(manifest.timelines[0].steps.map((s) => s.offset), [0, 0.4, 1]);
});
it('merges comma-separated keyframe selectors', async () => {
const manifest = manifestOf(await run(`
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }
.bar { animation: pulse 1s; animation-timeline: view(); }
`));
const offsets = manifest.timelines[0].steps.map((s) => s.offset);
assert.deepEqual(offsets, [0, 0.5, 1]);
});
it('emits a two-state transition fallback for entry-style timelines', async () => {
const { css } = await run(FIXTURE);
const fallback = css.slice(css.indexOf('@supports not'));
assert.match(fallback, /\.card \{[^}]*opacity: 0/);
assert.match(fallback, /\.card\.saf-in-view \{[^}]*opacity: 1/);
assert.match(fallback, /transition: opacity var\(--saf-duration, 600ms\)/);
assert.match(fallback, /@media \(prefers-reduced-motion: reduce\)/);
});
it('picks the progress driver for continuous ranges', async () => {
const manifest = manifestOf(await run(`
@keyframes pan { from { opacity: 0; } to { opacity: 1; } }
.bar { animation: pan 1s; animation-timeline: view(); animation-range: cover; }
`));
assert.equal(manifest.timelines[0].mode, 'progress');
});
it('emits no fallback CSS when the animation has no keyframes', async () => {
const { css } = await run('.bar { animation: missing 1s; animation-timeline: view(); }');
assert.doesNotMatch(css, /@supports not/);
});
});
describe('options', () => {
it('fallbackMode: "progress" forces the progress driver everywhere', async () => {
const result = await run(FIXTURE, { fallbackMode: 'progress' });
const manifest = manifestOf(result);
assert.equal(manifest.timelines[0].mode, 'progress');
assert.match(result.css, /--saf-progress: 0/);
assert.doesNotMatch(result.css, /saf-in-view/);
});
it('fallbackMode: "none" emits no fallback layer but still builds a manifest', async () => {
const result = await run(FIXTURE, { fallbackMode: 'none' });
assert.doesNotMatch(result.css, /@supports not/);
assert.equal(manifestOf(result).timelines.length, 1);
assert.equal(manifestOf(result).timelines[0].mode, 'none');
});
it('classPrefix renames classes, custom properties and ids', async () => {
const result = await run(FIXTURE, { classPrefix: 'zz' });
const [entry] = manifestOf(result).timelines;
assert.equal(entry.id, 'zz-0');
assert.equal(entry.classes.inView, 'zz-in-view');
assert.equal(entry.progressProperty, '--zz-progress');
assert.match(result.css, /\.card\.zz-in-view/);
assert.doesNotMatch(result.css, /saf-/);
});
});