Jasmine Angular Test

by willtx

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular-mocks.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine-html.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.4.1/jasmine.css">

JavaScript

(function(angular) {

  var adminApp = angular.module('adminClientApp', []);

  adminApp.controller('AdminClientController', [function() {
    var adminClient = this;
    adminClient.name = 'Client Name';
    adminClient.sites = [{
      id: "site1",
      domain: "myschool.com",
      pages: [{
        id: 1,
        name: "HOME"
      }, {
        id: 2,
        name: "FORM"
      }, {
        id: 3,
        name: "LOGIN"
      }, {
        id: 4,
        name: "CONTACT"
      }, {
        id: 5,
        name: "ABOUT"
      }, {
        id: 6,
        name: "NEWS"
      }]
    }];
  }]);

})(angular);


// ---SPECS-------------------------
describe('AdminClientController', function() {

  beforeEach(module('adminClientApp'));

  var $scope, $controller;

  beforeEach(inject(function(_$controller_, _$rootScope_) {
    $rootScope = _$rootScope_;
    $controller = _$controller_;

    $scope = $rootScope.$new();
  }));


  describe('site info', function() {

    beforeEach(function() {
      //$scope = {};
      controller = $controller('AdminClientController as adminClient', {
        $scope: $scope
      });
    });

    it('should create a site model with 6 pages', function() {
      expect(controller.adminClient.sites[0].pages.length).toBe(6);
    });

    it('should set the name', function() {
      expect(controller.adminClient.name).toBe('Client Name');
    });
  });

});

// --- Runner -------------------------
(function() {
  var jasmineEnv = jasmine.getEnv();
  jasmineEnv.updateInterval = 1000;

  var htmlReporter = new jasmine.HtmlReporter();

  jasmineEnv.addReporter(htmlReporter);

  jasmineEnv.specFilter = function(spec) {
    return htmlReporter.specFilter(spec);
  };

  var currentWindowOnload = window.onload;

  window.onload = function() {
    if (currentWindowOnload) {
      currentWindowOnload();
    }
    execJasmine();
  };

  function execJasmine() {
    jasmineEnv.execute();
  }

})();