Skip to content

Commit

Permalink
A starting point for test-driving the implementation of javascript se…
Browse files Browse the repository at this point in the history
…rvices that will replace the results of my previous spike.
  • Loading branch information
jan-molak committed Jul 9, 2013
1 parent e4c1f3c commit 1e6583a
Show file tree
Hide file tree
Showing 10 changed files with 6,248 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"scripts": {
"test": "node_modules/karma/bin/karma start src/test/resources/karma-unit.conf.js --auto-watch"
"test": "node_modules/karma/bin/karma start src/test/resources/karma.conf.js --auto-watch"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</execution>
</executions>
<configuration>
<configFile>src/test/resources/karma-unit.conf.js</configFile>
<configFile>src/test/resources/karma.conf.js</configFile>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@

angular.
module('buildMonitor.services').
service('jenkins', function() {
this.proxy = <st:bind value="${it}"/>;
this.buildMonitorName = '${it.displayName}';
});
value('jenkinsProxy', <st:bind value="${it}"/>).
value('buildMonitorName', '${it.displayName}');
</script>
<script src="${resourcesURL}/scripts/controllers.js"></script>
</l:main-panel>
Expand Down
13 changes: 8 additions & 5 deletions src/main/webapp/scripts/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ angular.module('buildMonitor.services', ['ui.bootstrap.dialog', 'template/dialog
}
}).

service('fetch',function ($q, $rootScope, jenkins) {
service('fetch',function ($q, $rootScope, jenkinsProxy) {
this.current = function () {
var deferred = $q.defer();

jenkins.proxy.fetchJobViews(function (response) {
jenkinsProxy.fetchJobViews(function (response) {

if (response.status === 200) {
deferred.resolve(response.responseObject());
} else {
// todo remove the below debug once proper error handling has been implemented
console.error('RESPONSE RECEIVED', response.status);
deferred.reject({ status: response.status });
}

Expand All @@ -39,7 +41,7 @@ angular.module('buildMonitor.services', ['ui.bootstrap.dialog', 'template/dialog
}
}).

service('storage',function ($cookies, jenkins, hashCode) {
service('storage',function ($cookies, buildMonitorName, hashCode) {
this.persist = function (name, value) {
$cookies[prefix(name)] = value;
}
Expand All @@ -53,13 +55,14 @@ angular.module('buildMonitor.services', ['ui.bootstrap.dialog', 'template/dialog
}

function prefix(name) {
return 'buildMonitor.' + hashCode.of(jenkins.buildMonitorName) + '.' + name;
return 'buildMonitor.' + hashCode.of(buildMonitorName) + '.' + name;
}
}).

service('hashCode', function() {
this.of = function(name) {
var hash = 0,
var name = name || '',
hash = 0,
char;

if (name.length == 0) {
Expand Down
6 changes: 0 additions & 6 deletions src/test/javascript/unit/KarmaSpec.js

This file was deleted.

24 changes: 24 additions & 0 deletions src/test/javascript/unit/services/hashCodeSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

describe('buildMonitor', function () {
describe('buildMonitor.services', function () {
describe('hashCode', function () {

beforeEach(module('buildMonitor.services'));

it('produces a hash code uniquely identifying a string of text', inject(function (hashCode) {
expect(hashCode.of('')).toEqual(0);
expect(hashCode.of('name')).toEqual(3373707)
expect(hashCode.of('Name')).toEqual(2420395);
}));

it('should treat "undefined" the same way as empty string', inject(function (hashCode) {
expect(hashCode.of()).toEqual(hashCode.of(''));
}));

it('should treat "null" the same way as empty string', inject(function (hashCode) {
expect(hashCode.of(null)).toEqual(hashCode.of(''));
}));
});
});
});
28 changes: 28 additions & 0 deletions src/test/javascript/unit/services/storageSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

describe('buildMonitor', function () {
describe('buildMonitor.services', function () {
describe('storage', function () {

beforeEach(module('buildMonitor.services', function($provide) {
$provide.value('buildMonitorName', 'Example Build Monitor Name');
$provide.service('hashCode', serviceStub({ of : 'hashCode'}));
}));

it('persist values using a build monitor-specific cookie', inject(function (storage, $cookies) {
storage.persist('numberOfColumns', 3);

expect($cookies).toEqual({ 'buildMonitor.hashCode.numberOfColumns' : 3 });
}));


function serviceStub( methodAndReturnValuePairs ) {
return function() {
for (var method in methodAndReturnValuePairs) {
this[method] = function() { return methodAndReturnValuePairs[method]; }
}
}
}
});
});
});
Loading

0 comments on commit 1e6583a

Please sign in to comment.