// First we have our controller...
function SpeedmonitorController($scope) {
// The current url we're preparing to add.
$scope.currentUrl = "";
// In this array, we'll keep track of the urls and their last fetch time.
$scope.urls = [{url: "www.google.com", loadSpeed: 50},
{url: "angularjs.org", loadSpeed: 75},
{url: "www.dwmkerr.com", loadSpeed: 120}];
// Adds an url.
$scope.addUrl = function() {
$scope.urls.push({url: $scope.currentUrl, loadSpeed: Math.random() * 75 + 50});
$scope.currentUrl = ""; // Now clear the current URL.
};
// Remove an url.
$scope.removeUrl = function(index) {
$scope.urls.splice(index, 1);
};
}
// Now we have our specs.
describe("Speedmeter Controller", function() {
// Create a scope.
var scope = {};
var controller = {};
beforeEach(function() {
// Create a scope, run the controller
scope = {};
controller = new SpeedmonitorController(scope);
});
it("initialises an empty url", function() {
expect(scope.currentUrl).toBe('');
});
it("initialises three sample urls", function() {
expect(scope.urls.length).toBe(3);
});
it("correctly adds new urls", function() {
// Calling 'addUrl'...
scope.currentUrl = "www.test.com";
scope.addUrl();
// ...should add the url to the end of the list...
expect(scope.urls[scope.urls.length-1].url).toBe("www.test.com");
});
it("clears the current url after adding it", function() {
// Set an url and add it.
scope.currentUrl = "www.test.com";
scope.addUrl();
// It should now be empty.
expect(scope.currentUrl).toBe("");
});
it("removes urls based on index", function() {
// Get the first url.
var firstUrl = scope.urls[0];
// Remove it by index.
scope.removeUrl(0);
// It shouldn't be the first element.
expect(scope.urls[0]).toNotBe(firstUrl);
});
});