I got this pretty interesting homework assignment, which was to finish off a very simple Javascript library. I couldn't use jQuery.
First, I will show you the raw piece of code that I received, then I will show you my solution and the last piece of code is what I think is the test of all the functions I had to make. I checked it and it worked pretty well- I mean the console output was as expected. I would like to ask you whether my solution is okay or not.
What I was given:
'use strict';
var dom = {
// TODO: fill in library's API ...
create: function(tag_name, attrs){
},
// Attributes getter / setter
attr: function(elem, name, value){
},
// Append element as last child
append: function(elem, child){
},
// Prepend element as first child
prepend: function(elem, child){
},
// Detach element from dom
remove: function(elem){
}
}
What I did:
'use strict';
var dom = {
// TODO: fill in library's API ...
create: function(tag_name, attrs){
var temp = document.createElement(tag_name);
for(var key in attrs)
{
temp.setAttribute(key, attrs[key]);
}
return temp;
},
// Attributes getter / setter
attr: function(elem, name, value){
if(!value) return elem.getAttribute(name);
elem.setAttribute(name,value);
return elem;
},
// Append element as last child
append: function(elem, child){
elem.appendChild(child);
},
// Prepend element as first child
prepend: function(elem, child){
elem.prepend(child);
},
// Detach element from dom
remove: function(elem){
var par = elem.parentNode;
par.removeChild(elem);
}
}
WHAT I THINK IS A TEST:
'use strict';
describe('Dom library', function () {
it('should allow to create dom elements', function () {
var div = dom.create('div');
expect(div instanceof window.Element).toBeTruthy();
expect(div.tagName).toBe('DIV');
});
it('should allow to create dom elements with attributes', function () {
var div = dom.create('div',{
test:'test_value'
});
expect(div instanceof window.Element).toBeTruthy();
expect(div.getAttribute('test')).toBe('test_value');
});
it('should allow to append elements', function () {
var div = document.createElement('div');
div.innerHTML = '<div id="one"></div>\
<div id="two"></div>\
<div id="three"></div>';
var appendee = document.createElement('div');
dom.append(div, appendee)
expect(div.children[div.children.length - 1]).toBe(appendee);
});
it('should allow to prepend elements', function () {
var div = document.createElement('div');
div.innerHTML = '<div id="one"></div>\
<div id="two"></div>\
<div id="three"></div>';
var prependee = document.createElement('div');
dom.prepend(div, prependee)
expect(div.children[0]).toBe(prependee);
});
it('should allow to detach elements', function () {
var div = document.createElement('div');
div.innerHTML = '<div id="one"></div>\
<div id="two"></div>\
<div id="three"></div>';
var target = div.children[0];
var detached = dom.remove(target)
expect(target).toBe(detached);
expect(div.children[0]).not.toBe(target);
});
it('should allow get element attribute', function () {
var div = document.createElement('div');
div.setAttribute("test","test_value");
expect(dom.attr(div, 'test')).toEqual('test_value');
});
it('should allow set element attribute', function () {
var div = document.createElement('div');
dom.attr(div, 'test', 'test_value');
expect(div.getAttribute('test')).toEqual('test_value');
});
});