2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 14:50:56 +00:00

Web component: test exports, reuse span when re-rendering

This commit is contained in:
Vjacheslav Trushkin 2022-05-01 10:59:23 +03:00
parent e80495fcd6
commit 07b3bf3819
3 changed files with 35 additions and 3 deletions

View File

@ -25,7 +25,7 @@
"build:lib": "tsc -b",
"build:dist": "rollup -c rollup.config.js",
"test:jest": "jest --runInBand",
"test:mjs": "echo \"TODO...\"",
"test:mjs": "cross-env NODE_OPTIONS=--experimental-vm-modules node tests/import-test.mjs",
"test": "npm run test:jest && npm run test:mjs"
},
"exports": {

View File

@ -28,7 +28,14 @@ export function renderIcon(parent: Element | ShadowRoot, state: RenderedState) {
// Set element
// Assumes first node is a style node created with updateStyle()
if (parent.childNodes.length > 1) {
parent.removeChild(parent.lastChild);
const lastChild = parent.lastChild as HTMLElement;
if (node.tagName === 'SPAN' && lastChild.tagName === node.tagName) {
// Swap style instead of whole node
lastChild.setAttribute('style', node.getAttribute('style'));
} else {
parent.replaceChild(node, lastChild);
}
} else {
parent.appendChild(node);
}
parent.appendChild(node);
}

View File

@ -0,0 +1,25 @@
// Main file
import { addIcon } from 'iconify-icon';
// Named import from .mjs
import { loadIcons } from '../dist/iconify-icon.mjs';
/**
* Simple assertion function
*/
function test(value, expected, message) {
if (value !== expected) {
console.error(
'❌',
message + `: expected ${value} to equal ${expected}`
);
process.exit(1);
}
console.log('✓', message);
}
/**
* Test named exports
*/
test(typeof addIcon, 'function', 'Testing addIcon named export');
test(typeof loadIcons, 'function', 'Testing loadIcons named export');