From 65112208190e6eaded0999e8ca5d2af963ce9ae9 Mon Sep 17 00:00:00 2001 From: Kelly Domico Date: Mon, 29 Feb 2016 18:28:50 -0800 Subject: [PATCH] Add unit tests for service instance API and model --- .../serviceInstance.api.spec.js | 64 +++++++++++ .../serviceInstance.model.spec.js | 100 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/app/api/serviceInstance/serviceInstance.api.spec.js create mode 100644 src/app/model/serviceInstance/serviceInstance.model.spec.js diff --git a/src/app/api/serviceInstance/serviceInstance.api.spec.js b/src/app/api/serviceInstance/serviceInstance.api.spec.js new file mode 100644 index 0000000000..aeea74d7cd --- /dev/null +++ b/src/app/api/serviceInstance/serviceInstance.api.spec.js @@ -0,0 +1,64 @@ +(function () { + 'use strict'; + + describe('service instance API', function () { + var $httpBackend, serviceInstanceApi; + + beforeEach(module('green-box-console')); + beforeEach(inject(function ($injector) { + $httpBackend = $injector.get('$httpBackend'); + + var apiManager = $injector.get('app.api.apiManager'); + serviceInstanceApi = apiManager.retrieve('app.api.serviceInstance'); + })); + + afterEach(function () { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + + it('should be defined', function () { + expect(serviceInstanceApi).toBeDefined(); + }); + + it('should have `$http` property defined', function () { + expect(serviceInstanceApi.$http).toBeDefined(); + }); + + it('should return service instances for specified user', function () { + var data = { + items: ['x','y','z'] + }; + $httpBackend.when('GET', '/api/service-instances?username=dev').respond(200, data); + + serviceInstanceApi.list('dev').then(function (response) { + expect(response.data).toEqual({items: ['x','y','z']}); + }); + + $httpBackend.flush(); + }); + + it('should send POST request for register', function () { + var data = { + username: 'user', + name: 'service', + service_user: 'username', + service_password: 'password' + }; + $httpBackend.expectPOST('/api/service-instances/register', data).respond(200, ''); + serviceInstanceApi.register('user', 'service', 'username', 'password'); + $httpBackend.flush(); + }); + + it('should send POST request for unregister', function () { + var data = { + username: 'user', + name: 'service' + }; + $httpBackend.expectPOST('/api/service-instances/unregister', data).respond(200, ''); + serviceInstanceApi.unregister('user', 'service'); + $httpBackend.flush(); + }); + }); + +})(); diff --git a/src/app/model/serviceInstance/serviceInstance.model.spec.js b/src/app/model/serviceInstance/serviceInstance.model.spec.js new file mode 100644 index 0000000000..80e4979dff --- /dev/null +++ b/src/app/model/serviceInstance/serviceInstance.model.spec.js @@ -0,0 +1,100 @@ +(function () { + 'use strict'; + + describe('service instance model', function () { + var $httpBackend, serviceInstance, mockData; + + beforeEach(module('green-box-console')); + beforeEach(inject(function ($injector) { + $httpBackend = $injector.get('$httpBackend'); + + var modelManager = $injector.get('app.model.modelManager'); + var account = modelManager.retrieve('app.model.account'); + account.username = 'dev'; + serviceInstance = modelManager.retrieve('app.model.serviceInstance'); + + mockData = { + items: [ + { name: 'cluster1', url:' cluster1_url' }, + { name: 'cluster2', url:' cluster2_url' } + ] + }; + })); + + afterEach(function () { + $httpBackend.verifyNoOutstandingExpectation(); + $httpBackend.verifyNoOutstandingRequest(); + }); + + it('should be defined', function () { + expect(serviceInstance).toBeDefined(); + }); + + it('should have initial properties defined', function () { + expect(serviceInstance.apiManager).toBeDefined(); + expect(serviceInstance.account).toBeDefined(); + expect(serviceInstance.serviceInstanceApi).toBeDefined(); + expect(serviceInstance.serviceInstances).toEqual([]); + expect(serviceInstance.numRegistered).toBe(0); + }); + + it('should set `serviceInstances` on list()', function () { + var expectedData = [ + { name: 'cluster1', url:' cluster1_url' }, + { name: 'cluster2', url:' cluster2_url' } + ]; + + $httpBackend.when('GET', '/api/service-instances?username=dev') + .respond(200, mockData); + + serviceInstance.list().then(function (response) { + expect(response).toEqual({ serviceInstances: expectedData, numRegistered: 0 }); + expect(serviceInstance.serviceInstances).toEqual(expectedData); + expect(serviceInstance.numRegistered).toBe(0); + }); + + $httpBackend.flush(); + }); + + it('should not set `serviceInstances` on list() and error', function () { + $httpBackend.when('GET', '/api/service-instances?username=dev') + .respond(403, {}); + + serviceInstance.list().then(function () {}, function (error) { + expect(error.status).toBe(403); + expect(error.data).toEqual({}); + expect(serviceInstance.serviceInstances).toEqual([]); + expect(serviceInstance.numRegistered).toBe(0); + }); + + $httpBackend.flush(); + }); + + it('should POST correct data on register()', function () { + var data = { + username: 'dev', + name: 'service', + service_user: 'username', + service_password: 'password' + }; + $httpBackend.expectPOST('/api/service-instances/register', data).respond(200, ''); + + serviceInstance.register('service', 'username', 'password'); + + $httpBackend.flush(); + }); + + it('should POST correct data on unregister()', function () { + var data = { + username: 'dev', + name: 'service' + }; + $httpBackend.expectPOST('/api/service-instances/unregister', data).respond(200, ''); + + serviceInstance.unregister('service'); + + $httpBackend.flush(); + }); + }); + +})();