Commit 87c0e18206d037cb418c3b3140fbfc68b31d2b57

Authored by Victor Costa
1 parent 4f023483

Create a method in profile service that change the current profile based on an identifier

src/app/profile/profile.component.spec.ts
@@ -16,22 +16,21 @@ describe("Components", () => { @@ -16,22 +16,21 @@ describe("Components", () => {
16 16
17 beforeEach(() => { 17 beforeEach(() => {
18 $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); 18 $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]);
19 - profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getByIdentifier", "getBoxes", "setCurrentProfile"]); 19 + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]);
20 20
21 - let getByIdentifierResponse = $q.defer();  
22 - getByIdentifierResponse.resolve({ data: [{ id: 1 }] }); 21 + let profileResponse = $q.defer();
  22 + profileResponse.resolve({ id: 1 });
23 let getBoxesResponse = $q.defer(); 23 let getBoxesResponse = $q.defer();
24 getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } }); 24 getBoxesResponse.resolve({ data: { boxes: [{ id: 2 }] } });
25 25
26 - profileServiceMock.getByIdentifier = jasmine.createSpy("getByIdentifier").and.returnValue(getByIdentifierResponse.promise); 26 + profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(profileResponse.promise);
27 profileServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise); 27 profileServiceMock.getBoxes = jasmine.createSpy("getBoxes").and.returnValue(getBoxesResponse.promise);
28 }); 28 });
29 29
30 it("get the profile and store in profile service", done => { 30 it("get the profile and store in profile service", done => {
31 let component: Profile = new Profile(profileServiceMock, $stateParams); 31 let component: Profile = new Profile(profileServiceMock, $stateParams);
32 $rootScope.$apply(); 32 $rootScope.$apply();
33 - expect(profileServiceMock.getByIdentifier).toHaveBeenCalled();  
34 - expect(profileServiceMock.setCurrentProfile).toHaveBeenCalled(); 33 + expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled();
35 expect(component.profile).toEqual({ id: 1 }); 34 expect(component.profile).toEqual({ id: 1 });
36 done(); 35 done();
37 }); 36 });
src/app/profile/profile.component.ts
@@ -76,10 +76,8 @@ export class Profile { @@ -76,10 +76,8 @@ export class Profile {
76 profile: noosferoModels.Profile; 76 profile: noosferoModels.Profile;
77 77
78 constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService) { 78 constructor(profileService: ProfileService, $stateParams: ng.ui.IStateParamsService) {
79 - profileService.resetCurrentProfile();  
80 - profileService.getByIdentifier($stateParams["profile"]).then((response: restangular.IResponse) => {  
81 - this.profile = response.data[0];  
82 - profileService.setCurrentProfile(this.profile); 79 + profileService.setCurrentProfileByIdentifier($stateParams["profile"]).then((profile: noosferoModels.Profile) => {
  80 + this.profile = profile;
83 return profileService.getBoxes(this.profile.id); 81 return profileService.getBoxes(this.profile.id);
84 }).then((response: restangular.IResponse) => { 82 }).then((response: restangular.IResponse) => {
85 this.boxes = response.data.boxes; 83 this.boxes = response.data.boxes;
src/lib/ng-noosfero-api/http/profile.service.spec.ts
@@ -79,6 +79,18 @@ describe("Services", () => { @@ -79,6 +79,18 @@ describe("Services", () => {
79 $httpBackend.flush(); 79 $httpBackend.flush();
80 }); 80 });
81 81
  82 + it("should find the profile by identifier, set and resolve the current profile", (done) => {
  83 + let identifier = 'profile1';
  84 + $httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, [{ name: "profile1" }]);
  85 + profileService.setCurrentProfileByIdentifier(identifier).then((profile: Profile) => {
  86 + expect(profile).toEqual({ name: "profile1" });
  87 + profileService.getCurrentProfile().then((profile: Profile) => {
  88 + expect(profile).toEqual({ name: "profile1" });
  89 + done();
  90 + });
  91 + });
  92 + $httpBackend.flush();
  93 + });
82 }); 94 });
83 95
84 96
src/lib/ng-noosfero-api/http/profile.service.ts
@@ -23,6 +23,14 @@ export class ProfileService { @@ -23,6 +23,14 @@ export class ProfileService {
23 this._currentProfilePromise.resolve(profile); 23 this._currentProfilePromise.resolve(profile);
24 } 24 }
25 25
  26 + setCurrentProfileByIdentifier(identifier: string) {
  27 + this.resetCurrentProfile();
  28 + return this.getByIdentifier(identifier).then((response: restangular.IResponse) => {
  29 + this.setCurrentProfile(response.data[0]);
  30 + return this.getCurrentProfile();
  31 + });
  32 + }
  33 +
26 getHomePage(profileId: number, params?: any) { 34 getHomePage(profileId: number, params?: any) {
27 return this.get(profileId).customGET("home_page", params); 35 return this.get(profileId).customGET("home_page", params);
28 } 36 }