Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add karma for testing

  • Loading branch information...
commit 0e4ce9e9e91945cb8c3a8913c4968e9ded245247 1 parent 7952b96
@mrako authored
View
18 Gruntfile.js
@@ -22,6 +22,11 @@ module.exports = function(grunt) {
}
}
},
+ open: {
+ server: {
+ url: 'http://localhost:<%= express.options.port %>'
+ }
+ },
jshint: {
options: {
jshintrc: '.jshintrc'
@@ -32,18 +37,21 @@ module.exports = function(grunt) {
'public/js/**/*.js'
]
},
- open: {
- server: {
- url: 'http://localhost:<%= express.options.port %>'
+ karma: {
+ unit: {
+ configFile: 'karma.conf.js',
+ singleRun: true
}
}
});
grunt.registerTask('server', ['express:dev', 'open', 'watch']);
- grunt.registerTask('default', ['server']);
+ grunt.registerTask('default', ['jshint', 'karma']);
grunt.loadNpmTasks('grunt-express-server');
grunt.loadNpmTasks('grunt-contrib-watch');
- grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-open');
+
+ grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-karma');
};
View
6 bower.json
@@ -2,9 +2,13 @@
"name": "todo-app",
"version": "0.0.0",
"dependencies": {
- "angular": "1.0.8",
+ "angular": "1.2.18",
+ "angular-route": "latest",
"angular-xeditable": "latest",
"bootstrap": "latest",
"fontawesome": "latest"
+ },
+ "devDependencies": {
+ "angular-mocks": "latest"
}
}
View
56 karma.conf.js
@@ -0,0 +1,56 @@
+// Karma configuration
+// http://karma-runner.github.io/0.10/config/configuration-file.html
+
+module.exports = function(config) {
+ config.set({
+ // base path, that will be used to resolve files and exclude
+ basePath: '',
+
+ // testing framework to use (jasmine/mocha/qunit/...)
+ frameworks: ['jasmine'],
+
+ // list of files / patterns to load in the browser
+ files: [
+ 'public/components/angular/angular.js',
+ 'public/components/angular-route/angular-route.js',
+ 'public/components/angular-xeditable/dist/js/xeditable.js',
+ 'public/components/angular-route/angular-route.js',
+ 'public/components/angular-mocks/angular-mocks.js',
+ 'public/js/app.js',
+ 'public/js/services/todos_service.js',
+ 'public/js/controllers/todos_controller.js',
+ 'test/spec/**/*.js'
+ ],
+
+ // list of files / patterns to exclude
+ exclude: [],
+
+ // web server port
+ port: 9001,
+
+ // level of logging
+ // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
+ logLevel: config.LOG_INFO,
+
+ reporters : ['spec'],
+
+ // enable / disable watching file and executing tests whenever any file changes
+ autoWatch: false,
+
+
+ // Start these browsers, currently available:
+ // - Chrome
+ // - ChromeCanary
+ // - Firefox
+ // - Opera
+ // - Safari (only Mac)
+ // - PhantomJS
+ // - IE (only Windows)
+ browsers: ['PhantomJS'],
+
+
+ // Continuous Integration mode
+ // if true, it capture browsers, run tests and exit
+ singleRun: false
+ });
+};
View
8 package.json
@@ -23,6 +23,12 @@
"grunt-express-server": "latest",
"grunt-contrib-watch": "latest",
"grunt-contrib-jshint": "latest",
- "grunt-open": "latest"
+ "grunt-open": "latest",
+
+ "karma": "latest",
+ "grunt-karma": "latest",
+ "karma-spec-reporter": "latest",
+ "karma-jasmine": "latest",
+ "karma-phantomjs-launcher": "latest"
}
}
View
6 public/index.html
@@ -34,11 +34,11 @@ <h3 class="text-muted">Todo App</h3>
<script src="/components/angular/angular.min.js"></script>
-
+ <script src="/components/angular-route/angular-route.min.js"></script>
<script src="/components/angular-xeditable/dist/js/xeditable.js"></script>
<script src="/js/app.js"></script>
- <script src="/js/controllers/todo_controller.js"></script>
- <script src="/js/services/todo_factory.js"></script>
+ <script src="/js/controllers/todos_controller.js"></script>
+ <script src="/js/services/todos_service.js"></script>
</body>
</html>
View
2  public/js/app.js
@@ -1,6 +1,6 @@
'use strict';
-var todoApp = angular.module('todoApp', ['xeditable']);
+var todoApp = angular.module('todoApp', ['ngRoute', 'xeditable']);
todoApp.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
View
14 public/js/controllers/todo_controller.js → public/js/controllers/todos_controller.js
@@ -1,12 +1,12 @@
'use strict';
-angular.module('todoApp').controller('TodoController', function($scope, $http, todoService) {
+angular.module('todoApp').controller('TodosController', function($scope, $http, todosService) {
$scope.formData = {};
$scope.submit = 'Create';
$scope.loading = true;
// GET =====================================================================
- todoService.list()
+ todosService.list()
.success(function(data) {
$scope.todos = data;
$scope.loading = false;
@@ -17,12 +17,12 @@ angular.module('todoApp').controller('TodoController', function($scope, $http, t
$scope.loading = true;
if ($scope.formData._id === undefined) {
- todoService.create($scope.formData)
+ todosService.create($scope.formData)
.success(function(data) {
$scope.todos = data;
});
} else {
- todoService.update($scope.formData._id, $scope.formData)
+ todosService.update($scope.formData._id, $scope.formData)
.success(function(data) {
$scope.todos = data;
});
@@ -35,7 +35,7 @@ angular.module('todoApp').controller('TodoController', function($scope, $http, t
// CREATE ==================================================================
$scope.edit = function (id) {
- todoService.get(id)
+ todosService.get(id)
.success(function(data) {
$scope.formData = data;
$scope.submit = 'Save';
@@ -51,7 +51,7 @@ angular.module('todoApp').controller('TodoController', function($scope, $http, t
$scope.updateData = function(todo, data) {
$scope.loading = true;
- todoService.update(todo._id, {done: todo.done, text: data})
+ todosService.update(todo._id, {done: todo.done, text: data})
.success(function(data) {
$scope.todos = data;
});
@@ -61,7 +61,7 @@ angular.module('todoApp').controller('TodoController', function($scope, $http, t
$scope.delete = function(id) {
$scope.loading = true;
- todoService.delete(id)
+ todosService.delete(id)
.success(function(data) {
$scope.loading = false;
$scope.todos = data;
View
2  public/js/services/todo_factory.js → public/js/services/todos_service.js
@@ -1,7 +1,7 @@
'use strict';
// SERVICE FOR API CALLS =======================================================
-angular.module('todoApp').factory('todoService', function ($http) {
+angular.module('todoApp').factory('todosService', function ($http) {
return {
list : function() {
return $http.get('/api/todos');
View
4 public/templates/todos.html
@@ -1,4 +1,4 @@
-<div ng-controller="TodoController">
+<div ng-controller="TodosController">
<div class="jumbotron">
<h3>You currently have <ng-pluralize count="todos.length" when="{'0': 'no todos', '1': '1 todo', 'other': '{} todos'}" /></h3>
@@ -28,7 +28,7 @@
<tbody>
<tr ng-repeat="todo in todos">
<td class="col-md-1">
- <input type="checkbox" ng-model="todo.done" ng-click="update(todo)"/>
+ <input type="checkbox" ng-model="todo.done" ng-change="update(todo)"/>
</td>
<td class="col-md-10">
<a href="#" editable-text="todo.text" onbeforesave="updateData(todo, $data)">{{ todo.text || "empty" }}</a>
View
30 test/spec/controllers/todos_controller.js
@@ -0,0 +1,30 @@
+'use strict';
+
+describe('Controller: TodosController', function () {
+
+ var TodosController,
+ scope,
+ todosService,
+ todos = [{_id: 1, text: "todo item", done: false}];
+
+ beforeEach(inject(function ($controller, $rootScope) {
+ scope = $rootScope.$new();
+
+ todosService = {
+ list: jasmine.createSpy('list').andReturn({success: function(fn) {fn(todos);}})
+ };
+ TodosController = $controller('TodosController', {
+ $scope: scope,
+ todosService: todosService
+ });
+ }));
+
+ it('gets todos from the todos service when attached', function () {
+ expect(todosService.list).toHaveBeenCalled();
+ });
+
+ it('attaches items to scope when promise is resolved', function () {
+ expect(scope.todos).toBe(todos);
+ });
+
+});
View
1  test/spec/spec_helper.js
@@ -0,0 +1 @@
+beforeEach(module('todoApp'));
Please sign in to comment.
Something went wrong with that request. Please try again.