Angular unit tests exercise
Angular unit tests exercise. Implement the failing tests
by maxisam
HTML
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.js"></script>
<script src="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine-html.js"></script>
<link rel="stylesheet" href="http://pivotal.github.io/jasmine/lib/jasmine-1.3.1/jasmine.css">
<script src="http://code.angularjs.org/1.0.5/angular.js"></script>
<script src="http://code.angularjs.org/1.0.5/angular-mocks.js"></script>
JavaScript
//--- CODE --------------------------
var myApp = angular.module('myApp', []);
myApp.service('mySrv', [function () {
// Converts name from camelCase to snake-case
this.camelToSnakeCase = function (camelCase) {
return camelCase.replace(/[A-Z]/g, function (match, pos) {
return (pos > 0 ? '-' : '') + match.toLowerCase();
});
}
}]);
myApp.controller('myCtrl', ['$scope', 'mySrv', function ($scope, mySrv) {
$scope.name = 'angular-js';
$scope.setName = function (name) {
$scope.name = mySrv.camelToSnakeCase(name);
};
}]);
myApp.directive('myDrtv', function () {
return {
restrict: 'E',
scope: {
name: '='
},
// Better to externalize to templateUrl, this is for demonstration sake
template: '<div>Hello {{name}}</div>',
replace: false
};
});
// ---SPECS-------------------------
describe('mySrv', function () {
var service;
beforeEach(function () {
module('myApp');
inject(function (mySrv){
service = mySrv;
});
});
it('transforms camelCase to snake-case', function () {
expect(service.camelToSnakeCase("testTest")).toEqual("test-test");
});
it('retains does not change snake-case input', function () {
expect(service.camelToSnakeCase("test-test")).toEqual("test-test");
});
it('does not add a dash to a work starting with upper case char', function () {
expect(service.camelToSnakeCase("TestTest")).toEqual("test-test");
});
});
describe('myCtrl', function () {
var controller,
mySrvSpy,
scope;
beforeEach(function () {
module('myApp');
inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('myCtrl', {
'$scope': scope
});
});
});
it('sets a deafult name', function () {
...