2020-04-28 09:47:35 +00:00
|
|
|
import mocha from 'mocha';
|
|
|
|
import chai from 'chai';
|
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
import { getNode, setRoot } from './node';
|
2020-07-01 20:29:43 +00:00
|
|
|
import { addFinder } from '@iconify/iconify/lib/modules/finder';
|
2020-04-28 09:47:35 +00:00
|
|
|
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';
|
2020-08-04 15:30:24 +00:00
|
|
|
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';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
const expect = chai.expect;
|
|
|
|
|
|
|
|
// Add finders
|
|
|
|
addFinder(iconifyFinder);
|
|
|
|
addFinder(iconifyIconFinder);
|
|
|
|
|
|
|
|
describe('Scanning DOM', () => {
|
|
|
|
// Add mentioned icons to storage
|
2020-05-29 19:08:45 +00:00
|
|
|
const storage = getStorage('', 'mdi');
|
2020-04-28 09:47:35 +00:00
|
|
|
addIconSet(storage, {
|
|
|
|
prefix: 'mdi',
|
|
|
|
icons: {
|
|
|
|
'account-box': {
|
|
|
|
body:
|
|
|
|
'<path d="M6 17c0-2 4-3.1 6-3.1s6 1.1 6 3.1v1H6m9-9a3 3 0 0 1-3 3a3 3 0 0 1-3-3a3 3 0 0 1 3-3a3 3 0 0 1 3 3M3 5v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2z" fill="currentColor"/>',
|
|
|
|
},
|
|
|
|
'account-cash': {
|
|
|
|
body:
|
|
|
|
'<path d="M11 8c0 2.21-1.79 4-4 4s-4-1.79-4-4s1.79-4 4-4s4 1.79 4 4m0 6.72V20H0v-2c0-2.21 3.13-4 7-4c1.5 0 2.87.27 4 .72M24 20H13V3h11v17m-8-8.5a2.5 2.5 0 0 1 5 0a2.5 2.5 0 0 1-5 0M22 7a2 2 0 0 1-2-2h-3c0 1.11-.89 2-2 2v9a2 2 0 0 1 2 2h3c0-1.1.9-2 2-2V7z" fill="currentColor"/>',
|
|
|
|
},
|
|
|
|
'account': {
|
|
|
|
body:
|
|
|
|
'<path d="M12 4a4 4 0 0 1 4 4a4 4 0 0 1-4 4a4 4 0 0 1-4-4a4 4 0 0 1 4-4m0 10c4.42 0 8 1.79 8 4v2H4v-2c0-2.21 3.58-4 8-4z" fill="currentColor"/>',
|
|
|
|
},
|
|
|
|
'home': {
|
|
|
|
body:
|
|
|
|
'<path d="M10 20v-6h4v6h5v-8h3L12 3L2 12h3v8h5z" fill="currentColor"/>',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
});
|
|
|
|
|
2020-08-02 18:48:53 +00:00
|
|
|
// Sanity check before running tests
|
2020-08-04 15:30:24 +00:00
|
|
|
expect(listRootNodes()).to.be.eql([]);
|
2020-08-02 18:48:53 +00:00
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
it('Scan DOM with preloaded icons', () => {
|
|
|
|
const node = getNode('scan-dom');
|
2020-08-04 15:30:24 +00:00
|
|
|
setRoot(node);
|
|
|
|
|
2020-04-28 09:47:35 +00:00
|
|
|
node.innerHTML =
|
|
|
|
'<div><p>Testing scanning DOM</p><ul>' +
|
|
|
|
'<li>Valid icons:' +
|
|
|
|
' <span class="iconify" data-icon="mdi:home" style="color: red; box-shadow: 0 0 2px black;"></span>' +
|
|
|
|
' <i class="iconify test-icon iconify--mdi-account" data-icon="mdi:account" style="vertical-align: 0;" data-flip="horizontal" aria-hidden="false"></i>' +
|
|
|
|
'</li>' +
|
|
|
|
'<li>Block icon:' +
|
|
|
|
' <iconify-icon data-icon="mdi-account-cash" title="<Cash>!"></iconify-icon>' +
|
|
|
|
' <iconify-icon data-icon="mdi:account-box" data-inline="true" data-rotate="2" data-width="42"></iconify-icon>' +
|
|
|
|
'</li>' +
|
|
|
|
'</ul></div>';
|
|
|
|
|
2020-08-02 18:48:53 +00:00
|
|
|
// Scan node
|
2020-04-28 09:47:35 +00:00
|
|
|
scanDOM();
|
|
|
|
|
|
|
|
// Find elements
|
|
|
|
const elements = node.querySelectorAll('svg.iconify');
|
|
|
|
expect(elements.length).to.be.equal(4);
|
2020-08-02 18:48:53 +00:00
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
// Check root nodes list
|
|
|
|
const nodes = listRootNodes();
|
|
|
|
expect(nodes.length).to.be.equal(1);
|
|
|
|
expect(nodes[0].node).to.be.equal(node);
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|
2020-08-01 08:12:55 +00:00
|
|
|
|
|
|
|
it('Scan DOM with unattached root', () => {
|
2020-08-04 15:30:24 +00:00
|
|
|
const fakeNode = getNode('scan-dom');
|
|
|
|
setRoot(fakeNode);
|
|
|
|
|
2020-08-01 08:12:55 +00:00
|
|
|
const node = document.createElement('div');
|
2020-08-04 15:30:24 +00:00
|
|
|
|
2020-08-01 08:12:55 +00:00
|
|
|
node.innerHTML = '<span class="iconify" data-icon="mdi:home"></span>';
|
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
// Check root nodes list
|
|
|
|
let nodes = listRootNodes();
|
|
|
|
expect(nodes.length).to.be.equal(1);
|
|
|
|
expect(nodes[0].node).to.be.equal(fakeNode);
|
2020-08-02 18:48:53 +00:00
|
|
|
|
|
|
|
// Scan node
|
2020-08-04 15:30:24 +00:00
|
|
|
scanElement(node);
|
2020-08-01 08:12:55 +00:00
|
|
|
|
|
|
|
// Find elements
|
|
|
|
const elements = node.querySelectorAll('svg.iconify');
|
|
|
|
expect(elements.length).to.be.equal(1);
|
2020-08-02 18:48:53 +00:00
|
|
|
|
|
|
|
// Make sure tempoary node was not added as root
|
2020-08-04 15:30:24 +00:00
|
|
|
nodes = listRootNodes();
|
|
|
|
expect(nodes.length).to.be.equal(1);
|
|
|
|
expect(nodes[0].node).to.be.equal(fakeNode);
|
2020-08-01 08:12:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Scan DOM with icon as root', () => {
|
2020-08-04 15:30:24 +00:00
|
|
|
const fakeNode = getNode('scan-dom');
|
|
|
|
setRoot(fakeNode);
|
|
|
|
|
2020-08-01 08:12:55 +00:00
|
|
|
const node = document.createElement('span');
|
|
|
|
node.setAttribute('data-icon', 'mdi:home');
|
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
// Check root nodes list
|
|
|
|
let nodes = listRootNodes();
|
|
|
|
expect(nodes.length).to.be.equal(1);
|
|
|
|
expect(nodes[0].node).to.be.equal(fakeNode);
|
|
|
|
|
2020-08-02 18:48:53 +00:00
|
|
|
// Scan node
|
2020-08-04 15:30:24 +00:00
|
|
|
scanElement(node);
|
2020-08-01 08:12:55 +00:00
|
|
|
|
|
|
|
// Check node
|
|
|
|
expect(node.tagName).to.be.equal('SPAN');
|
|
|
|
expect(node.innerHTML).to.be.equal('');
|
2020-08-04 15:30:24 +00:00
|
|
|
|
|
|
|
// 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);
|
2020-08-01 08:12:55 +00:00
|
|
|
});
|
2020-04-28 09:47:35 +00:00
|
|
|
});
|