From 97dc661d906b5bdcbf3a1d4b96795756e894edb3 Mon Sep 17 00:00:00 2001 From: Gavin Davies Date: Thu, 12 Nov 2015 19:32:39 +0000 Subject: [PATCH] Increase test coverage to 95% --- Gulpfile.js | 28 ++++++++++++ package.json | 1 + spec/modelSpec.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) diff --git a/Gulpfile.js b/Gulpfile.js index 9ee0538..0b8c647 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -7,6 +7,7 @@ var jshint = require('gulp-jshint'); var jscs = require('gulp-jscs'); var nsp = require('gulp-nsp'); var runSequence = require('run-sequence'); +var istanbul = require('gulp-istanbul'); /* * PLEASE NOTE: run-sequence is a @@ -32,6 +33,33 @@ gulp.task('coveralls', function() { .pipe(coveralls()); }); +// Task that calculates the unit test coverage for the module +gulp.task('coverage', function(cb) { + new Server({ + configFile: __dirname + '/config/karma.conf.js', + singleRun: true, + reporters: ['progress', 'coverage'], + + preprocessors: { + 'src/**/*.js':['coverage'] + }, + + // optionally, configure the reporter + coverageReporter: { + reporters: [ + { + type : 'html', + dir : 'build/coverage/' + }, + { + type: 'text' + } + ] + } + }, cb).start(); +}); + + gulp.task('lint', function() { return gulp.src([paths.src]) .pipe(jshint()) diff --git a/package.json b/package.json index 8e17dd4..24b3f9c 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "gulp": "^3.9.0", "gulp-coveralls": "^0.1.4", + "gulp-istanbul": "^0.10.2", "gulp-jscs": "^3.0.2", "gulp-jshint": "^1.12.0", "gulp-nsp": "^2.0.1", diff --git a/spec/modelSpec.js b/spec/modelSpec.js index 857e379..928d36d 100644 --- a/spec/modelSpec.js +++ b/spec/modelSpec.js @@ -677,6 +677,20 @@ describe('model', function() { })); }); + describe('$delete()', function() { + it('erases the object form the API', inject(function(model) { + + var selfurl = 'http://api/users/5'; + var user = model('Users').create({$links: {self: {href: selfurl}}}); + + $httpBackend.expectDELETE(selfurl).respond(204); + + user.$delete(); + + $httpBackend.flush(); + })); + }); + describe('syncing', function() { it('should correctly update local copy on successful save', inject(function(model) { var message = model('Messages').create({ @@ -704,5 +718,99 @@ describe('model', function() { }); })); }); + + describe('$collection', function() { + beforeEach(function() { + var data = [ + {name: 'First Project', $links: {self: {href: 'http://api/projects/1138'}}}, + {name: 'Second Project', $links: {self: {href: 'http://api/projects/1139'}}} + ]; + $httpBackend.expectGET('http://api/projects').respond(200, JSON.stringify(data)); + }); + + describe('add()', function() { + it('adds a new model instance to the API', inject(function(model) { + $httpBackend.expectPOST('http://api/projects/1140').respond(201); + + model('Projects').all().then(function(result) { + var data = {name: 'Third Project', $links: {self: {href: 'http://api/projects/1140'}}}; + var newItem = model('Projects').create({}); + result.add(newItem, data); + }); + $httpBackend.flush(); + })); + }); + + describe('remove()', function() { + it('removes items from the API and from the cache', inject(function(model) { + $httpBackend.expectDELETE('http://api/projects/1138').respond(204); + + model('Projects').all().then(function(result) { + expect(result.length).toEqual(2); + result.remove(0).then(function() { + expect(result.length).toEqual(1); + }); + }); + $httpBackend.flush(); + })); + }); + }); + }); + + describe('directive', function() { + + var $httpBackend; + + beforeEach(inject(function ($injector) { + $httpBackend = $injector.get('$httpBackend'); + })); + + afterEach(function () { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + + describe('link directive', function() { + var elm, scope; + + describe('when a relation, resource and href are provided', function() { + beforeEach(inject(function($rootScope, $compile) { + elm = angular.element(''); + + scope = $rootScope; + + $compile(elm)(scope); + scope.$digest(); + })); + + it("assigns the model's URL from the href attribute", inject(function(model) { + var message = model('Messages').create({ + content: 'Hello!' + }); + + message.$save(); + + $httpBackend.expectPOST('/api-of-your-system/Messages').respond(201); + $httpBackend.flush(); + })); + }); + + describe('when rel !== resource', function() { + beforeEach(inject(function($rootScope, $compile) { + elm = angular.element(''); + + scope = $rootScope; + + $compile(elm)(scope); + scope.$digest(); + })); + + it("is ignored", inject(function(model) { + expect(model('Messages')).toBeUndefined(); + })); + }); + + }); }); + });