Add method to get bound element
This commit is contained in:
parent
dace94b72b
commit
c04a45f8d3
@ -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}']`);
|
||||
}
|
||||
}
|
||||
|
@ -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}']`);
|
||||
});
|
||||
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user