diff --git a/frontend/app/services/Tooltip.js b/frontend/app/services/Tooltip.js index 6cb8b15d..92b2ad0d 100644 --- a/frontend/app/services/Tooltip.js +++ b/frontend/app/services/Tooltip.js @@ -19,7 +19,7 @@ function ShowTooltip(){ animate(tooltip, { opacity: [0, 1], translateY: [20, 0] - }, {duration: 0.1, ease: 'ease-out'}); + }, {duration: 0.1, ease: 'easeOut'}); showed = true; hided = false; } @@ -33,7 +33,7 @@ function HideTooltip(){ animate(tooltip, { opacity: [1, 0], translateY: [0, 20] - }, {duration: 0.1, ease: 'ease-in'}).finished.then(() => tooltip.style.display = "none") + }, {duration: 0.1, ease: 'easeIn'}).finished.then(() => tooltip.style.display = "none") hided = true; showed = false; } diff --git a/tests/motion-easing.test.js b/tests/motion-easing.test.js new file mode 100644 index 00000000..83f213f2 --- /dev/null +++ b/tests/motion-easing.test.js @@ -0,0 +1,75 @@ +// 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}'` + ); + } + } +});