diff --git a/src/lib/ng-noosfero-api/http/restangular_service.spec.ts b/src/lib/ng-noosfero-api/http/restangular_service.spec.ts index fc788cf..c44fd29 100644 --- a/src/lib/ng-noosfero-api/http/restangular_service.spec.ts +++ b/src/lib/ng-noosfero-api/http/restangular_service.spec.ts @@ -1,10 +1,18 @@ import {RestangularService} from "./restangular_service"; interface ObjectModel extends noosfero.RestModel { +} + +interface RootObjectModel extends noosfero.RestModel { } -describe("Restangular Service", () => { + + + +describe("Restangular Service - base Class", () => { + + class ObjectRestService extends RestangularService { public getDataKeys() { @@ -19,9 +27,23 @@ describe("Restangular Service", () => { } } + class RootObjectRestService extends RestangularService { + public getDataKeys() { + return { + singular: "rootObject", + plural: "rootObjects" + } + } + + public getResourcePath() { + return "rootObjects"; + } + } + let restangularService: restangular.IService; let $httpBackend: ng.IHttpBackendService; let objectRestService: ObjectRestService; + let rootObjectRestService: RootObjectRestService; beforeEach(angular.mock.module("noosferoApp", ($translateProvider: angular.translate.ITranslateProvider) => { $translateProvider.translations('en', {}); @@ -29,50 +51,169 @@ describe("Restangular Service", () => { beforeEach(inject((_Restangular_: restangular.IService, _$q_: ng.IQService, _$httpBackend_: ng.IHttpBackendService) => { restangularService = _Restangular_; - objectRestService = new ObjectRestService(_Restangular_, _$q_, console); + objectRestService = new ObjectRestService(_Restangular_, _$q_, null); + rootObjectRestService = new RootObjectRestService(_Restangular_, _$q_, null); $httpBackend = _$httpBackend_; })); - - it("calls GET /objects", (done) => { + it("list() calls GET /objects", (done) => { $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); objectRestService.list().then((result: noosfero.RestResult) => { - console.log(result); expect(result.data).toBeDefined(); expect((result.data).length).toEqual(2); done(); }); $httpBackend.flush(); + + $httpBackend.verifyNoOutstandingExpectation(); }); - it("calls GET /objects", (done) => { - $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); + it("list(rootObject) calls GET /rootObjects/1/objects", (done) => { - objectRestService.list().then((result: noosfero.RestResult) => { - console.log(result); + $httpBackend.expectGET("/api/v1/rootObjects/1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); + + objectRestService.list(rootObj).then((result: noosfero.RestResult) => { expect(result.data).toBeDefined(); - console.log("HERE", (result.data).length); expect((result.data).length).toEqual(2); + done(); }); + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); - setTimeout(() => { - $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 3 }] }); + it("get(1) calls GET /objects/1", (done) => { + $httpBackend.expectGET("/api/v1/objects/1").respond(200, { object: { id: 1 } }); - objectRestService.list().then((result: noosfero.RestResult) => { - console.log(result); + objectRestService.get(1).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).id).toEqual(1); + done(); + }); + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + it("objectService.get(1, rootObject) calls GET /rootObjects/1/objects/1", (done) => { + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); + $httpBackend.expectGET("/api/v1/rootObjects/1/objects/1").respond(200, { object: { id: 1 } }); + + objectRestService.get(1, rootObj).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).id).toEqual(1); + done(); + }); + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + it("remove(object) calls DELETE /objects/1", (done) => { + $httpBackend.expectGET("/api/v1/objects/1").respond(200, { object: { id: 1 } }); + objectRestService.get(1).then((result: noosfero.RestResult) => { + let object: ObjectModel = result.data; + + $httpBackend.expectDELETE("/api/v1/objects/1").respond(204, { object: { id: 1 } }); + + objectRestService.remove(object).then((result: noosfero.RestResult) => { expect(result.data).toBeDefined(); - console.log("HERE 2", (result.data).length); - expect((result.data).length).toEqual(2); + expect((result.data).id).toEqual(1); done(); }); - $httpBackend.flush(); - }, 2000); + }); + + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + + it("remove(object, rootObject) calls DELETE /rootObjects/1/objects/1", (done) => { + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); + + let obj: ObjectModel = objectRestService.getElement(1, rootObj); + + $httpBackend.expectDELETE("/api/v1/rootObjects/1/objects/1").respond(204, { object: { id: 1, rootId: 1 } }); + objectRestService.remove(obj, rootObj).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).id).toEqual(1); + expect((result.data).rootId).toEqual(1); + done(); + }); + $httpBackend.flush(); + }); + + + it("update(object) calls PUT /objects/1", (done) => { + $httpBackend.expectPUT("/api/v1/objects/1").respond(200, { object: { id: 1 } }); + + let object: ObjectModel = objectRestService.getElement(1); + + objectRestService.update(object).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).id).toEqual(1); + done(); + }); + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + + it("update(object, rootObject) calls PUT /rootObjects/1/objects/1", (done) => { + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); + + let obj: ObjectModel = objectRestService.getElement(1, rootObj); + + $httpBackend.expectPUT("/api/v1/rootObjects/1/objects/1").respond(200, { object: { id: 1, rootId: 1 } }); + objectRestService.update(obj, rootObj).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).id).toEqual(1); + expect((result.data).rootId).toEqual(1); + done(); + }); + $httpBackend.flush(); }); + + + it("save(object) calls POST /objects", (done) => { + $httpBackend.expectPOST("/api/v1/objects").respond(201, { object: { attr: 1 } }); + + let object: ObjectModel = objectRestService.getElement(1); + + objectRestService.create(object).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).attr).toEqual(1); + done(); + }); + + $httpBackend.flush(); + $httpBackend.verifyNoOutstandingExpectation(); + }); + + + it("save(object, rootObject) calls POST /rootObjects/1/objects", (done) => { + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); + + let obj: ObjectModel = objectRestService.getElement(1, rootObj); + + $httpBackend.expectPOST("/api/v1/rootObjects/1/objects").respond(201, { object: { attr: 1, rootId: 1 } }); + + objectRestService.create(obj, rootObj).then((result: noosfero.RestResult) => { + expect(result.data).toBeDefined(); + expect((result.data).attr).toEqual(1); + expect((result.data).rootId).toEqual(1); + done(); + }); + $httpBackend.flush(); + }); + + + + + }); \ No newline at end of file diff --git a/src/lib/ng-noosfero-api/http/restangular_service.ts b/src/lib/ng-noosfero-api/http/restangular_service.ts index 9f67fd2..c872ced 100644 --- a/src/lib/ng-noosfero-api/http/restangular_service.ts +++ b/src/lib/ng-noosfero-api/http/restangular_service.ts @@ -140,12 +140,22 @@ export abstract class RestangularService { * Removes the object provided from the resource collection, * calls DELETE /resourcepath/:resourceId */ - public remove(obj: T, queryParams?: any, headers?: any): ng.IPromise> { + public remove(obj: T, rootElement?: noosfero.RestModel, queryParams?: any, headers?: any): ng.IPromise> { + let restangularObj: restangular.IElement; + + + + if (rootElement) { + restangularObj = rootElement.one(this.getResourcePath(), obj.id); + } else { + restangularObj = this.restangularService.one(this.getResourcePath(), obj.id); + } + let deferred = this.$q.defer>(); let restRequest: ng.IPromise>; - restRequest = obj.remove(queryParams, headers); + restRequest = restangularObj.remove(queryParams, headers); restRequest .then(this.getHandleSuccessFunction(deferred)) @@ -158,12 +168,20 @@ export abstract class RestangularService { * Updates the object into the resource collection * calls PUT /resourcePath/:resourceId {object} */ - public update(obj: T, queryParams?: any, headers?: any): ng.IPromise> { + public update(obj: T, rootElement?: noosfero.RestModel, queryParams?: any, headers?: any): ng.IPromise> { let deferred = this.$q.defer>(); let restRequest: ng.IPromise>; - restRequest = obj.put(queryParams, headers); + let restangularObj: restangular.IElement; + + if (rootElement) { + restangularObj = rootElement.one(this.getResourcePath(), obj.id); + } else { + restangularObj = this.restangularService.one(this.getResourcePath(), obj.id); + } + + restRequest = restangularObj.put(queryParams, headers); restRequest.then(this.getHandleSuccessFunction(deferred)) .catch(this.getHandleErrorFunction(deferred)); @@ -195,11 +213,11 @@ export abstract class RestangularService { /** * Returns a Restangular IElement representing the */ - protected getElement(id: number, rootElement?: noosfero.RestModel): restangular.IElement { + public getElement(id: number, rootElement?: noosfero.RestModel): noosfero.RestModel { if (rootElement) { - return rootElement.one(this.getResourcePath(), id); + return rootElement.one(this.getResourcePath(), id); } else { - return this.restangularService.one(this.getResourcePath(), id); + return this.restangularService.one(this.getResourcePath(), id); } } -- libgit2 0.21.2