Skip to content

Commit ea47e40

Browse files
committed
chore: update tests to work with new sprite logic
1 parent c1e5aeb commit ea47e40

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@
8383
"lint": "biome check ./src",
8484
"lint:fix": "biome check --write ./src",
8585
"format": "biome format --write .",
86-
"test": "node --experimental-test-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
87-
"test:watch": "node --watch --experimental-test-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
88-
"test:updateSnapshots": "node --experimental-test-snapshots --test-update-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
86+
"test": "node --experimental-loader=./tools/svg-loader.mjs --import=./tools/__mocks__/sprite-init.mjs --experimental-test-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
87+
"test:watch": "node --watch --experimental-loader=./tools/svg-loader.mjs --import=./tools/__mocks__/sprite-init.mjs --experimental-test-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
88+
"test:updateSnapshots": "node --experimental-loader=./tools/svg-loader.mjs --import=./tools/__mocks__/sprite-init.mjs --experimental-test-snapshots --test-update-snapshots --require ./tools/test-setup.cjs --test --no-warnings src/**/*.test.{js,mjs}",
8989
"test:ci": "npm test --coverage",
9090
"start": "npm-run-all build:icons dev",
9191
"semantic-release": "semantic-release --ci --debug",

tools/__mocks__/sprite-init.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Initialize the SVG sprite for tests
3+
* This uses the global function set up in test-setup.cjs
4+
*/
5+
import { loaded } from '../../src/lib/js/common/loaders.js'
6+
7+
// Call the global initialization function if it exists
8+
if (typeof global.__initFormeoSprite === 'function') {
9+
global.__initFormeoSprite(loaded)
10+
}

tools/svg-loader.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { readFileSync } from 'node:fs'
2+
import { fileURLToPath } from 'node:url'
3+
4+
/**
5+
* Custom loader to handle SVG imports with ?raw suffix in tests
6+
* This loader is needed because Node.js doesn't natively support importing .svg files
7+
*/
8+
9+
export async function resolve(specifier, context, nextResolve) {
10+
// Let the default resolver handle the path resolution first
11+
return nextResolve(specifier, context)
12+
}
13+
14+
export async function load(url, context, nextLoad) {
15+
// Check if this is an SVG file (with or without ?raw query parameter)
16+
const urlWithoutQuery = url.split('?')[0]
17+
18+
if (urlWithoutQuery.endsWith('.svg')) {
19+
try {
20+
const filePath = fileURLToPath(urlWithoutQuery)
21+
const content = readFileSync(filePath, 'utf-8')
22+
23+
// Return as an ES module with a default export
24+
return {
25+
format: 'module',
26+
source: `export default ${JSON.stringify(content)}`,
27+
shortCircuit: true,
28+
}
29+
} catch {
30+
// If file doesn't exist or can't be read, return empty string
31+
return {
32+
format: 'module',
33+
source: 'export default ""',
34+
shortCircuit: true,
35+
}
36+
}
37+
}
38+
39+
// Let Node.js handle all other file types
40+
return nextLoad(url, context)
41+
}

tools/test-setup.cjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const { snapshot } = require('node:test')
2-
const { basename, join, dirname } = require('node:path')
2+
const { basename, join, dirname, resolve } = require('node:path')
33
const { JSDOM } = require('jsdom')
44
const { ResizeObserver } = require('./__mocks__/ResizeObserver')
5+
const { readFileSync } = require('node:fs')
56

67
const { window } = new JSDOM('<!DOCTYPE html><p>Hello World</p>', { pretendToBeVisual: true })
78

@@ -10,6 +11,20 @@ global.window = window
1011
global.document = window.document
1112
global.navigator = window.navigator
1213

14+
// Prepare the SVG sprite data for sprite-init.mjs (imported via --import flag)
15+
const spritePath = resolve(__dirname, '..', 'src', 'lib', 'icons', 'formeo-sprite.svg')
16+
const spriteContent = readFileSync(spritePath, 'utf-8')
17+
const parser = new window.DOMParser()
18+
const svgDoc = parser.parseFromString(spriteContent, 'image/svg+xml')
19+
const parsedSprite = svgDoc.documentElement
20+
21+
// Expose a global function that sprite-init.mjs will call
22+
global.__initFormeoSprite = loadedObject => {
23+
if (!loadedObject.formeoSprite) {
24+
loadedObject.formeoSprite = parsedSprite
25+
}
26+
}
27+
1328
// jsdom does not provide this method
1429
window.Element.prototype.animate = () => ({
1530
finished: Promise.resolve(),

0 commit comments

Comments
 (0)