JSCS Cleanup - Use mock module to avoid using window

The specs now use common mocks to share functions for tests
instead of using the global window object

Change-Id: Ifc8f7717fcacf2ddf68997408b8e0d33e530c795
Partially-Implements: blueprint jscs-cleanup
This commit is contained in:
Nathan Zeplowitz 2015-07-07 15:34:44 -07:00 committed by Kevin Fox
parent b808dcf2f7
commit a898da92eb
9 changed files with 109 additions and 74 deletions

View File

@ -18,15 +18,18 @@
'use strict';
describe('Cinder API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.cinder', function(cinderAPI) {
service = cinderAPI;
@ -67,9 +70,10 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
});
})();

View File

@ -17,29 +17,48 @@
(function() {
'use strict';
/* This function tests the 'typical' way that apiService calls are made.
Look at this typical approach:
angular
.module('horizon.mock.openstack-service-api', [])
.constant('initServices', initServices);
this.getVolumes = function(params) {
var config = (params) ? {'params': params} : {};
return apiService.get('/api/cinder/volumes/', config)
.error(function () {
toastService.add('error', gettext('Unable to retrieve the volumes.'));
});
function initServices($provide, apiService, toastService) {
angular.extend(apiService, { get: angular.noop,
post: angular.noop,
put: angular.noop,
patch: angular.noop,
delete: angular.noop });
angular.extend(toastService, { add: angular.noop });
$provide.value('horizon.framework.util.http.service', apiService);
$provide.value('horizon.framework.widgets.toast.service', toastService);
In this case there is an apiService call that is made with one or two
arguments, and on error a function is called that invokes the
toastService's add method.
return testCall;
}
The function below takes in the set of variables, both input and output,
that are required to test the above code. It spies on the apiService
method that is called, and also spies on its returned simulated promise
'error' method.
/*
This function tests the 'typical' way that apiService calls are made.
Look at this typical approach:
Having established those spies, the code then inspects the parameters
passed to the apiService, as well as the results of those methods.
Then the code invokes of function passed to the error handler, and
ensures that the toastService is called with the appropriate parameters.
this.getVolumes = function(params) {
var config = (params) ? {'params': params} : {};
return apiService.get('/api/cinder/volumes/', config)
.error(function () {
toastService.add('error', gettext('Unable to retrieve the volumes.'));
});
}
In this case there is an apiService call that is made with one or two
arguments, and on error a function is called that invokes the
toastService's add method.
The function below takes in the set of variables, both input and output,
that are required to test the above code. It spies on the apiService
method that is called, and also spies on its returned simulated promise
'error' method.
Having established those spies, the code then inspects the parameters
passed to the apiService, as well as the results of those methods.
Then the code invokes of function passed to the error handler, and
ensures that the toastService is called with the appropriate parameters.
*/
function testCall(apiService, service, toastService, config) {
// 'promise' simulates a promise, including a self-referential success
@ -67,17 +86,4 @@
expect(toastService.add).toHaveBeenCalledWith(config.messageType || 'error', config.error);
}
function initServices ($provide, apiService, toastService) {
angular.extend(apiService, { get: angular.noop,
post: angular.noop,
put: angular.noop,
patch: angular.noop,
delete: angular.noop });
angular.extend(toastService, { add: angular.noop });
$provide.value('horizon.framework.util.http.service', apiService);
$provide.value('horizon.framework.widgets.toast.service', toastService);
}
window.apiTest = { testCall: testCall, initServices: initServices };
})();

View File

@ -18,15 +18,18 @@
'use strict';
describe('Glance API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.glance', function(glanceAPI) {
service = glanceAPI;
@ -97,7 +100,7 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
@ -107,4 +110,5 @@
});
});
})();

View File

@ -18,15 +18,18 @@
'use strict';
describe('Keystone API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.keystone', function(keystoneAPI) {
service = keystoneAPI;
@ -382,7 +385,7 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
@ -560,4 +563,5 @@
});
});
})();

View File

@ -18,15 +18,18 @@
'use strict';
describe('Neutron API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.neutron', function(neutronAPI) {
service = neutronAPI;
@ -91,7 +94,7 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});

View File

@ -18,15 +18,18 @@
'use strict';
describe('Nova API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.nova', function(novaAPI) {
service = novaAPI;
@ -211,7 +214,7 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
@ -311,4 +314,5 @@
expect(factory.ifEnabled).toBeDefined();
});
});
})();

View File

@ -18,15 +18,18 @@
'use strict';
describe('Policy API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.policy', function(policyAPI) {
service = policyAPI;
@ -54,9 +57,10 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
});
})();

View File

@ -18,15 +18,18 @@
'use strict';
describe('Security Group API', function() {
var service;
var testCall, service;
var apiService = {};
var toastService = {};
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(
module('horizon.mock.openstack-service-api',
function($provide, initServices) {
testCall = initServices($provide, apiService, toastService);
})
);
beforeEach(module(function($provide) {
window.apiTest.initServices($provide, apiService, toastService);
}));
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(inject(['horizon.app.core.openstack-service-api.security-group', function(securityGroup) {
service = securityGroup;
@ -51,9 +54,10 @@
angular.forEach(tests, function(params) {
it('defines the ' + params.func + ' call properly', function() {
var callParams = [apiService, service, toastService, params];
window.apiTest.testCall.apply(this, callParams);
testCall.apply(this, callParams);
});
});
});
})();

View File

@ -36,7 +36,9 @@
var settingsService;
beforeEach(module('horizon.app.core.openstack-service-api'));
beforeEach(module('horizon.framework.util.http'));
beforeEach(inject(function (_$httpBackend_, $injector) {
responseMockOpts.succeed = true;
settingsService = $injector.get('horizon.app.core.openstack-service-api.settings');