From bb1b763100a5065d67fdc42b51e51d0595fa5ebe Mon Sep 17 00:00:00 2001 From: Michel Felipe de Oliveira Ferreira Date: Tue, 15 Mar 2016 15:26:34 -0300 Subject: [PATCH] Added specs to content-viewer component with nested promises unit test --- src/app/content-viewer/content-viewer.component.spec.ts | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/app/content-viewer/content-viewer.component.ts | 6 ++---- src/spec/mocks.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/app/content-viewer/content-viewer.component.spec.ts diff --git a/src/app/content-viewer/content-viewer.component.spec.ts b/src/app/content-viewer/content-viewer.component.spec.ts new file mode 100644 index 0000000..92abc37 --- /dev/null +++ b/src/app/content-viewer/content-viewer.component.spec.ts @@ -0,0 +1,90 @@ +import {providers} from 'ng-forward/cjs/testing/providers'; + +import {Input, Component, provide} from 'ng-forward'; + +import * as helpers from "../../spec/helpers"; + +import {ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; +import {ContentViewer} from './content-viewer.component'; + +// this htmlTemplate will be re-used between the container components in this spec file +const htmlTemplate: string = ''; + +describe('Content Viewer Component', () => { + + let stateParamsService: any; + + //loading the templates + beforeEach(() => { + angular.mock.module("templates"); + + stateParamsService = { page: 1 }; + + providers((provide: any) => { + return [ + provide('ArticleService', { + useValue: helpers.mocks.articleService + }), + provide('ProfileService', { + useValue: helpers.mocks.profileService + }), + // TODO: Como criar um mock do atributo "page" de stateParams + provide('$stateParams', { + useValue: stateParamsService + }) + ] + }); + }); + + let buildComponent = (): Promise => { + return helpers.quickCreateComponent({ + providers: [ + helpers.provideEmptyObjects('Restangular') + ], + directives: [ContentViewer], + template: htmlTemplate + }); + }; + + it('renders content viewer directive', (done: Function) => { + buildComponent().then((fixture: ComponentFixture) => { + expect(fixture.debugElement.query('content-viewer').length).toEqual(1); + + done(); + }) + }); + + it('check if article was loaded', (done: Function) => { + var article: any = { + id: 1, + title: 'The article test' + }; + var profile: any = { + id: 1, + identifier: 'the-profile-test', + type: 'Person' + }; + + helpers.mocks.profileService.getCurrentProfile = () => { + return helpers.mocks.promiseResultTemplate(profile); + }; + + helpers.mocks.articleService.getByProfile = (id: number, params: any) => { + return helpers.mocks.promiseResultTemplate({ + data: { + article: article + } + }); + }; + + + buildComponent().then((fixture: ComponentFixture) => { + let contentViewerComp: ContentViewer = fixture.debugElement.componentViewChildren[0].componentInstance; + + expect(contentViewerComp.profile).toEqual(jasmine.objectContaining(profile)); + expect(contentViewerComp.article).toEqual(jasmine.objectContaining(article)); + + done(); + }); + }); +}); diff --git a/src/app/content-viewer/content-viewer.component.ts b/src/app/content-viewer/content-viewer.component.ts index 4e855d9..68c0091 100644 --- a/src/app/content-viewer/content-viewer.component.ts +++ b/src/app/content-viewer/content-viewer.component.ts @@ -26,11 +26,9 @@ export class ContentViewer { @Input() profile: noosfero.Profile = null; - constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { - this.activate(); - } + constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { } - activate() { + ngOnInit() { this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { this.profile = profile; return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); diff --git a/src/spec/mocks.ts b/src/spec/mocks.ts index cbea101..ab9ad6a 100644 --- a/src/spec/mocks.ts +++ b/src/spec/mocks.ts @@ -38,6 +38,32 @@ export var mocks = { authService: { logout: () => { } }, + articleService: { + getByProfile: (profileId: number, params?: any) => { + return { + then: (func?: Function) => { + if (func) func({ + data: { + article: null + } + }); + } + }; + }, + getChildren: (articleId: number, params?: any) => { + return { + then: (func?: Function) => { if (func) func(); } + }; + } + }, + profileService: { + getCurrentProfile: (profile: any) => { + return mocks.promiseResultTemplate({ + profile: profile + }); + }, + instant: () => { } + }, sessionWithCurrentUser: (user: any) => { return { currentUser: () => { return user; } @@ -60,5 +86,13 @@ export var mocks = { loadScript: (script?: string) => { return Promise.resolve(); } + }, + promiseResultTemplate: (response?: {}) => { + + return { + then: (func?: (response: any) => void) => { + if (func) { return func(response); } + } + } } }; -- libgit2 0.21.2