Write tests, fix bugs

This commit is contained in:
Timothy Farrell 2017-02-02 22:19:53 -06:00
parent 40139e1c02
commit 49259828af
4 changed files with 64 additions and 12 deletions

View File

@ -51,11 +51,11 @@ describe('Projector', () => {
} }
describe('getElement', () => { describe('getElement', () => {
it('getElement(null) method should to return the container.', () => { it('should to return the container from a null id.', () => {
expect(projector.getElement(null)).toBe(container); expect(projector.getElement(null)).toBe(container);
}); });
it('getElement(id) method should to return the patched node.', () => { it('should to return the patched node from an integer id.', () => {
const id = add(h('div', { className: 'first' })); const id = add(h('div', { className: 'first' }));
expect(projector.getElement(id)._id).toBe(id); expect(projector.getElement(id)._id).toBe(id);
}); });
@ -245,11 +245,15 @@ describe('Projector', () => {
it('do not propagate a removed sibling event', done => { it('do not propagate a removed sibling event', done => {
let callCount = 1; let callCount = 1;
function eventHandler(evt) { function eventHandler(evt) {
expect(evt.target._id).toBe(Bid); if (evt.target._id === Bid) {
patch(Bid, { onclick: null }); patch(Bid, { onclick: null });
if (0 === callCount--) { if (0 === callCount--) {
fail('Should not be called twice.'); fail('Should not be called twice.');
} }
} else {
expect(evt.target._id).toBe(Aid);
expect(callCount).toBe(0);
}
} }
projector.subscribe(eventHandler); projector.subscribe(eventHandler);
@ -261,7 +265,10 @@ describe('Projector', () => {
return waitForNextFrame(); return waitForNextFrame();
}) })
.then(() => { .then(() => {
// debugger; projector.getElement(Aid).click();
return waitForNextFrame();
})
.then(() => {
projector.getElement(Bid).click(); projector.getElement(Bid).click();
return waitForNextFrame(); return waitForNextFrame();
}) })
@ -269,4 +276,49 @@ describe('Projector', () => {
.catch(console.error.bind(console)); .catch(console.error.bind(console));
}); });
}); });
describe('setElementProperty', () => {
it('Can set element properties', () => {
const id = add(h('div', { className: 'first' }));
const el = projector.getElement(id);
expect(el.className).toBe('first');
projector.setElementProperty(id, 'className', 'second');
expect(el.className).toBe('second');
});
it('Can set pathed element properties', () => {
const id = add(h('div', { className: 'first' }));
const el = projector.getElement(id);
expect(el.className).toBe('first');
projector.setElementProperty(id, 'style.backgroundColor', 'blue');
expect(el.style.backgroundColor).toBe('blue');
});
it('Cannot set innerHTML', () => {
const id = add(h('div', { className: 'first' }));
const el = projector.getElement(id);
expect(el.className).toBe('first');
try {
projector.setElementProperty(id, 'innerHTML', '<hr>');
} catch (e) {
expect(el.innerHTML).not.toBe('<hr>');
expect(e.message).toBe('Access Denied');
return;
}
fail('Setting innerHTML should fail.');
});
});
describe('runElementMethod', () => {
it('Can click', done => {
function eventHandler(evt) {
expect(evt.target._id).toBe(id);
done();
}
projector.subscribe(eventHandler);
const id = add(h('input', { type: 'text', value: 'test', onclick: true }));
projector.runElementMethod(id, 'click');
});
});
}); });

View File

@ -9,7 +9,7 @@ export function history(onChange) {
return (...args) => onChange([6, 'history', name, args]); return (...args) => onChange([6, 'history', name, args]);
} }
// return undefined; // return undefined;
} },
set(target, name, value) { set(target, name, value) {
throw new Error(`Cannot set ${name} on history.`); throw new Error(`Cannot set ${name} on history.`);

View File

@ -1,2 +1,2 @@
export { Projector } from './projector.js'; export { Projector } from './projector.js';
export { Scanner } from './scanner.js'; // export { Scanner } from './scanner.js';

View File

@ -166,9 +166,9 @@ export function Projector(domRoot) {
throw new Error('Access Denied'); throw new Error('Access Denied');
} }
do { while (path.length > 1) {
ptr = ptr[path.pop()]; ptr = ptr[path.shift()];
} while (path.length > 1); }
return (ptr[path[0]] = value); return (ptr[path[0]] = value);
} }