diff --git a/js/src/viewmodel.js b/js/src/viewmodel.js index d558a39..9c2cc02 100644 --- a/js/src/viewmodel.js +++ b/js/src/viewmodel.js @@ -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}']`); + } } diff --git a/js/test/viewmodel.test.js b/js/test/viewmodel.test.js index 3e0f077..5094874 100644 --- a/js/test/viewmodel.test.js +++ b/js/test/viewmodel.test.js @@ -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}']`); + }); + });