Skip to content

Commit

Permalink
Tests for MainController
Browse files Browse the repository at this point in the history
  • Loading branch information
Antti Salo committed Oct 15, 2014
1 parent 526ab9b commit cd2b04c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 22 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"angular-highlightjs": "0.3.0",
"angular-bootstrap-colorpicker": "3.0.8",
"angular-local-storage": "0.1.0",
"ui-router": "0.2.11"
"ui-router": "0.2.11",
"angular-mocks": "1.2.26"
},
"devDependencies": {}
}
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gulp.task('jscs', function() {
'*.js',
'lib/*.js',
'test/*.js',
'test/**/*.js',
'lib/app/js/app.js',
'lib/app/js/controllers/**/**.js',
'lib/app/js/directives/**/**.js',
Expand Down
22 changes: 12 additions & 10 deletions lib/app/js/controllers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ angular.module('sgApp')

$scope.isNavCollapsed = false;

//on page refresh check if all markup was hidden or not
var showAllMarkup = localStorageService.get('showAllMarkup');

//because localStorage only saves String type values, try to convert to boolean
if (showAllMarkup == null) {
$scope.showAllMarkup = true;
} else {
if (showAllMarkup == 'false') {
$scope.showAllMarkup = false;
} else {
// Because localStorage only saves String type values, try to convert to boolean
$scope.checkIfMarkupVisible = function() {
var showAllMarkup = localStorageService.get('showAllMarkup');
if (showAllMarkup == null) {
$scope.showAllMarkup = true;
} else {
if (showAllMarkup == 'false' || showAllMarkup === false) {
$scope.showAllMarkup = false;
} else {
$scope.showAllMarkup = true;
}
}
}

$scope.checkIfMarkupVisible();

// Fetch styleguide data
Styleguide.get()
.success(function(data) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@
"gulp-sourcemaps": "^1.2.2",
"jscs": "^1.7.1",
"karma": "^0.12.24",
"karma-chai": "^0.1.0",
"karma-mocha": "^0.1.9",
"karma-mocha-reporter": "^0.3.1",
"karma-phantomjs-launcher": "^0.1.4",
"karma-sinon-chai": "^0.2.0",
"main-bower-files": "^2.0.0",
"mocha": "^1.21.4",
"sinon": "^1.10.3",
"sinon-chai": "^2.6.0",
"tiny-lr": "^0.1.4"
},
"scripts": {
Expand Down
9 changes: 1 addition & 8 deletions test/angular/appSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ describe('sgApp module registration', function() {
var module;

before(function() {
module = angular.module('sgApp', [
'ui.router',
'ngAnimate',
'colorpicker.module',
'hljs',
'LocalStorageModule',
'oc.lazyLoad'
]);
module = angular.mock.module('sgApp');
});

it('should be registered', function() {
Expand Down
84 changes: 84 additions & 0 deletions test/angular/controllers/mainCtrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

describe('Controller: MainCtrl', function() {

var ctrl,
scope,
httpBackend,
json,
localstorage;

// Load the controller's module
beforeEach(angular.mock.module('sgApp'));

// Initialize the controller and a mock scope
beforeEach(inject(function($controller, $rootScope, $httpBackend, localStorageService) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
localstorage = localStorageService;

ctrl = $controller('MainCtrl', {
$scope: scope
});

json = {sections:[{heading: 'Title'}, {heading: 'Title2'}]};

httpBackend.expectGET('styleguide.json').
respond(json);
httpBackend.expectGET('views/main.html').
respond('');
httpBackend.expectGET('views/sections.html').
respond('');

httpBackend.flush();
}));

it('should be defined', function() {
expect(ctrl).not.to.equal(null);
});

it('search parameter should be cleared after search', function() {
scope.search = 'test';
scope.clearSearch();
expect(scope.search).to.be.empty;
});

it('should get section data from json', function() {
expect(scope.sections).to.eql(json.sections);
});

describe('getting markup visible state from localstorage', function() {

it('should return true with true values', function() {
sinon.stub(localstorage, 'get').returns(true);
scope.checkIfMarkupVisible();
expect(scope.showAllMarkup).to.eql(true);
});

it('should return true by default', function() {
sinon.stub(localstorage, 'get');
scope.checkIfMarkupVisible();
expect(scope.showAllMarkup).to.eql(true);
});

it('should return false with false string value', function() {
sinon.stub(localstorage, 'get').returns('false');
scope.checkIfMarkupVisible();
expect(scope.showAllMarkup).to.eql(false);
});

it('should return false with false boolean value', function() {
sinon.stub(localstorage, 'get').returns(false);
scope.checkIfMarkupVisible();
expect(scope.showAllMarkup).to.eql(false);
});

it('should return true with unknown values', function() {
sinon.stub(localstorage, 'get').returns('foobar');
scope.checkIfMarkupVisible();
expect(scope.showAllMarkup).to.eql(true);
});

});

});
18 changes: 16 additions & 2 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],
frameworks: ['mocha', 'sinon-chai'],

// list of files / patterns to load in the browser
files: [
// components
'lib/app/js/components/angular/angular.js',
'lib/app/js/components/ui-router/release/angular-ui-router.js',
'lib/app/js/components/angular-animate/angular-animate.js',
'lib/app/js/components/angular-bootstrap-colorpicker/js/bootstrap-colorpicker-module.js',
'lib/app/js/components/angular-local-storage/dist/angular-local-storage.js',
'lib/app/js/components/highlightjs/highlight.pack.js',
'lib/app/js/components/angular-highlightjs/angular-highlightjs.js',
'lib/app/js/components/oclazyload/dist/ocLazyLoad.js',
'lib/app/js/components/angular-mocks/angular-mocks.js',
'node_modules/mock-localstorage/lib/mock-localstorage.js',
// application code
'lib/app/js/*.js',
'lib/app/js/controllers/*.js',
Expand All @@ -39,7 +46,14 @@ module.exports = function(config) {
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
reporters: ['mocha'],

plugins: [
'karma-mocha',
'karma-sinon-chai',
'karma-mocha-reporter',
'karma-phantomjs-launcher'
],

// web server port
port: 8080,
Expand Down

0 comments on commit cd2b04c

Please sign in to comment.