Add method to get bound element

This commit is contained in:
Bartek Fabiszewski 2019-12-28 22:22:51 +01:00
parent dace94b72b
commit c04a45f8d3
2 changed files with 24 additions and 0 deletions

View File

@ -142,4 +142,8 @@ export default class ViewModel {
unsubscribe(property, callback) {
uObserve.unobserve(this.model, property, callback);
}
getBoundElement(property) {
return this.root.querySelector(`[data-bind='${property}']`);
}
}

View File

@ -58,6 +58,16 @@ describe('ViewModel tests', () => {
expect(ViewModel.prototype.bind).toHaveBeenCalledWith(propertyBool);
});
it('should set root element', () => {
// given
spyOn(ViewModel.prototype, 'bind');
const rootEl = document.querySelector('body');
// when
vm.bindAll(rootEl);
// then
expect(vm.root).toEqual(rootEl);
});
it('should set up binding between model property and DOM input element', () => {
// given
/** @type {HTMLInputElement} */
@ -192,4 +202,14 @@ describe('ViewModel tests', () => {
expect(uObserve.unobserve).toHaveBeenCalledWith(vm.model, propertyString, callback);
});
it('should get bound element by property name', () => {
// given
const property = 'property';
spyOn(vm.root, 'querySelector');
// when
vm.getBoundElement(property);
// then
expect(vm.root.querySelector).toHaveBeenCalledWith(`[data-bind='${property}']`);
});
});