Commit 9a1d573f97d52cb45c775b988eb208279144ef28
1 parent
44ddf3b7
Exists in
master
and in
31 other branches
tests for restangular service base class
Showing
2 changed files
with
184 additions
and
25 deletions
Show diff stats
src/lib/ng-noosfero-api/http/restangular_service.spec.ts
| 1 | 1 | import {RestangularService} from "./restangular_service"; |
| 2 | 2 | |
| 3 | 3 | interface ObjectModel extends noosfero.RestModel { |
| 4 | +} | |
| 5 | + | |
| 6 | +interface RootObjectModel extends noosfero.RestModel { | |
| 4 | 7 | |
| 5 | 8 | } |
| 6 | 9 | |
| 7 | -describe("Restangular Service", () => { | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | +describe("Restangular Service - base Class", () => { | |
| 14 | + | |
| 15 | + | |
| 8 | 16 | |
| 9 | 17 | class ObjectRestService extends RestangularService<ObjectModel> { |
| 10 | 18 | public getDataKeys() { |
| ... | ... | @@ -19,9 +27,23 @@ describe("Restangular Service", () => { |
| 19 | 27 | } |
| 20 | 28 | } |
| 21 | 29 | |
| 30 | + class RootObjectRestService extends RestangularService<ObjectModel> { | |
| 31 | + public getDataKeys() { | |
| 32 | + return { | |
| 33 | + singular: "rootObject", | |
| 34 | + plural: "rootObjects" | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + public getResourcePath() { | |
| 39 | + return "rootObjects"; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + | |
| 22 | 43 | let restangularService: restangular.IService; |
| 23 | 44 | let $httpBackend: ng.IHttpBackendService; |
| 24 | 45 | let objectRestService: ObjectRestService; |
| 46 | + let rootObjectRestService: RootObjectRestService; | |
| 25 | 47 | |
| 26 | 48 | beforeEach(angular.mock.module("noosferoApp", ($translateProvider: angular.translate.ITranslateProvider) => { |
| 27 | 49 | $translateProvider.translations('en', {}); |
| ... | ... | @@ -29,50 +51,169 @@ describe("Restangular Service", () => { |
| 29 | 51 | |
| 30 | 52 | beforeEach(inject((_Restangular_: restangular.IService, _$q_: ng.IQService, _$httpBackend_: ng.IHttpBackendService) => { |
| 31 | 53 | restangularService = _Restangular_; |
| 32 | - objectRestService = new ObjectRestService(_Restangular_, _$q_, <any>console); | |
| 54 | + objectRestService = new ObjectRestService(_Restangular_, _$q_, null); | |
| 55 | + rootObjectRestService = new RootObjectRestService(_Restangular_, _$q_, null); | |
| 33 | 56 | $httpBackend = _$httpBackend_; |
| 34 | 57 | })); |
| 35 | 58 | |
| 36 | - | |
| 37 | - it("calls GET /objects", (done) => { | |
| 59 | + it("list() calls GET /objects", (done) => { | |
| 38 | 60 | $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); |
| 39 | 61 | |
| 40 | 62 | objectRestService.list().then((result: noosfero.RestResult<ObjectModel>) => { |
| 41 | - console.log(result); | |
| 42 | 63 | expect(result.data).toBeDefined(); |
| 43 | 64 | expect((<ObjectModel[]>result.data).length).toEqual(2); |
| 44 | 65 | done(); |
| 45 | 66 | }); |
| 46 | 67 | |
| 47 | 68 | $httpBackend.flush(); |
| 69 | + | |
| 70 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 48 | 71 | }); |
| 49 | 72 | |
| 50 | - it("calls GET /objects", (done) => { | |
| 51 | - $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); | |
| 73 | + it("list(rootObject) calls GET /rootObjects/1/objects", (done) => { | |
| 52 | 74 | |
| 53 | - objectRestService.list().then((result: noosfero.RestResult<ObjectModel>) => { | |
| 54 | - console.log(result); | |
| 75 | + $httpBackend.expectGET("/api/v1/rootObjects/1/objects").respond(200, { objects: [{ id: 1 }, { id: 2 }] }); | |
| 76 | + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); | |
| 77 | + | |
| 78 | + objectRestService.list(rootObj).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 55 | 79 | expect(result.data).toBeDefined(); |
| 56 | - console.log("HERE", (<ObjectModel[]>result.data).length); | |
| 57 | 80 | expect((<ObjectModel[]>result.data).length).toEqual(2); |
| 81 | + done(); | |
| 58 | 82 | }); |
| 83 | + | |
| 59 | 84 | $httpBackend.flush(); |
| 60 | 85 | |
| 86 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 87 | + }); | |
| 61 | 88 | |
| 62 | - setTimeout(() => { | |
| 63 | - $httpBackend.expectGET("/api/v1/objects").respond(200, { objects: [{ id: 1 }, { id: 3 }] }); | |
| 89 | + it("get(1) calls GET /objects/1", (done) => { | |
| 90 | + $httpBackend.expectGET("/api/v1/objects/1").respond(200, { object: { id: 1 } }); | |
| 64 | 91 | |
| 65 | - objectRestService.list().then((result: noosfero.RestResult<ObjectModel>) => { | |
| 66 | - console.log(result); | |
| 92 | + objectRestService.get(1).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 93 | + expect(result.data).toBeDefined(); | |
| 94 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 95 | + done(); | |
| 96 | + }); | |
| 97 | + $httpBackend.flush(); | |
| 98 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 99 | + }); | |
| 100 | + | |
| 101 | + it("objectService.get(1, rootObject) calls GET /rootObjects/1/objects/1", (done) => { | |
| 102 | + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); | |
| 103 | + $httpBackend.expectGET("/api/v1/rootObjects/1/objects/1").respond(200, { object: { id: 1 } }); | |
| 104 | + | |
| 105 | + objectRestService.get(1, rootObj).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 106 | + expect(result.data).toBeDefined(); | |
| 107 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 108 | + done(); | |
| 109 | + }); | |
| 110 | + $httpBackend.flush(); | |
| 111 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 112 | + }); | |
| 113 | + | |
| 114 | + it("remove(object) calls DELETE /objects/1", (done) => { | |
| 115 | + $httpBackend.expectGET("/api/v1/objects/1").respond(200, { object: { id: 1 } }); | |
| 116 | + objectRestService.get(1).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 117 | + let object: ObjectModel = <ObjectModel>result.data; | |
| 118 | + | |
| 119 | + $httpBackend.expectDELETE("/api/v1/objects/1").respond(204, { object: { id: 1 } }); | |
| 120 | + | |
| 121 | + objectRestService.remove(object).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 67 | 122 | expect(result.data).toBeDefined(); |
| 68 | - console.log("HERE 2", (<ObjectModel[]>result.data).length); | |
| 69 | - expect((<ObjectModel[]>result.data).length).toEqual(2); | |
| 123 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 70 | 124 | done(); |
| 71 | 125 | }); |
| 72 | - $httpBackend.flush(); | |
| 73 | - }, 2000); | |
| 126 | + }); | |
| 127 | + | |
| 128 | + $httpBackend.flush(); | |
| 129 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 130 | + }); | |
| 131 | + | |
| 132 | + | |
| 133 | + it("remove(object, rootObject) calls DELETE /rootObjects/1/objects/1", (done) => { | |
| 134 | + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); | |
| 135 | + | |
| 136 | + let obj: ObjectModel = objectRestService.getElement(1, rootObj); | |
| 137 | + | |
| 138 | + $httpBackend.expectDELETE("/api/v1/rootObjects/1/objects/1").respond(204, { object: { id: 1, rootId: 1 } }); | |
| 74 | 139 | |
| 140 | + objectRestService.remove(obj, rootObj).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 141 | + expect(result.data).toBeDefined(); | |
| 142 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 143 | + expect((<any>result.data).rootId).toEqual(1); | |
| 144 | + done(); | |
| 145 | + }); | |
| 146 | + $httpBackend.flush(); | |
| 147 | + }); | |
| 148 | + | |
| 149 | + | |
| 150 | + it("update(object) calls PUT /objects/1", (done) => { | |
| 151 | + $httpBackend.expectPUT("/api/v1/objects/1").respond(200, { object: { id: 1 } }); | |
| 152 | + | |
| 153 | + let object: ObjectModel = objectRestService.getElement(1); | |
| 154 | + | |
| 155 | + objectRestService.update(object).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 156 | + expect(result.data).toBeDefined(); | |
| 157 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 158 | + done(); | |
| 159 | + }); | |
| 75 | 160 | |
| 161 | + $httpBackend.flush(); | |
| 162 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 163 | + }); | |
| 164 | + | |
| 165 | + | |
| 166 | + it("update(object, rootObject) calls PUT /rootObjects/1/objects/1", (done) => { | |
| 167 | + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); | |
| 168 | + | |
| 169 | + let obj: ObjectModel = objectRestService.getElement(1, rootObj); | |
| 170 | + | |
| 171 | + $httpBackend.expectPUT("/api/v1/rootObjects/1/objects/1").respond(200, { object: { id: 1, rootId: 1 } }); | |
| 76 | 172 | |
| 173 | + objectRestService.update(obj, rootObj).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 174 | + expect(result.data).toBeDefined(); | |
| 175 | + expect((<ObjectModel>result.data).id).toEqual(1); | |
| 176 | + expect((<any>result.data).rootId).toEqual(1); | |
| 177 | + done(); | |
| 178 | + }); | |
| 179 | + $httpBackend.flush(); | |
| 77 | 180 | }); |
| 181 | + | |
| 182 | + | |
| 183 | + it("save(object) calls POST /objects", (done) => { | |
| 184 | + $httpBackend.expectPOST("/api/v1/objects").respond(201, { object: { attr: 1 } }); | |
| 185 | + | |
| 186 | + let object: ObjectModel = objectRestService.getElement(1); | |
| 187 | + | |
| 188 | + objectRestService.create(object).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 189 | + expect(result.data).toBeDefined(); | |
| 190 | + expect((<any>result.data).attr).toEqual(1); | |
| 191 | + done(); | |
| 192 | + }); | |
| 193 | + | |
| 194 | + $httpBackend.flush(); | |
| 195 | + $httpBackend.verifyNoOutstandingExpectation(); | |
| 196 | + }); | |
| 197 | + | |
| 198 | + | |
| 199 | + it("save(object, rootObject) calls POST /rootObjects/1/objects", (done) => { | |
| 200 | + let rootObj: RootObjectModel = rootObjectRestService.getElement(1); | |
| 201 | + | |
| 202 | + let obj: ObjectModel = objectRestService.getElement(1, rootObj); | |
| 203 | + | |
| 204 | + $httpBackend.expectPOST("/api/v1/rootObjects/1/objects").respond(201, { object: { attr: 1, rootId: 1 } }); | |
| 205 | + | |
| 206 | + objectRestService.create(obj, rootObj).then((result: noosfero.RestResult<ObjectModel>) => { | |
| 207 | + expect(result.data).toBeDefined(); | |
| 208 | + expect((<any>result.data).attr).toEqual(1); | |
| 209 | + expect((<any>result.data).rootId).toEqual(1); | |
| 210 | + done(); | |
| 211 | + }); | |
| 212 | + $httpBackend.flush(); | |
| 213 | + }); | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 78 | 219 | }); |
| 79 | 220 | \ No newline at end of file | ... | ... |
src/lib/ng-noosfero-api/http/restangular_service.ts
| ... | ... | @@ -140,12 +140,22 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 140 | 140 | * Removes the object provided from the resource collection, |
| 141 | 141 | * calls DELETE /resourcepath/:resourceId |
| 142 | 142 | */ |
| 143 | - public remove(obj: T, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<T>> { | |
| 143 | + public remove(obj: T, rootElement?: noosfero.RestModel, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<T>> { | |
| 144 | + let restangularObj: restangular.IElement; | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + if (rootElement) { | |
| 149 | + restangularObj = rootElement.one(this.getResourcePath(), <string>obj.id); | |
| 150 | + } else { | |
| 151 | + restangularObj = this.restangularService.one(this.getResourcePath(), <string>obj.id); | |
| 152 | + } | |
| 153 | + | |
| 144 | 154 | let deferred = this.$q.defer<noosfero.RestResult<T>>(); |
| 145 | 155 | |
| 146 | 156 | let restRequest: ng.IPromise<noosfero.RestResult<T>>; |
| 147 | 157 | |
| 148 | - restRequest = obj.remove(queryParams, headers); | |
| 158 | + restRequest = restangularObj.remove(queryParams, headers); | |
| 149 | 159 | |
| 150 | 160 | restRequest |
| 151 | 161 | .then(this.getHandleSuccessFunction(deferred)) |
| ... | ... | @@ -158,12 +168,20 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 158 | 168 | * Updates the object into the resource collection |
| 159 | 169 | * calls PUT /resourcePath/:resourceId {object} |
| 160 | 170 | */ |
| 161 | - public update(obj: T, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<T>> { | |
| 171 | + public update(obj: T, rootElement?: noosfero.RestModel, queryParams?: any, headers?: any): ng.IPromise<noosfero.RestResult<T>> { | |
| 162 | 172 | let deferred = this.$q.defer<noosfero.RestResult<T>>(); |
| 163 | 173 | |
| 164 | 174 | let restRequest: ng.IPromise<noosfero.RestResult<T>>; |
| 165 | 175 | |
| 166 | - restRequest = obj.put(queryParams, headers); | |
| 176 | + let restangularObj: restangular.IElement; | |
| 177 | + | |
| 178 | + if (rootElement) { | |
| 179 | + restangularObj = rootElement.one(this.getResourcePath(), <string>obj.id); | |
| 180 | + } else { | |
| 181 | + restangularObj = this.restangularService.one(this.getResourcePath(), <string>obj.id); | |
| 182 | + } | |
| 183 | + | |
| 184 | + restRequest = restangularObj.put(queryParams, headers); | |
| 167 | 185 | |
| 168 | 186 | restRequest.then(this.getHandleSuccessFunction(deferred)) |
| 169 | 187 | .catch(this.getHandleErrorFunction(deferred)); |
| ... | ... | @@ -195,11 +213,11 @@ export abstract class RestangularService<T extends noosfero.RestModel> { |
| 195 | 213 | /** |
| 196 | 214 | * Returns a Restangular IElement representing the |
| 197 | 215 | */ |
| 198 | - protected getElement(id: number, rootElement?: noosfero.RestModel): restangular.IElement { | |
| 216 | + public getElement(id: number, rootElement?: noosfero.RestModel): noosfero.RestModel { | |
| 199 | 217 | if (rootElement) { |
| 200 | - return rootElement.one(this.getResourcePath(), id); | |
| 218 | + return <noosfero.RestModel>rootElement.one(this.getResourcePath(), id); | |
| 201 | 219 | } else { |
| 202 | - return this.restangularService.one(this.getResourcePath(), id); | |
| 220 | + return <noosfero.RestModel>this.restangularService.one(this.getResourcePath(), id); | |
| 203 | 221 | } |
| 204 | 222 | } |
| 205 | 223 | ... | ... |