I recently have start using Jasmine, and I have a weird situation. I am testing a function which does a DOM manipulation with another function call. Jasmine is throwing an error by saying that elem.className is not a constructor. Here is the structure of my code:
Controller code:
function resetElem(elem) {
elem.classList.remove(/some string/);
}
function a() {
resetElem(document.getElementsByClassName('xx')[0])
}
Jasmine:
var elem;
beforeEach(inject(function($rootScope, $controller, $q) {
deferred = $q.defer();
scope = $rootScope.$new();
mockHTMLElements();
createController($controller);
}));
function mockHTMLElements() {
/* //This was the other method that I tried.
jasmine.spyOn(document,
'getElementsByClassName').and.CallFake(function(name) {
var el;
if (!elems[name]) {
el = document.createElement('div');
el.className = name;
elems[name] = el;
}
return [elems[name]];
});
*/
elems['preview-promo'] = document.createElement('div');
elems['preview-promo'].className="preview-promo";
elems['create-promo'] = document.createElement('div');
elems['create-promo'].className="create-promo";
elem = {
classList : {
add : jasmine.createSpy('add'),
remove : jasmine.createSpy('remove'),
},
className : 'test'
};
}
it('tests the a function', function() {
// vm is ref of this of controller
vm.a(true);
...some other tests
});
Console returns
TypeError: undefined is not an object (evaluating 'elem.classList')
Not sure how to make this right.