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';
|
|
|
|
import { listRootNodes } from '@iconify/iconify/lib/modules/root';
|
2020-07-01 20:29:43 +00:00
|
|
|
import {
|
|
|
|
initObserver,
|
|
|
|
pauseObserver,
|
|
|
|
} from '@iconify/iconify/lib/modules/observer';
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
const expect = chai.expect;
|
|
|
|
|
|
|
|
describe('Testing observer creation', () => {
|
2020-06-11 21:46:47 +00:00
|
|
|
it('Creating observer and triggering event', (done) => {
|
2020-04-28 09:47:35 +00:00
|
|
|
const node = getNode('observer-creation');
|
2020-07-01 20:29:43 +00:00
|
|
|
setRoot(node);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
// Get node
|
|
|
|
const list = listRootNodes();
|
|
|
|
expect(list.length).to.be.equal(1);
|
|
|
|
|
|
|
|
const item = list[0];
|
|
|
|
expect(item.node).to.be.equal(node);
|
|
|
|
expect(item.observer).to.be.equal(void 0);
|
|
|
|
|
|
|
|
// Do test
|
2020-04-28 09:47:35 +00:00
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
node.innerHTML = '<div></div><ul><li>test</li><li>test2</li></ul>';
|
2020-07-01 20:29:43 +00:00
|
|
|
initObserver((root) => {
|
2020-08-04 15:30:24 +00:00
|
|
|
expect(root.node).to.be.equal(node);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
// Should be called only once
|
|
|
|
expect(counter).to.be.equal(1);
|
|
|
|
|
2020-08-04 15:30:24 +00:00
|
|
|
// Check if observer is paused
|
|
|
|
expect(item.observer).to.not.be.equal(void 0);
|
|
|
|
expect(item.observer.paused).to.be.equal(0);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
// Pause observer
|
2020-07-01 20:29:43 +00:00
|
|
|
pauseObserver();
|
2020-08-04 15:30:24 +00:00
|
|
|
expect(item.observer.paused).to.be.equal(1);
|
2020-04-28 09:47:35 +00:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Add few nodes to trigger observer
|
2020-08-04 15:30:24 +00:00
|
|
|
expect(item.observer).to.not.be.equal(void 0);
|
|
|
|
expect(item.observer.paused).to.be.equal(0);
|
2020-04-28 09:47:35 +00:00
|
|
|
node.querySelector('div').innerHTML =
|
|
|
|
'<span class="test">Some text</span><i>!</i>';
|
|
|
|
});
|
|
|
|
});
|