From 5368292db8eb99227dcd5088b46e94160a37acd9 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 8 Mar 2016 16:36:03 -0300 Subject: [PATCH] Add tests to profile home component --- src/app/profile/profile-home.component.spec.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/profile/profile-home.component.ts | 6 +++--- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/app/profile/profile-home.component.spec.ts diff --git a/src/app/profile/profile-home.component.spec.ts b/src/app/profile/profile-home.component.spec.ts new file mode 100644 index 0000000..70476ac --- /dev/null +++ b/src/app/profile/profile-home.component.spec.ts @@ -0,0 +1,58 @@ +import {quickCreateComponent} from "../../spec/helpers"; +import {ProfileHome} from "./profile-home.component"; + +describe("Components", () => { + describe("Profile Home Component", () => { + + let $rootScope: ng.IRootScopeService; + let $q: ng.IQService; + let homePageResponse: ng.IDeferred; + let profileServiceMock: any; + let $state: any; + + beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => { + $rootScope = _$rootScope_; + $q = _$q_; + })); + + beforeEach(() => { + $state = jasmine.createSpyObj("$state", ["transitionTo"]); + profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["getCurrentProfile", "getHomePage"]); + + let currentProfileResponse = $q.defer(); + currentProfileResponse.resolve({ identifier: "profile" }); + homePageResponse = $q.defer(); + + profileServiceMock.getCurrentProfile = jasmine.createSpy("getCurrentProfile").and.returnValue(currentProfileResponse.promise); + profileServiceMock.getHomePage = jasmine.createSpy("getHomePage").and.returnValue(homePageResponse.promise); + }); + + it("transition to profile homepage when there is a homepage setted", done => { + homePageResponse.resolve({ data: { article: { path: "something" } } }); + + let component: ProfileHome = new ProfileHome(profileServiceMock, $state); + $rootScope.$apply(); + expect(profileServiceMock.getCurrentProfile).toHaveBeenCalled(); + expect(profileServiceMock.getHomePage).toHaveBeenCalled(); + + expect($state.transitionTo). + toHaveBeenCalledWith("main.profile.page", + { page: "something", profile: "profile" }, { location: false }); + done(); + }); + + it("transition to profile info page when there is no homepage setted", done => { + homePageResponse.resolve({ data: {} }); + + let component: ProfileHome = new ProfileHome(profileServiceMock, $state); + $rootScope.$apply(); + expect(profileServiceMock.getCurrentProfile).toHaveBeenCalled(); + expect(profileServiceMock.getHomePage).toHaveBeenCalled(); + + expect($state.transitionTo). + toHaveBeenCalledWith("main.profile.info", + { profile: "profile" }, { location: false }); + done(); + }); + }); +}); diff --git a/src/app/profile/profile-home.component.ts b/src/app/profile/profile-home.component.ts index b5119da..5a7e840 100644 --- a/src/app/profile/profile-home.component.ts +++ b/src/app/profile/profile-home.component.ts @@ -8,15 +8,15 @@ import {ProfileService} from "../../lib/ng-noosfero-api/http/profile.service"; template: "
", providers: [provide('profileService', { useClass: ProfileService })] }) -@Inject(ProfileService, "$log", "$stateParams", "$scope", "$state") +@Inject(ProfileService, "$state") export class ProfileHome { profile: Profile; - constructor(profileService: ProfileService, $log: ng.ILogService, $stateParams: ng.ui.IStateParamsService, $scope: ng.IScope, $state: ng.ui.IStateService) { + constructor(profileService: ProfileService, $state: ng.ui.IStateService) { profileService.getCurrentProfile().then((profile: Profile) => { this.profile = profile; - return profileService.get(this.profile.id).customGET('home_page', { fields: 'path' }); + return profileService.getHomePage(this.profile.id, { fields: 'path' }); }).then((response: restangular.IResponse) => { if (response.data.article) { $state.transitionTo('main.profile.page', { page: response.data.article.path, profile: this.profile.identifier }, { location: false }); -- libgit2 0.21.2