Commit bb1b763100a5065d67fdc42b51e51d0595fa5ebe
1 parent
ae4699f3
Exists in
master
and in
32 other branches
Added specs to content-viewer component with nested promises unit test
Showing
3 changed files
with
126 additions
and
4 deletions
Show diff stats
| @@ -0,0 +1,90 @@ | @@ -0,0 +1,90 @@ | ||
| 1 | +import {providers} from 'ng-forward/cjs/testing/providers'; | ||
| 2 | + | ||
| 3 | +import {Input, Component, provide} from 'ng-forward'; | ||
| 4 | + | ||
| 5 | +import * as helpers from "../../spec/helpers"; | ||
| 6 | + | ||
| 7 | +import {ComponentFixture} from 'ng-forward/cjs/testing/test-component-builder'; | ||
| 8 | +import {ContentViewer} from './content-viewer.component'; | ||
| 9 | + | ||
| 10 | +// this htmlTemplate will be re-used between the container components in this spec file | ||
| 11 | +const htmlTemplate: string = '<content-viewer [article]="ctrl.article" [profile]="ctrl.profile"></content-viewer>'; | ||
| 12 | + | ||
| 13 | +describe('Content Viewer Component', () => { | ||
| 14 | + | ||
| 15 | + let stateParamsService: any; | ||
| 16 | + | ||
| 17 | + //loading the templates | ||
| 18 | + beforeEach(() => { | ||
| 19 | + angular.mock.module("templates"); | ||
| 20 | + | ||
| 21 | + stateParamsService = { page: 1 }; | ||
| 22 | + | ||
| 23 | + providers((provide: any) => { | ||
| 24 | + return <any>[ | ||
| 25 | + provide('ArticleService', { | ||
| 26 | + useValue: helpers.mocks.articleService | ||
| 27 | + }), | ||
| 28 | + provide('ProfileService', { | ||
| 29 | + useValue: helpers.mocks.profileService | ||
| 30 | + }), | ||
| 31 | + // TODO: Como criar um mock do atributo "page" de stateParams | ||
| 32 | + provide('$stateParams', { | ||
| 33 | + useValue: stateParamsService | ||
| 34 | + }) | ||
| 35 | + ] | ||
| 36 | + }); | ||
| 37 | + }); | ||
| 38 | + | ||
| 39 | + let buildComponent = (): Promise<ComponentFixture> => { | ||
| 40 | + return helpers.quickCreateComponent({ | ||
| 41 | + providers: [ | ||
| 42 | + helpers.provideEmptyObjects('Restangular') | ||
| 43 | + ], | ||
| 44 | + directives: [ContentViewer], | ||
| 45 | + template: htmlTemplate | ||
| 46 | + }); | ||
| 47 | + }; | ||
| 48 | + | ||
| 49 | + it('renders content viewer directive', (done: Function) => { | ||
| 50 | + buildComponent().then((fixture: ComponentFixture) => { | ||
| 51 | + expect(fixture.debugElement.query('content-viewer').length).toEqual(1); | ||
| 52 | + | ||
| 53 | + done(); | ||
| 54 | + }) | ||
| 55 | + }); | ||
| 56 | + | ||
| 57 | + it('check if article was loaded', (done: Function) => { | ||
| 58 | + var article: any = { | ||
| 59 | + id: 1, | ||
| 60 | + title: 'The article test' | ||
| 61 | + }; | ||
| 62 | + var profile: any = { | ||
| 63 | + id: 1, | ||
| 64 | + identifier: 'the-profile-test', | ||
| 65 | + type: 'Person' | ||
| 66 | + }; | ||
| 67 | + | ||
| 68 | + helpers.mocks.profileService.getCurrentProfile = () => { | ||
| 69 | + return helpers.mocks.promiseResultTemplate(profile); | ||
| 70 | + }; | ||
| 71 | + | ||
| 72 | + helpers.mocks.articleService.getByProfile = (id: number, params: any) => { | ||
| 73 | + return helpers.mocks.promiseResultTemplate({ | ||
| 74 | + data: { | ||
| 75 | + article: article | ||
| 76 | + } | ||
| 77 | + }); | ||
| 78 | + }; | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + buildComponent().then((fixture: ComponentFixture) => { | ||
| 82 | + let contentViewerComp: ContentViewer = fixture.debugElement.componentViewChildren[0].componentInstance; | ||
| 83 | + | ||
| 84 | + expect(contentViewerComp.profile).toEqual(jasmine.objectContaining(profile)); | ||
| 85 | + expect(contentViewerComp.article).toEqual(jasmine.objectContaining(article)); | ||
| 86 | + | ||
| 87 | + done(); | ||
| 88 | + }); | ||
| 89 | + }); | ||
| 90 | +}); |
src/app/content-viewer/content-viewer.component.ts
| @@ -26,11 +26,9 @@ export class ContentViewer { | @@ -26,11 +26,9 @@ export class ContentViewer { | ||
| 26 | @Input() | 26 | @Input() |
| 27 | profile: noosfero.Profile = null; | 27 | profile: noosfero.Profile = null; |
| 28 | 28 | ||
| 29 | - constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { | ||
| 30 | - this.activate(); | ||
| 31 | - } | 29 | + constructor(private articleService: ArticleService, private profileService: ProfileService, private $log: ng.ILogService, private $stateParams: angular.ui.IStateParamsService) { } |
| 32 | 30 | ||
| 33 | - activate() { | 31 | + ngOnInit() { |
| 34 | this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { | 32 | this.profileService.getCurrentProfile().then((profile: noosfero.Profile) => { |
| 35 | this.profile = profile; | 33 | this.profile = profile; |
| 36 | return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); | 34 | return this.articleService.getByProfile(this.profile.id, { path: this.$stateParams["page"] }); |
src/spec/mocks.ts
| @@ -38,6 +38,32 @@ export var mocks = { | @@ -38,6 +38,32 @@ export var mocks = { | ||
| 38 | authService: { | 38 | authService: { |
| 39 | logout: () => { } | 39 | logout: () => { } |
| 40 | }, | 40 | }, |
| 41 | + articleService: { | ||
| 42 | + getByProfile: (profileId: number, params?: any) => { | ||
| 43 | + return { | ||
| 44 | + then: (func?: Function) => { | ||
| 45 | + if (func) func({ | ||
| 46 | + data: { | ||
| 47 | + article: null | ||
| 48 | + } | ||
| 49 | + }); | ||
| 50 | + } | ||
| 51 | + }; | ||
| 52 | + }, | ||
| 53 | + getChildren: (articleId: number, params?: any) => { | ||
| 54 | + return { | ||
| 55 | + then: (func?: Function) => { if (func) func(); } | ||
| 56 | + }; | ||
| 57 | + } | ||
| 58 | + }, | ||
| 59 | + profileService: { | ||
| 60 | + getCurrentProfile: (profile: any) => { | ||
| 61 | + return mocks.promiseResultTemplate({ | ||
| 62 | + profile: profile | ||
| 63 | + }); | ||
| 64 | + }, | ||
| 65 | + instant: () => { } | ||
| 66 | + }, | ||
| 41 | sessionWithCurrentUser: (user: any) => { | 67 | sessionWithCurrentUser: (user: any) => { |
| 42 | return { | 68 | return { |
| 43 | currentUser: () => { return user; } | 69 | currentUser: () => { return user; } |
| @@ -60,5 +86,13 @@ export var mocks = { | @@ -60,5 +86,13 @@ export var mocks = { | ||
| 60 | loadScript: (script?: string) => { | 86 | loadScript: (script?: string) => { |
| 61 | return Promise.resolve(); | 87 | return Promise.resolve(); |
| 62 | } | 88 | } |
| 89 | + }, | ||
| 90 | + promiseResultTemplate: (response?: {}) => { | ||
| 91 | + | ||
| 92 | + return { | ||
| 93 | + then: (func?: (response: any) => void) => { | ||
| 94 | + if (func) { return func(response); } | ||
| 95 | + } | ||
| 96 | + } | ||
| 63 | } | 97 | } |
| 64 | }; | 98 | }; |