squiffy-runtime 6.0.0-beta.3 → 6.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation.d.ts +1 -0
- package/dist/infixExpressions.d.ts +2 -0
- package/dist/infixExpressions.test.d.ts +1 -0
- package/dist/squiffy.runtime.global.js +52 -51
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +2861 -2578
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/textProcessor.d.ts +2 -2
- package/dist/types.d.ts +1 -0
- package/dist/updater.d.ts +2 -2
- package/package.json +4 -4
- package/src/animation.ts +4 -0
- package/src/infixExpressions.test.ts +187 -0
- package/src/infixExpressions.ts +323 -0
- package/src/plugins/animate.ts +39 -5
- package/src/plugins/plugins.test.ts +166 -19
- package/src/squiffy.runtime.test.ts +467 -25
- package/src/squiffy.runtime.ts +52 -22
- package/src/textProcessor.ts +10 -7
- package/src/types.ts +1 -0
- package/src/updater.ts +4 -4
- package/vitest.config.ts +1 -1
|
@@ -1,8 +1,20 @@
|
|
|
1
|
-
import { expect, test, beforeEach, afterEach } from "vitest";
|
|
1
|
+
import { expect, test, beforeEach, afterEach, vi } from "vitest";
|
|
2
2
|
import globalJsdom from "global-jsdom";
|
|
3
3
|
import { init } from "../squiffy.runtime.js";
|
|
4
4
|
import { compile as squiffyCompile } from "squiffy-compiler";
|
|
5
5
|
|
|
6
|
+
// anime.js captures the global document when it's imported, which is before each test
|
|
7
|
+
// sets up jsdom, so it can't run here. Replace it with a stub that completes immediately.
|
|
8
|
+
vi.mock("animejs", () => ({
|
|
9
|
+
animate: (_targets: unknown, params: { onComplete?: () => void }) => {
|
|
10
|
+
setTimeout(() => params.onComplete?.());
|
|
11
|
+
},
|
|
12
|
+
stagger: () => 0,
|
|
13
|
+
text: {
|
|
14
|
+
split: (el: HTMLElement) => ({ chars: [el], words: [el], lines: [el] }),
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
6
18
|
const html = `
|
|
7
19
|
<!DOCTYPE html>
|
|
8
20
|
<html>
|
|
@@ -70,10 +82,39 @@ const findLink = (element: HTMLElement, linkType: string, linkText: string) => {
|
|
|
70
82
|
return Array.from(links).find(link => link.textContent === linkText) as HTMLElement | undefined;
|
|
71
83
|
};
|
|
72
84
|
|
|
85
|
+
// jsdom doesn't run CSS transitions, so fadeReplace would wait forever for a
|
|
86
|
+
// "transitionend" event. Fire one whenever a fade class is added instead.
|
|
87
|
+
const completeFadeTransitions = () => {
|
|
88
|
+
const TransitionEvent = window.Event;
|
|
89
|
+
const observer = new MutationObserver(mutations => {
|
|
90
|
+
for (const mutation of mutations) {
|
|
91
|
+
const el = mutation.target as HTMLElement;
|
|
92
|
+
const oldClasses = (mutation.oldValue || "").split(" ");
|
|
93
|
+
for (const fadeClass of ["fade-out", "fade-in"]) {
|
|
94
|
+
if (el.classList.contains(fadeClass) && !oldClasses.includes(fadeClass)) {
|
|
95
|
+
setTimeout(() => el.dispatchEvent(new TransitionEvent("transitionend")));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
observer.observe(document.body, {
|
|
101
|
+
subtree: true,
|
|
102
|
+
attributes: true,
|
|
103
|
+
attributeFilter: ["class"],
|
|
104
|
+
attributeOldValue: true,
|
|
105
|
+
});
|
|
106
|
+
return () => observer.disconnect();
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const findPassageLink = (element: HTMLElement, passageName: string) =>
|
|
110
|
+
element.querySelector<HTMLElement>(`a.squiffy-link[data-passage="${passageName}"]`) ?? undefined;
|
|
111
|
+
|
|
73
112
|
let cleanup: { (): void };
|
|
113
|
+
let stopCompletingTransitions: { (): void };
|
|
74
114
|
|
|
75
115
|
beforeEach(() => {
|
|
76
116
|
cleanup = globalJsdom(html);
|
|
117
|
+
stopCompletingTransitions = completeFadeTransitions();
|
|
77
118
|
// Mock document.fonts for animations
|
|
78
119
|
Object.defineProperty(document, "fonts", {
|
|
79
120
|
value: { status: "loaded" },
|
|
@@ -83,6 +124,7 @@ beforeEach(() => {
|
|
|
83
124
|
});
|
|
84
125
|
|
|
85
126
|
afterEach(() => {
|
|
127
|
+
stopCompletingTransitions();
|
|
86
128
|
localStorage.clear();
|
|
87
129
|
cleanup();
|
|
88
130
|
});
|
|
@@ -156,9 +198,26 @@ Your score is {{#label "score"}}0{{/label}}.
|
|
|
156
198
|
expect(labelSpan?.textContent).toBe("0");
|
|
157
199
|
});
|
|
158
200
|
|
|
159
|
-
test
|
|
160
|
-
|
|
161
|
-
|
|
201
|
+
test("Replace plugin: replace labeled content", async () => {
|
|
202
|
+
const script = `
|
|
203
|
+
Your score is {{#label "score"}}0{{/label}}.
|
|
204
|
+
|
|
205
|
+
[Increase score]
|
|
206
|
+
|
|
207
|
+
[Increase score]:
|
|
208
|
+
{{#replace "score"}}**10**{{/replace}}
|
|
209
|
+
`;
|
|
210
|
+
|
|
211
|
+
const { squiffyApi, element } = await initScript(script);
|
|
212
|
+
|
|
213
|
+
const link = findPassageLink(element, "Increase score");
|
|
214
|
+
await squiffyApi.clickLink(link!);
|
|
215
|
+
|
|
216
|
+
const labelSpans = element.querySelectorAll(".squiffy-label-score");
|
|
217
|
+
expect(labelSpans.length).toBe(1);
|
|
218
|
+
expect(labelSpans[0].innerHTML).toBe("<strong>10</strong>");
|
|
219
|
+
expect(labelSpans[0].classList.contains("fade-out")).toBe(false);
|
|
220
|
+
expect(labelSpans[0].classList.contains("fade-in")).toBe(false);
|
|
162
221
|
});
|
|
163
222
|
|
|
164
223
|
// ===== RotateSequencePlugin Tests =====
|
|
@@ -262,31 +321,119 @@ Score: {{live "score"}}.
|
|
|
262
321
|
expect(liveSpan?.textContent).toBe("0");
|
|
263
322
|
});
|
|
264
323
|
|
|
265
|
-
test
|
|
266
|
-
|
|
267
|
-
|
|
324
|
+
test("Live plugin: updates when attribute changes", async () => {
|
|
325
|
+
const script = `
|
|
326
|
+
@set score = 0
|
|
327
|
+
|
|
328
|
+
Score: {{live "score"}}.
|
|
329
|
+
|
|
330
|
+
[Increase score]
|
|
331
|
+
|
|
332
|
+
[Increase score]:
|
|
333
|
+
@set score = 10
|
|
334
|
+
`;
|
|
335
|
+
|
|
336
|
+
const { squiffyApi, element } = await initScript(script);
|
|
337
|
+
|
|
338
|
+
const link = findPassageLink(element, "Increase score");
|
|
339
|
+
await squiffyApi.clickLink(link!);
|
|
340
|
+
|
|
341
|
+
// Live updates run in the background after the attribute is set, so wait for the fade to finish
|
|
342
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
343
|
+
await vi.waitFor(() => {
|
|
344
|
+
expect(liveSpan?.textContent).toBe("10");
|
|
345
|
+
expect(liveSpan?.classList.contains("fade-in")).toBe(false);
|
|
346
|
+
});
|
|
268
347
|
});
|
|
269
348
|
|
|
270
|
-
test
|
|
271
|
-
|
|
272
|
-
|
|
349
|
+
test("Live plugin: embed section content", async () => {
|
|
350
|
+
const script = `
|
|
351
|
+
[[start]]:
|
|
352
|
+
@set mood = happy
|
|
353
|
+
|
|
354
|
+
Status: {{live "mood" section="status"}}
|
|
355
|
+
|
|
356
|
+
[Cheer up]
|
|
357
|
+
|
|
358
|
+
[Cheer up]:
|
|
359
|
+
@set mood = ecstatic
|
|
360
|
+
|
|
361
|
+
[[status]]:
|
|
362
|
+
You are {{mood}}.
|
|
363
|
+
`;
|
|
364
|
+
|
|
365
|
+
const { squiffyApi, element } = await initScript(script);
|
|
366
|
+
|
|
367
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
368
|
+
expect(liveSpan?.getAttribute("data-section")).toBe("status");
|
|
369
|
+
expect(liveSpan?.textContent).toBe("You are happy.");
|
|
370
|
+
|
|
371
|
+
const link = findPassageLink(element, "Cheer up");
|
|
372
|
+
await squiffyApi.clickLink(link!);
|
|
373
|
+
|
|
374
|
+
await vi.waitFor(() => expect(liveSpan?.textContent).toBe("You are ecstatic."));
|
|
273
375
|
});
|
|
274
376
|
|
|
275
|
-
test
|
|
276
|
-
|
|
277
|
-
|
|
377
|
+
test("Live plugin: embed passage content", async () => {
|
|
378
|
+
const script = `
|
|
379
|
+
@set mood = happy
|
|
380
|
+
|
|
381
|
+
Status: {{live "mood" passage="status"}}
|
|
382
|
+
|
|
383
|
+
[Cheer up]
|
|
384
|
+
|
|
385
|
+
[Cheer up]:
|
|
386
|
+
@set mood = ecstatic
|
|
387
|
+
|
|
388
|
+
[status]:
|
|
389
|
+
You are {{mood}}.
|
|
390
|
+
`;
|
|
391
|
+
|
|
392
|
+
const { squiffyApi, element } = await initScript(script);
|
|
393
|
+
|
|
394
|
+
const liveSpan = element.querySelector(".squiffy-live");
|
|
395
|
+
expect(liveSpan?.getAttribute("data-passage")).toBe("status");
|
|
396
|
+
expect(liveSpan?.textContent).toBe("You are happy.");
|
|
397
|
+
|
|
398
|
+
const link = findPassageLink(element, "Cheer up");
|
|
399
|
+
await squiffyApi.clickLink(link!);
|
|
400
|
+
|
|
401
|
+
await vi.waitFor(() => expect(liveSpan?.textContent).toBe("You are ecstatic."));
|
|
278
402
|
});
|
|
279
403
|
|
|
280
404
|
// ===== AnimatePlugin Tests =====
|
|
281
405
|
|
|
282
|
-
test
|
|
283
|
-
|
|
284
|
-
|
|
406
|
+
test("Animate plugin: creates animated span", async () => {
|
|
407
|
+
const script = `
|
|
408
|
+
{{#animate "typewriter"}}Hello **world**{{/animate}}
|
|
409
|
+
`;
|
|
410
|
+
|
|
411
|
+
const { element } = await initScript(script);
|
|
412
|
+
|
|
413
|
+
// Once the animation completes, the original content is restored
|
|
414
|
+
const animateSpan = element.querySelector("span[data-name]");
|
|
415
|
+
expect(animateSpan).not.toBeNull();
|
|
416
|
+
expect(animateSpan?.innerHTML).toBe("Hello <strong>world</strong>");
|
|
417
|
+
expect(animateSpan?.classList.contains("squiffy-animate")).toBe(false);
|
|
285
418
|
});
|
|
286
419
|
|
|
287
|
-
test
|
|
288
|
-
|
|
289
|
-
|
|
420
|
+
test("Animate plugin: sets data attributes", async () => {
|
|
421
|
+
const script = `
|
|
422
|
+
{{#animate "typewriter" trigger="link" interval=50}}[Click me]{{/animate}}
|
|
423
|
+
|
|
424
|
+
[Click me]:
|
|
425
|
+
You clicked!
|
|
426
|
+
`;
|
|
427
|
+
|
|
428
|
+
const { element } = await initScript(script);
|
|
429
|
+
|
|
430
|
+
const animateSpan = element.querySelector<HTMLElement>(".squiffy-animate");
|
|
431
|
+
expect(animateSpan).not.toBeNull();
|
|
432
|
+
expect(animateSpan?.tagName).toBe("SPAN");
|
|
433
|
+
expect(animateSpan?.getAttribute("data-name")).toBe('"typewriter"');
|
|
434
|
+
expect(JSON.parse(animateSpan!.getAttribute("data-content")!)).toContain('data-passage="Click me"');
|
|
435
|
+
expect(animateSpan?.getAttribute("data-trigger")).toBe('"link"');
|
|
436
|
+
expect(animateSpan?.getAttribute("data-interval")).toBe("50");
|
|
290
437
|
});
|
|
291
438
|
|
|
292
439
|
test("Animate plugin: trigger on link", async () => {
|