2022-04-29 20:19:22 +00:00
|
|
|
import {
|
|
|
|
cleanupGlobals,
|
|
|
|
expectedBlock,
|
|
|
|
expectedInline,
|
|
|
|
setupDOM,
|
2022-05-02 06:28:48 +00:00
|
|
|
nextTick,
|
2022-10-08 10:38:53 +00:00
|
|
|
styleOpeningTag,
|
2022-09-07 18:25:59 +00:00
|
|
|
} from '../src/tests/helpers';
|
2022-04-29 20:19:22 +00:00
|
|
|
import { defineIconifyIcon, IconifyIconHTMLElement } from '../src/component';
|
|
|
|
import type { IconState } from '../src/state';
|
|
|
|
|
|
|
|
export declare interface DebugIconifyIconHTMLElement
|
|
|
|
extends IconifyIconHTMLElement {
|
|
|
|
// Internal stuff, used for debugging
|
|
|
|
_shadowRoot: ShadowRoot;
|
|
|
|
_state: IconState;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Testing icon component', () => {
|
2022-05-02 06:28:48 +00:00
|
|
|
afterEach(async () => {
|
|
|
|
await nextTick();
|
|
|
|
cleanupGlobals();
|
|
|
|
});
|
2022-04-29 20:19:22 +00:00
|
|
|
|
2022-05-01 11:01:54 +00:00
|
|
|
it('Registering component', () => {
|
|
|
|
// Setup DOM
|
|
|
|
const doc = setupDOM('').window.document;
|
|
|
|
|
|
|
|
// Make sure component does not exist and registry is available
|
|
|
|
expect(window.customElements).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeUndefined();
|
|
|
|
|
|
|
|
// Define component
|
|
|
|
const IconifyIcon = defineIconifyIcon();
|
|
|
|
expect(IconifyIcon).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeDefined();
|
|
|
|
|
|
|
|
// Create element
|
|
|
|
const node = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
|
|
|
expect(node instanceof IconifyIcon).toBe(true);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('loading');
|
2022-05-01 11:01:54 +00:00
|
|
|
|
|
|
|
// Define component again (should return previous class)
|
|
|
|
const IconifyIcon2 = defineIconifyIcon();
|
|
|
|
expect(IconifyIcon2).toBe(IconifyIcon);
|
|
|
|
|
|
|
|
// Create another element
|
|
|
|
const node2 = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
|
|
|
expect(node2 instanceof IconifyIcon).toBe(true);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node2.status).toBe('loading');
|
2022-05-01 11:01:54 +00:00
|
|
|
});
|
|
|
|
|
2022-05-02 06:28:48 +00:00
|
|
|
it('Creating component instance, changing properties', async () => {
|
2022-04-29 20:19:22 +00:00
|
|
|
// Setup DOM
|
|
|
|
const doc = setupDOM('').window.document;
|
|
|
|
|
|
|
|
// Make sure component does not exist and registry is available
|
|
|
|
expect(window.customElements).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeUndefined();
|
|
|
|
|
|
|
|
// Define component
|
2022-04-30 15:24:44 +00:00
|
|
|
const IconifyIcon = defineIconifyIcon();
|
|
|
|
expect(IconifyIcon).toBeDefined();
|
2022-04-29 20:19:22 +00:00
|
|
|
expect(window.customElements.get('iconify-icon')).toBeDefined();
|
|
|
|
|
|
|
|
// Create element
|
|
|
|
const node = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
|
|
|
|
|
|
|
// Should be empty
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('loading');
|
2022-04-29 20:19:22 +00:00
|
|
|
|
2022-04-30 15:24:44 +00:00
|
|
|
// Check for dynamically added methods
|
|
|
|
expect(typeof node.loadIcon).toBe('function');
|
|
|
|
expect(typeof IconifyIcon.loadIcon).toBe('function');
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
// Set icon
|
|
|
|
node.icon = {
|
|
|
|
body: '<g />',
|
|
|
|
};
|
|
|
|
expect(node.icon).toEqual({
|
|
|
|
body: '<g />',
|
|
|
|
});
|
|
|
|
expect(node.getAttribute('icon')).toBe(
|
|
|
|
JSON.stringify({
|
|
|
|
body: '<g />',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-05-02 06:28:48 +00:00
|
|
|
// Should still be empty: waiting for next tick
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>`
|
2022-05-02 06:28:48 +00:00
|
|
|
);
|
|
|
|
expect(node.status).toBe('loading');
|
|
|
|
await nextTick();
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
// Should render SVG
|
|
|
|
const blankSVG =
|
2022-04-30 20:12:34 +00:00
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"><g></g></svg>';
|
2022-04-29 20:19:22 +00:00
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>${blankSVG}`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
|
|
|
|
// Check inline attribute
|
|
|
|
expect(node.inline).toBe(false);
|
|
|
|
expect(node.getAttribute('inline')).toBe(null);
|
|
|
|
|
|
|
|
// Change inline
|
|
|
|
node.inline = true;
|
|
|
|
expect(node.inline).toBe(true);
|
|
|
|
expect(node.getAttribute('inline')).toBe('true');
|
|
|
|
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedInline}</style>${blankSVG}`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
});
|
|
|
|
|
2022-05-01 11:01:54 +00:00
|
|
|
it('Testing changes to inline', () => {
|
|
|
|
// Setup DOM
|
|
|
|
const doc = setupDOM('').window.document;
|
|
|
|
|
|
|
|
// Make sure component does not exist and registry is available
|
|
|
|
expect(window.customElements).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeUndefined();
|
|
|
|
|
|
|
|
// Define component
|
|
|
|
const IconifyIcon = defineIconifyIcon();
|
|
|
|
expect(IconifyIcon).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeDefined();
|
|
|
|
|
|
|
|
// Create element
|
|
|
|
const node = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('loading');
|
2022-05-01 11:01:54 +00:00
|
|
|
|
|
|
|
// Should be empty with block style
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>`
|
2022-05-01 11:01:54 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Check inline
|
|
|
|
expect(node.inline).toBe(false);
|
|
|
|
expect(node.hasAttribute('inline')).toBe(false);
|
|
|
|
expect(node.getAttribute('inline')).toBeFalsy();
|
|
|
|
|
|
|
|
// Change to inline via property
|
|
|
|
node.inline = true;
|
|
|
|
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedInline}</style>`
|
2022-05-01 11:01:54 +00:00
|
|
|
);
|
|
|
|
expect(node.inline).toBe(true);
|
|
|
|
expect(node.hasAttribute('inline')).toBe(true);
|
|
|
|
expect(node.getAttribute('inline')).toBeTruthy();
|
|
|
|
|
|
|
|
// Change to block by removing attribute
|
|
|
|
node.removeAttribute('inline');
|
|
|
|
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>`
|
2022-05-01 11:01:54 +00:00
|
|
|
);
|
|
|
|
expect(node.inline).toBe(false);
|
|
|
|
expect(node.hasAttribute('inline')).toBe(false);
|
|
|
|
expect(node.getAttribute('inline')).toBeFalsy();
|
|
|
|
|
|
|
|
// Change to inline by setting attribute
|
|
|
|
node.setAttribute('inline', 'inline');
|
|
|
|
|
|
|
|
expect(node._shadowRoot.innerHTML).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedInline}</style>`
|
2022-05-01 11:01:54 +00:00
|
|
|
);
|
|
|
|
expect(node.inline).toBe(true);
|
|
|
|
expect(node.hasAttribute('inline')).toBe(true);
|
|
|
|
expect(node.getAttribute('inline')).toBeTruthy();
|
2022-05-02 06:28:48 +00:00
|
|
|
|
|
|
|
// No icon data, so still loading
|
|
|
|
expect(node.status).toBe('loading');
|
2022-05-01 11:01:54 +00:00
|
|
|
});
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
it('Restarting animation', async () => {
|
|
|
|
// Setup DOM
|
|
|
|
const doc = setupDOM('').window.document;
|
|
|
|
|
|
|
|
// Make sure component does not exist and registry is available
|
|
|
|
expect(window.customElements).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeUndefined();
|
|
|
|
|
|
|
|
// Define component
|
|
|
|
expect(defineIconifyIcon()).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeDefined();
|
|
|
|
|
|
|
|
// Create element
|
|
|
|
const node = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('loading');
|
2022-04-29 20:19:22 +00:00
|
|
|
|
|
|
|
// Set icon
|
|
|
|
const body =
|
|
|
|
'<rect width="10" height="10"><animate attributeName="width" values="10;5;10" dur="10s" repeatCount="indefinite" /></rect>';
|
|
|
|
node.icon = {
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
expect(node.icon).toEqual({
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
expect(node.getAttribute('icon')).toBe(
|
|
|
|
JSON.stringify({
|
|
|
|
body,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-05-02 06:28:48 +00:00
|
|
|
// Wait to render
|
|
|
|
await nextTick();
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
// Should render SPAN, with comment
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
const renderedIconWithComment =
|
2022-04-30 20:12:34 +00:00
|
|
|
"<span style=\"--svg: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16'%3E%3Crect width='10' height='10'%3E%3Canimate attributeName='width' values='10;5;10' dur='10s' repeatCount='indefinite' /%3E%3C/rect%3E%3C!-- --%3E%3C/svg%3E"); width: 1em; height: 1em; background-color: transparent; background-repeat: no-repeat; background-size: 100% 100%;\"></span>";
|
2022-04-29 20:19:22 +00:00
|
|
|
const html1 = node._shadowRoot.innerHTML;
|
|
|
|
expect(html1.replace(/-- [0-9]+ --/, '-- --')).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>${renderedIconWithComment}`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Restart animation, test icon again
|
|
|
|
node.restartAnimation();
|
|
|
|
|
|
|
|
const html2 = node._shadowRoot.innerHTML;
|
|
|
|
expect(html2).not.toBe(html1);
|
|
|
|
expect(html2.replace(/-- [0-9]+ --/, '-- --')).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>${renderedIconWithComment}`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
|
|
|
|
// Small delay to make sure timer is increased to get new number
|
|
|
|
await new Promise((fulfill) => {
|
|
|
|
setTimeout(fulfill, 10);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Restart animation again, test icon again
|
|
|
|
node.restartAnimation();
|
|
|
|
|
|
|
|
const html3 = node._shadowRoot.innerHTML;
|
|
|
|
expect(html3.replace(/-- [0-9]+ --/, '-- --')).toBe(
|
2022-10-08 10:38:53 +00:00
|
|
|
`${styleOpeningTag}${expectedBlock}</style>${renderedIconWithComment}`
|
2022-04-29 20:19:22 +00:00
|
|
|
);
|
|
|
|
expect(html3).not.toBe(html1);
|
|
|
|
expect(html3).not.toBe(html2);
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
});
|
|
|
|
|
2022-05-02 06:28:48 +00:00
|
|
|
it('Restarting animation for SVG', async () => {
|
2022-04-29 20:19:22 +00:00
|
|
|
// Setup DOM
|
|
|
|
const doc = setupDOM('').window.document;
|
|
|
|
|
|
|
|
// Make sure component does not exist and registry is available
|
|
|
|
expect(window.customElements).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeUndefined();
|
|
|
|
|
|
|
|
// Define component
|
|
|
|
expect(defineIconifyIcon()).toBeDefined();
|
|
|
|
expect(window.customElements.get('iconify-icon')).toBeDefined();
|
|
|
|
|
|
|
|
// Create element
|
|
|
|
const node = document.createElement(
|
|
|
|
'iconify-icon'
|
|
|
|
) as DebugIconifyIconHTMLElement;
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('loading');
|
2022-04-29 20:19:22 +00:00
|
|
|
|
2022-05-03 07:06:02 +00:00
|
|
|
// Set mode by changing property
|
|
|
|
node.mode = 'svg';
|
|
|
|
expect(node.getAttribute('mode')).toBe('svg');
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
// Set icon
|
|
|
|
const body =
|
|
|
|
'<rect width="10" height="10"><animate attributeName="width" values="10;5;10" dur="10s" repeatCount="indefinite" /></rect>';
|
|
|
|
node.icon = {
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
expect(node.icon).toEqual({
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
expect(node.getAttribute('icon')).toBe(
|
|
|
|
JSON.stringify({
|
|
|
|
body,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2022-05-02 06:28:48 +00:00
|
|
|
// Wait to render
|
|
|
|
await nextTick();
|
|
|
|
|
2022-04-29 20:19:22 +00:00
|
|
|
// Should render SVG
|
2022-05-02 06:28:48 +00:00
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
const renderedIcon =
|
2022-04-30 20:12:34 +00:00
|
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"><rect width="10" height="10"><animate attributeName="width" values="10;5;10" dur="10s" repeatCount="indefinite"></animate></rect></svg>';
|
2022-04-29 20:19:22 +00:00
|
|
|
const html1 = node._shadowRoot.innerHTML;
|
|
|
|
const svg1 = node._shadowRoot.lastChild as SVGSVGElement;
|
|
|
|
const setCurrentTimeSupported = !!svg1.setCurrentTime;
|
2022-10-08 10:38:53 +00:00
|
|
|
expect(html1).toBe(
|
|
|
|
`${styleOpeningTag}${expectedBlock}</style>${renderedIcon}`
|
|
|
|
);
|
2022-04-29 20:19:22 +00:00
|
|
|
expect(svg1.outerHTML).toBe(renderedIcon);
|
|
|
|
|
|
|
|
// Restart animation, test icon again
|
|
|
|
node.restartAnimation();
|
|
|
|
|
|
|
|
const html2 = node._shadowRoot.innerHTML;
|
|
|
|
const svg2 = node._shadowRoot.lastChild as SVGSVGElement;
|
|
|
|
expect(html2).toBe(html1);
|
|
|
|
expect(svg2.outerHTML).toBe(renderedIcon);
|
|
|
|
|
|
|
|
// Node should be different because JSDOM does not support setCurrentTime(), but that might change in future
|
|
|
|
if (setCurrentTimeSupported) {
|
|
|
|
expect(svg2).toBe(svg1);
|
|
|
|
} else {
|
|
|
|
expect(svg2).not.toBe(svg1);
|
|
|
|
}
|
2022-05-02 06:28:48 +00:00
|
|
|
|
|
|
|
expect(node.status).toBe('rendered');
|
2022-04-29 20:19:22 +00:00
|
|
|
});
|
|
|
|
});
|