From 87c0e18206d037cb418c3b3140fbfc68b31d2b57 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Thu, 10 Mar 2016 11:20:16 -0300 Subject: [PATCH] Create a method in profile service that change the current profile based on an identifier --- src/app/profile/profile.component.spec.ts | 11 +++++------ src/app/profile/profile.component.ts | 6 ++---- src/lib/ng-noosfero-api/http/profile.service.spec.ts | 12 ++++++++++++ src/lib/ng-noosfero-api/http/profile.service.ts | 8 ++++++++ 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/app/profile/profile.component.spec.ts b/src/app/profile/profile.component.spec.ts index 66277bd..2cf44ce 100644 --- a/src/app/profile/profile.component.spec.ts +++ b/src/app/profile/profile.component.spec.ts @@ -16,22 +16,21 @@ describe("Components", () => { beforeEach(() => { $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); - profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getByIdentifier", "getBoxes", "setCurrentProfile"]); + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]); - let getByIdentifierResponse = $q.defer(); - getByIdentifierResponse.resolve({ data: [{ id: 1 }] }); + let profileResponse = $q.defer(); + profileResponse.resolve({ id: 1 }); let getBoxesResponse = $q.defer(); getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); - profileServiceMock.getByIdentifier = jasmine.createSpy("getByIdentifier").and.returnValue(getByIdentifierResponse.promise); + profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(profileResponse.promise); profileServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); }); it("get the profile and store in profile service", done => { let component: Profile = new Profile(profileServiceMock, $stateParams); $rootScope.$apply(); - expect(profileServiceMock.getByIdentifier).toHaveBeenCalled(); - expect(profileServiceMock.setCurrentProfile).toHaveBeenCalled(); + expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); expect(component.profile).toEqual({ id: 1 }); done(); }); diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index 4c18159..3f328ba 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -76,10 +76,8 @@ export class Profile { profile: noosferoModels.Profile; constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService) { - profileService.resetCurrentProfile(); - profileService.getByIdentifier($stateParams["profile"]).then((response: restangular.IResponse) => { - this.profile = response.data[0]; - profileService.setCurrentProfile(this.profile); + profileService.setCurrentProfileByIdentifier($stateParams["profile"]).then((profile: noosferoModels.Profile) => { + this.profile = profile; return profileService.getBoxes(this.profile.id); }).then((response: restangular.IResponse) => { this.boxes = response.data.boxes; diff --git a/src/lib/ng-noosfero-api/http/profile.service.spec.ts b/src/lib/ng-noosfero-api/http/profile.service.spec.ts index 651fe27..90227df 100644 --- a/src/lib/ng-noosfero-api/http/profile.service.spec.ts +++ b/src/lib/ng-noosfero-api/http/profile.service.spec.ts @@ -79,6 +79,18 @@ describe("Services", () => { $httpBackend.flush(); }); + it("should find the profile by identifier, set and resolve the current profile", (done) => { + let identifier = 'profile1'; + $httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, [{ name: "profile1" }]); + profileService.setCurrentProfileByIdentifier(identifier).then((profile: Profile) => { + expect(profile).toEqual({ name: "profile1" }); + profileService.getCurrentProfile().then((profile: Profile) => { + expect(profile).toEqual({ name: "profile1" }); + done(); + }); + }); + $httpBackend.flush(); + }); }); diff --git a/src/lib/ng-noosfero-api/http/profile.service.ts b/src/lib/ng-noosfero-api/http/profile.service.ts index 3acad69..073bbe5 100644 --- a/src/lib/ng-noosfero-api/http/profile.service.ts +++ b/src/lib/ng-noosfero-api/http/profile.service.ts @@ -23,6 +23,14 @@ export class ProfileService { this._currentProfilePromise.resolve(profile); } + setCurrentProfileByIdentifier(identifier: string) { + this.resetCurrentProfile(); + return this.getByIdentifier(identifier).then((response: restangular.IResponse) => { + this.setCurrentProfile(response.data[0]); + return this.getCurrentProfile(); + }); + } + getHomePage(profileId: number, params?: any) { return this.get(profileId).customGET("home_page", params); } -- libgit2 0.21.2