How to set the style on $document.body in Jasmine unit test - angularjs

I'm trying to test a function that dynamically determines how many empty rows and columns should be added to a grid. In order to do that I need to set the width of the $document.body. I've so far been able to successfully append and style divs on the body, but not style the body itself. Here's that code:
// Set up the DOM
body = angular.element($document[0].body);
mainColumn = angular.element('<div></div>')[0];
reportGrid = angular.element('<div></div>')[0];
body.append(mainColumn);
body.append(classReportGrid);
// Style elements
mainColumn.style.height = '413px';
mainColumn.style.width = '600px';
reportGrid.style.height = '1000px';
reportGrid.style.width = '1040px';
Here are some approaches I've tried with their corresponding errors bellow:
body.style.width = '800px';
// TypeError: 'undefined' is not an object (evaluating 'body.style.width = '800px'')
body.setAttribute('style', 'width: 800px');
// TypeError: 'undefined' is not a function (evaluating 'body.setAttribute('style', 'width: 800px')')
If I log the body.style, it's undefined. Any advice is appreciated!

Related

Cannot Change Property of Non-state Object Inside setState Function

I'm updating the 'backgroundColor' property of the all the objects nested within a sliderSegmentStyleDict state object using the following:
setSliderSegmentStyleDict(prevState => {
let updatedSegmentStyleDict = prevState
for(let segmentIndex in Object.keys(updatedSegmentStyleDict)) {
segmentIndex = Object.keys(updatedSegmentStyleDict)[segmentIndex]
let segmentDict = updatedSegmentStyleDict[segmentIndex]
segmentDict['backgroundColor'] = snappedSegmentIndex > segmentIndex ? '#19486A' : '#d1d5db'
updatedSegmentStyleDict[segmentIndex] = segmentDict
}
return updatedSegmentStyleDict
})
The first time the component renders, it works fine and sets the appropriate colour.
However, when I subsequently invoke this function manually, I get the following error in the console:
Uncaught TypeError: Cannot assign to read only property 'backgroundColor' of object '#<Object>'
Why am I unable to modify the backgroundColor property of segmentDict object?

getElementById is not working if a checkbox's value is passed as variable

Here is JS function. this function is an event handler for a checkbox. so the element is the checkbox.
function updateSelect(element) {
// this works
document.getElementById("file_update_" + 1).disabled = !element.checked;
// this does not works. the element.value is also 1
document.getElementById("file_update_" + element.value).disabled = !element.checked;
}
the console has the following error.
Uncaught TypeError: Cannot set property 'disabled' of null
at updateSelect (<anonymous>:21:75)
at HTMLInputElement.onchange (updateuser:1)
any idea....what is unique about the element.value?
After doing some trial and error this worked
function updateSelect(element) {
var v = parseInt(element.value, 10);
document.getElementById("file_update_" + v).disabled = !element.checked;
}
I do not know why I had to do this to make it work.
I would appreciate it if someone helps me to understand it.

Extjs grid.reconfigure throws Exception (Uncaught TypeError: Cannot read property 'isSorter' of null)

I'm trying to reset grid to initial state before any user's changes of column width, order, visibility and so on. I saved columnConfigs in initialConfig of grid like that
var gridColumns = grid.getColumns();
var initialColumnConfigs = [];
Ext.Array.map(gridColumns, function (column) {
initialColumnConfigs.push(column.getInitialConfig());
});
grid.getInitialConfig().initialColumnConfigs = initialColumnConfigs;
and now I'm trying to reset grid using reconfigure like that:
initialColumnConfigs = grid.getInitialConfig().initialColumnConfigs;
Ext.state.Manager.clear(grid.stateId);
grid.reconfigure(grid.getStore(), initialColumnConfigs);
but I end up with exception from title. Is there something that I'm doing wrong?

AngularJS Object [object Object] has no method 'appendChild'

I am trying to create a simple bar chart, but when appending the bars to the DOM I get this error
Object [object Object] has no method 'appendChild'
$rootScope.drawChart = function (data,selector,padding){
var max = Math.max.apply(Math, data);
var chart = angular.element(document.getElementById("chartxx"));
var barwidth = ((chart.offsetWidth-(data.length-1)*padding-(data.length)*10)/data.length);
var sum = data.reduce(function(pv, cv) { return pv + cv; }, 0);
var left = 0;
for (var i in data){
var newbar = document.createElement('div');
newbar.setAttribute("class", "bar");
newbar.style.width=barwidth+"px";
newbar.style.height=((data[i]/max)*100)+"%";
newbar.style.left=left+"px";
console.log(chart);
angular.element(document.getElementById("chartxx")).appendChild(newbar);
left += (barwidth+padding+10);
}
}
var values = [85,95,120,100,200,200,230,230,60,320,23,3433,434,45,23,23];
$rootScope.drawChart(values,"#chartxx2",5);
<div class="wrapperx2">
<div id="chartxx"></div>
</div>
appendChild() is a method inherent to native HTML objects, aka the result of document.getElementById. When you give that HTML object to angular.element it becomes a jQuery Object. jQuery objects have a similar method called append()
So you can do document.getElementById("chartxx").appendChild(newbar) or angular.element(document.getElementById("chartxxx")).append(newbar).
So that should answer your question but I can't help but ask myself : why would you do something like that when you're using AngularJS ?
Edit
Ok so here's a very poorly achieved version of what I would have done in your place (if I was ABSOLUTELY unable to use an external library, because using an external library for charts is what I would normally do). I suggest you see and try to understand how the ng-repeat works here and try to apply it to your case.
angular.element doesn't contain method appendChild
angular.element doc
You can try:
chart.append(newbar);

Protractor: addLocator() : 'Cannot read property 'querySelectorAll of null' '

I trying to run standart example from Protractor's documentation ( http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.addLocator ). And have an Error: 'Cannot read property 'querySelectorAll of null'
by.addLocator('buttonTextSimple',
function(buttonText, opt_parentElement, opt_rootSelector ) {
var using = opt_parentElement,
buttons = using.querySelectorAll('button');
return Array.prototype.filter.call(buttons, function(button) {
return button.textContent === buttonText;
});
});
View the same:
<button ng-click="doAddition()">Go!</button>
What I should do to solve this problem?
You should declare a variable called using like this:
var using = opt_parentElement || document;
so if there is no optional parent element provided, then global document will be used to query for results.
Not sure if it is a typo in the documentation or Protractor was expected to auto-fill opt_parentElement variable with some defaults if it is not being set.

Resources