import mocha from 'mocha';
import chai from 'chai';
import { getNode, setRoot } from './node';
import { addFinder } from '@iconify/iconify/lib/modules/finder';
import { finder as iconifyFinder } from '@iconify/iconify/lib/finders/iconify';
import { finder as iconifyIconFinder } from '@iconify/iconify/lib/finders/iconify-icon';
import { getStorage, addIconSet } from '@iconify/core/lib/storage/storage';
import { listRootNodes } from '@iconify/iconify/lib/modules/root';
import { scanDOM, scanElement } from '@iconify/iconify/lib/modules/scanner';
import { removeObservedNode } from '@iconify/iconify/lib/modules/observer';
const expect = chai.expect;
// Add finders
addFinder(iconifyFinder);
addFinder(iconifyIconFinder);
describe('Scanning DOM', () => {
// Add mentioned icons to storage
const storage = getStorage('', 'mdi');
addIconSet(storage, {
prefix: 'mdi',
icons: {
'account-box': {
body:
'',
},
'account-cash': {
body:
'',
},
'account': {
body:
'',
},
'home': {
body:
'',
},
},
width: 24,
height: 24,
});
// Sanity check before running tests
expect(listRootNodes()).to.be.eql([]);
it('Scan DOM with preloaded icons', () => {
const node = getNode('scan-dom');
setRoot(node);
node.innerHTML =
'
Testing scanning DOM (should render SVG!)
' +
'- Valid icons:' +
' ' +
' ' +
'
' +
'- Block icon:' +
' ' +
' ' +
'
' +
'
';
// Scan node
scanDOM();
// Find elements
const elements = node.querySelectorAll('svg.iconify');
expect(elements.length).to.be.equal(4);
// Check root nodes list
const nodes = listRootNodes();
expect(nodes.length).to.be.equal(1);
expect(nodes[0].node).to.be.equal(node);
});
it('Scan DOM with unattached root', () => {
const fakeNode = getNode('scan-dom');
setRoot(fakeNode);
const node = document.createElement('div');
node.innerHTML = '';
// Check root nodes list
let nodes = listRootNodes();
expect(nodes.length).to.be.equal(1);
expect(nodes[0].node).to.be.equal(fakeNode);
// Scan node
scanElement(node);
// Find elements
const elements = node.querySelectorAll('svg.iconify');
expect(elements.length).to.be.equal(1);
// Make sure tempoary node was not added as root
nodes = listRootNodes();
expect(nodes.length).to.be.equal(1);
expect(nodes[0].node).to.be.equal(fakeNode);
});
it('Scan DOM with icon as root', () => {
const fakeNode = getNode('scan-dom');
setRoot(fakeNode);
const node = document.createElement('span');
node.setAttribute('data-icon', 'mdi:home');
// Check root nodes list
let nodes = listRootNodes();
expect(nodes.length).to.be.equal(1);
expect(nodes[0].node).to.be.equal(fakeNode);
// Scan node
scanElement(node);
// Check node
expect(node.tagName).to.be.equal('SPAN');
expect(node.innerHTML).to.be.equal('');
// Make sure tempoary node was not added as root
nodes = listRootNodes();
expect(nodes.length).to.be.equal(1);
expect(nodes[0].node).to.be.equal(fakeNode);
});
});