Skip to content

Instantly share code, notes, and snippets.

@mariapacana
Last active December 16, 2015 01:19
Show Gist options
  • Save mariapacana/5354325 to your computer and use it in GitHub Desktop.
Save mariapacana/5354325 to your computer and use it in GitHub Desktop.
The first file is a Jasmine test, the second is the file I am trying to test. When I try to run both tests (one for adding a row and one for deleting a row), Jasmine gives me a "TypeError: Object is not a function" error. But if I comment out one of the tests and just run the other test, it works perfectly. Specifically, Jasmine says: TypeError:…
//The Jasmine test.
describe("A suite", function() {
var addFriendButton;
var removeFriendButton;
var contacts;
beforeEach(function() {
addFriendButton = document.createElement("button");
document.body.appendChild(addFriendButton);
addFriendButton.setAttribute("id", "addFriendButton");
removeFriendButton = document.createElement("button");
document.body.appendChild(removeFriendButton);
removeFriendButton.setAttribute("id", "removeFriendButton");
contacts = document.createElement("table");
document.body.appendChild(contacts);
contacts.setAttribute("id", "contacts");
var firstRow = contacts.insertRow(0);
for (i = 0; i < 4 ; i++) {
firstRow.insertCell(i);
}
//The part that Jasmine is objecting to?
contactList = new contactList(addFriendButton, removeFriendButton, contacts);
});
//First test.
it("should be able to add a row to the table", function() {
var event = new MouseEvent("click", {bubbles: true, cancelable: true});
contactList.addFriendButton.dispatchEvent(event);
var rows = contactList.contacts.getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
expect(rows).toEqual(2);
});
//Second test.
it("should be able to delete a row from the table", function() {
var event = new MouseEvent("click", {bubbles: true, cancelable: true});
contactList.removeFriendButton.dispatchEvent(event);
var rows = contactList.contacts.getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
console.log(rows);
expect(rows).toEqual(1);
});
afterEach(function() {
document.body.removeChild(addFriendButton);
document.body.removeChild(removeFriendButton);
document.body.removeChild(contacts);
});
});
// function onload() {
// var addFriendButton = document.getElementById("addFriendButton");
// var removeFriendButton = document.getElementById("removeFriendButton");
// var contacts = document.getElementById("contacts");
// contactList = new contactList(addFriendButton, removeFriendButton, contacts)
// };
//Javascript being tested.
function contactList(addFriendButton, removeFriendButton, contacts) {
this.addFriendButton = addFriendButton;
this.removeFriendButton = removeFriendButton;
this.contacts = contacts;
var self = this;
this.addFriendButton.addEventListener("click", function (e) { self.addFriend(e) }, false);
this.removeFriendButton.addEventListener("click", function (e) { self.removeFriend(e) }, false);
};
contactList.prototype.addFriend = function (e) {
if (!event) var event = window.event;
event.preventDefault();
var lastrow = this.contacts.insertRow(-1);
numColumns = this.contacts.rows[0].cells.length;
for (var i=0; i<numColumns; i++) {
var cell = lastrow.insertCell(i);
cell.innerHTML = "<input type='text'>";
}
};
contactList.prototype.removeFriend = function (e) {
if (!event) var event = window.event;
event.preventDefault();
this.contacts.deleteRow(-1);
};
//Revised Jasmine test!!
describe("A suite", function() {
var addFriendButton;
var removeFriendButton;
var contacts;
var myContactList;
beforeEach(function() {
addFriendButton = document.createElement("button");
document.body.appendChild(addFriendButton);
addFriendButton.setAttribute("id", "addFriendButton");
removeFriendButton = document.createElement("button");
document.body.appendChild(removeFriendButton);
removeFriendButton.setAttribute("id", "removeFriendButton");
contacts = document.createElement("table");
document.body.appendChild(contacts);
contacts.setAttribute("id", "contacts");
var firstRow = contacts.insertRow(0);
for (i = 0; i < 4 ; i++) {
firstRow.insertCell(i);
}
myContactList = new contactList(addFriendButton, removeFriendButton, contacts);
});
it("should be able to add a row to the table", function() {
var event = new MouseEvent("click", {bubbles: true, cancelable: true});
myContactList.addFriendButton.dispatchEvent(event);
var rows = myContactList.contacts.getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
expect(rows).toEqual(2);
});
it("should be able to delete a row from the table", function() {
var event = new MouseEvent("click", {bubbles: true, cancelable: true});
myContactList.removeFriendButton.dispatchEvent(event);
var rows = myContactList.contacts.getElementsByTagName("tbody")[0].getElementsByTagName("tr").length;
console.log(rows);
expect(rows).toEqual(0);
});
afterEach(function() {
document.body.removeChild(addFriendButton);
document.body.removeChild(removeFriendButton);
document.body.removeChild(contacts);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment