From 671d498cdb2acc945552ca5d6c8d493644303228 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 26 Aug 2015 16:58:36 +0200 Subject: [PATCH] showcase: REST service cars: get remote values --- .../src/showcase/app/rest/rest.constants.js | 2 +- .../src/showcase/app/rest/rest.controller.js | 9 ++++- .../src/showcase/app/rest/rest.controller.spec.js | 40 ++++++++++++++++++++++ angularjs/showcase/src/showcase/app/rest/rest.html | 13 ++++--- .../showcase/src/showcase/app/rest/rest.service.js | 2 +- 5 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js diff --git a/angularjs/showcase/src/showcase/app/rest/rest.constants.js b/angularjs/showcase/src/showcase/app/rest/rest.constants.js index a82540f..0a8591c 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.constants.js +++ b/angularjs/showcase/src/showcase/app/rest/rest.constants.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - var prefix = 'http://localhost:8080/'; + var prefix = '/'; angular .module('app.rest') diff --git a/angularjs/showcase/src/showcase/app/rest/rest.controller.js b/angularjs/showcase/src/showcase/app/rest/rest.controller.js index 66f6956..5e34c09 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.controller.js +++ b/angularjs/showcase/src/showcase/app/rest/rest.controller.js @@ -10,6 +10,7 @@ * @name app.rest.controller:Rest * * @requires $location + * @requires app.rest.cars * *

*
@@ -20,13 +21,19 @@ * Rest controller. */ /* @ngInject */ - function Rest($location) { + function Rest($location, cars) { var vm = this; vm.example = { text: 'try to send data', word: /^\s*\w*\s*$/, singleModel: 1 }; + vm.getCars = getCars; + vm.cars = undefined; + + function getCars() { + vm.cars = cars.getAll(); + } } })(); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js b/angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js new file mode 100644 index 0000000..62f1b3a --- /dev/null +++ b/angularjs/showcase/src/showcase/app/rest/rest.controller.spec.js @@ -0,0 +1,40 @@ +describe('app.rest', function() { + 'use strict'; + + var Rest; + var cars = { + getAll: function() { + return {}; + } + }; + var $q; + + beforeEach(function() { + module('app.rest'); + + inject(function($controller, $location, _$q_) { + Rest = $controller('Rest', { + $location: $location, + cars: cars + }); + $q = _$q_; + }); + }); + + describe('Rest controller', function () { + + it('should invoke GET all cars in service', function () { + + spyOn(cars, 'getAll') + .and.callFake(function() { + var deferred = $q.defer(); + return deferred.promise; + }); + + Rest.getCars(); + + expect(cars.getAll).toHaveBeenCalled(); + }); + }); + +}); diff --git a/angularjs/showcase/src/showcase/app/rest/rest.html b/angularjs/showcase/src/showcase/app/rest/rest.html index c34fdb4..47d2136 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.html +++ b/angularjs/showcase/src/showcase/app/rest/rest.html @@ -16,12 +16,15 @@ myForm.input.$error = {{myForm.input.$error}}
myForm.$valid = {{myForm.$valid}}
myForm.$error.required = {{!!myForm.$error.required}}
- +

+ - - - - \ No newline at end of file diff --git a/angularjs/showcase/src/showcase/app/rest/rest.service.js b/angularjs/showcase/src/showcase/app/rest/rest.service.js index 618eb82..ca5aa43 100644 --- a/angularjs/showcase/src/showcase/app/rest/rest.service.js +++ b/angularjs/showcase/src/showcase/app/rest/rest.service.js @@ -41,7 +41,7 @@ .catch(getAllFailed); function getAllCompleted(response) { - return response.data.results; + return response.data; } function getAllFailed(error) { -- 2.1.4