76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
// Tests for the motion error of Syndria98/dragonroll#34: the tooltip animated itself with
|
|
// `ease: 'ease-in'`, and motion throws `Invalid easing type 'ease-in'` on that. Motion's easing
|
|
// options are its own names (`easeIn`, `easeOut`, ...), not the CSS keywords, so every string ease
|
|
// option in the frontend has to be one of the names motion accepts.
|
|
//
|
|
// `frontend/app/services/Tooltip.js` imports `vue` and `motion` and the repository root installs no
|
|
// dependencies, so the checks read the sources the way `tests/map-viewer.test.js` reads them.
|
|
//
|
|
// Run with `npm test` from the repository root, or `node --test tests/`.
|
|
|
|
const { test } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
// The strings motion accepts for `ease` (https://motion.dev/troubleshooting/invalid-easing-type).
|
|
const VALID_EASINGS = [
|
|
"linear",
|
|
"easeIn",
|
|
"easeInOut",
|
|
"easeOut",
|
|
"circIn",
|
|
"circInOut",
|
|
"circOut",
|
|
"backIn",
|
|
"backInOut",
|
|
"backOut",
|
|
"anticipate",
|
|
];
|
|
|
|
function frontendSources(dir = path.join(__dirname, "..", "frontend", "app")) {
|
|
const found = [];
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) found.push(...frontendSources(full));
|
|
else if (/\.(js|ts|vue)$/.test(entry.name)) found.push({ file: full, source: fs.readFileSync(full, "utf8") });
|
|
}
|
|
return found;
|
|
}
|
|
|
|
// An ease option is the `ease` key of a JS object (motion's options), so it is always quoted. CSS
|
|
// `transition: ... ease` declarations have no colon after `ease` and never match.
|
|
function easeOptions(source) {
|
|
return [...source.matchAll(/\bease:\s*(['"])([^'"]*)\1/g)].map((match) => match[2]);
|
|
}
|
|
|
|
function readTooltip() {
|
|
return fs.readFileSync(
|
|
path.join(__dirname, "..", "frontend", "app", "services", "Tooltip.js"),
|
|
"utf8"
|
|
);
|
|
}
|
|
|
|
test("the tooltip keeps animating itself in and out", () => {
|
|
const ease = easeOptions(readTooltip());
|
|
// Guards the checks below against silently matching nothing.
|
|
assert.equal(ease.length, 2, `expected the show and the hide ease option, found ${ease.length}`);
|
|
});
|
|
|
|
test("the tooltip options use easing names motion accepts", () => {
|
|
for (const value of easeOptions(readTooltip())) {
|
|
assert.ok(VALID_EASINGS.includes(value), `invalid easing type '${value}'`);
|
|
}
|
|
});
|
|
|
|
test("no frontend animation uses a CSS keyword as motion easing", () => {
|
|
for (const { file, source } of frontendSources()) {
|
|
for (const value of easeOptions(source)) {
|
|
assert.ok(
|
|
VALID_EASINGS.includes(value),
|
|
`${path.relative(path.join(__dirname, ".."), file)} uses invalid easing type '${value}'`
|
|
);
|
|
}
|
|
}
|
|
});
|