Commit bdd5ee03491b47515d86c382e84b162f80d4887f
Exists in
master
and in
26 other branches
Merge branch 'block-service' into 'master'
Use block endpoint to get the block content See merge request !17
Showing
7 changed files
with
151 additions
and
65 deletions
Show diff stats
src/app/layout/blocks/recent-documents/recent-documents-block.component.spec.ts
... | ... | @@ -11,9 +11,9 @@ describe("Components", () => { |
11 | 11 | describe("Recent Documents Block Component", () => { |
12 | 12 | |
13 | 13 | let settingsObj = {}; |
14 | - let mockedArticleService = { | |
15 | - getByProfile: (profile: noosfero.Profile, filters: any): any => { | |
16 | - return Promise.resolve({ data: [{ name: "article1" }], headers: (name: string) => { return name; } }); | |
14 | + let mockedBlockService = { | |
15 | + getApiContent: (block: noosfero.Block): any => { | |
16 | + return Promise.resolve({ articles: [{ name: "article1" }], headers: (name: string) => { return name; } }); | |
17 | 17 | } |
18 | 18 | }; |
19 | 19 | let profile = { name: 'profile-name' }; |
... | ... | @@ -25,8 +25,8 @@ describe("Components", () => { |
25 | 25 | function getProviders() { |
26 | 26 | return [ |
27 | 27 | new Provider('$state', { useValue: state }), |
28 | - new Provider('ArticleService', { | |
29 | - useValue: mockedArticleService | |
28 | + new Provider('BlockService', { | |
29 | + useValue: mockedBlockService | |
30 | 30 | }), |
31 | 31 | ].concat(provideFilters("truncateFilter", "stripTagsFilter")); |
32 | 32 | } |
... | ... | @@ -44,7 +44,7 @@ describe("Components", () => { |
44 | 44 | } |
45 | 45 | |
46 | 46 | |
47 | - it("get recent documents from the article service", done => { | |
47 | + it("get recent documents from the block service", done => { | |
48 | 48 | tcb.createAsync(getComponent()).then(fixture => { |
49 | 49 | let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
50 | 50 | expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); |
... | ... | @@ -61,20 +61,5 @@ describe("Components", () => { |
61 | 61 | }); |
62 | 62 | }); |
63 | 63 | |
64 | - it("it uses default limit 5 if not defined on block", done => { | |
65 | - settingsObj = null; | |
66 | - mockedArticleService = jasmine.createSpyObj("mockedArticleService", ["getByProfile"]); | |
67 | - (<any>mockedArticleService).mocked = true; | |
68 | - let thenMocked = jasmine.createSpy("then"); | |
69 | - mockedArticleService.getByProfile = jasmine.createSpy("getByProfile").and.returnValue({then: thenMocked}); | |
70 | - let getByProfileFunct = mockedArticleService.getByProfile; | |
71 | - tcb.createAsync(getComponent()).then(fixture => { | |
72 | - let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | |
73 | - recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } }); | |
74 | - expect(getByProfileFunct).toHaveBeenCalledWith(profile, { content_type: 'TinyMceArticle', per_page: 5 }); | |
75 | - done(); | |
76 | - }); | |
77 | - }); | |
78 | - | |
79 | 64 | }); |
80 | 65 | }); | ... | ... |
src/app/layout/blocks/recent-documents/recent-documents-block.component.ts
1 | 1 | import {Component, Inject, Input} from "ng-forward"; |
2 | -import {ArticleService} from "../../../../lib/ng-noosfero-api/http/article.service"; | |
2 | +import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; | |
3 | 3 | |
4 | 4 | @Component({ |
5 | 5 | selector: "noosfero-recent-documents-block", |
6 | 6 | templateUrl: 'app/layout/blocks/recent-documents/recent-documents-block.html' |
7 | 7 | }) |
8 | -@Inject(ArticleService, "$state") | |
8 | +@Inject(BlockService, "$state") | |
9 | 9 | export class RecentDocumentsBlockComponent { |
10 | 10 | |
11 | 11 | @Input() block: any; |
... | ... | @@ -13,23 +13,15 @@ export class RecentDocumentsBlockComponent { |
13 | 13 | |
14 | 14 | profile: any; |
15 | 15 | documents: any; |
16 | - | |
17 | 16 | documentsLoaded: boolean = false; |
18 | 17 | |
19 | - constructor(private articleService: ArticleService, private $state: any) { | |
20 | - } | |
18 | + constructor(private blockService: BlockService, private $state: any) { } | |
21 | 19 | |
22 | 20 | ngOnInit() { |
23 | 21 | this.profile = this.owner; |
24 | 22 | this.documents = []; |
25 | - | |
26 | - let limit = ((this.block && this.block.settings) ? this.block.settings.limit : null) || 5; | |
27 | - // FIXME get all text articles | |
28 | - // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle | |
29 | - // and the promise should be of type TinyMceArticle[], per example | |
30 | - this.articleService.getByProfile(this.profile, { content_type: 'TinyMceArticle', per_page: limit }) | |
31 | - .then((result: noosfero.RestResult<noosfero.Article[]>) => { | |
32 | - this.documents = <noosfero.Article[]>result.data; | |
23 | + this.blockService.getApiContent(this.block).then((content: any) => { | |
24 | + this.documents = content.articles; | |
33 | 25 | this.documentsLoaded = true; |
34 | 26 | }); |
35 | 27 | } | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +import {BlockService} from "./block.service"; | |
2 | + | |
3 | + | |
4 | +describe("Services", () => { | |
5 | + | |
6 | + describe("Block Service", () => { | |
7 | + | |
8 | + let $httpBackend: ng.IHttpBackendService; | |
9 | + let blockService: BlockService; | |
10 | + | |
11 | + beforeEach(angular.mock.module("noosferoApp", ($translateProvider: angular.translate.ITranslateProvider) => { | |
12 | + $translateProvider.translations('en', {}); | |
13 | + })); | |
14 | + | |
15 | + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _BlockService_: BlockService) => { | |
16 | + $httpBackend = _$httpBackend_; | |
17 | + blockService = _BlockService_; | |
18 | + })); | |
19 | + | |
20 | + | |
21 | + describe("Succesfull requests", () => { | |
22 | + | |
23 | + it("should return api content of a block", (done) => { | |
24 | + let blockId = 1; | |
25 | + $httpBackend.expectGET(`/api/v1/blocks/${blockId}`).respond(200, { block: { api_content: [{ name: "article1" }] } }); | |
26 | + blockService.getApiContent(<noosfero.Block>{ id: blockId }).then((content: any) => { | |
27 | + expect(content).toEqual([{ name: "article1" }]); | |
28 | + done(); | |
29 | + }); | |
30 | + $httpBackend.flush(); | |
31 | + }); | |
32 | + }); | |
33 | + | |
34 | + | |
35 | + }); | |
36 | +}); | ... | ... |
... | ... | @@ -0,0 +1,40 @@ |
1 | +import { Injectable, Inject } from "ng-forward"; | |
2 | +import {RestangularService} from "./restangular_service"; | |
3 | +import {ProfileService} from "./profile.service"; | |
4 | + | |
5 | +@Injectable() | |
6 | +@Inject("Restangular", "$q", "$log") | |
7 | +export class BlockService extends RestangularService<noosfero.Block> { | |
8 | + | |
9 | + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { | |
10 | + super(Restangular, $q, $log); | |
11 | + } | |
12 | + | |
13 | + getResourcePath() { | |
14 | + return "blocks"; | |
15 | + } | |
16 | + | |
17 | + getDataKeys() { | |
18 | + return { | |
19 | + singular: 'block', | |
20 | + plural: 'blocks' | |
21 | + }; | |
22 | + } | |
23 | + | |
24 | + getApiContent(block: noosfero.Block) { | |
25 | + let apiContentPromise = this.$q.defer(); | |
26 | + if (block) { | |
27 | + if (block.api_content) { | |
28 | + apiContentPromise.resolve(block.api_content); | |
29 | + } else { | |
30 | + this.get(block.id) | |
31 | + .then((result: noosfero.RestResult<noosfero.Block>) => { | |
32 | + block = result.data; | |
33 | + apiContentPromise.resolve(block.api_content); | |
34 | + }); | |
35 | + } | |
36 | + } | |
37 | + return apiContentPromise.promise; | |
38 | + } | |
39 | + | |
40 | +} | ... | ... |
src/lib/ng-noosfero-api/interfaces/block.ts
src/plugins/comment_paragraph/block/discussion/discussion-block.component.spec.ts
0 → 100644
... | ... | @@ -0,0 +1,54 @@ |
1 | +import {TestComponentBuilder} from 'ng-forward/cjs/testing/test-component-builder'; | |
2 | +import {Provider, Input, provide, Component} from 'ng-forward'; | |
3 | +import {provideFilters} from '../../../../spec/helpers'; | |
4 | +import {DiscussionBlockComponent} from './discussion-block.component'; | |
5 | +import {ComponentTestHelper, createClass} from './../../../../spec/component-test-helper'; | |
6 | + | |
7 | +const htmlTemplate: string = '<noosfero-comment-paragraph-plugin-discussion-block [block]="ctrl.block" [owner]="ctrl.owner"></noosfero-comment-paragraph-plugin-discussion-block>'; | |
8 | + | |
9 | +const tcb = new TestComponentBuilder(); | |
10 | + | |
11 | +describe("Components", () => { | |
12 | + describe("Discussion Block Component", () => { | |
13 | + | |
14 | + let helper: ComponentTestHelper<DiscussionBlockComponent>; | |
15 | + let settingsObj = {}; | |
16 | + let mockedBlockService = { | |
17 | + getApiContent: (block: noosfero.Block): any => { | |
18 | + return Promise.resolve({ articles: [{ name: "article1" }], headers: (name: string) => { return name; } }); | |
19 | + } | |
20 | + }; | |
21 | + let profile = { name: 'profile-name' }; | |
22 | + | |
23 | + let state = jasmine.createSpyObj("state", ["go"]); | |
24 | + | |
25 | + let providers = [ | |
26 | + new Provider('$state', { useValue: state }), | |
27 | + new Provider('BlockService', { | |
28 | + useValue: mockedBlockService | |
29 | + }), | |
30 | + ].concat(provideFilters("truncateFilter", "stripTagsFilter", "translateFilter", "amDateFormatFilter")); | |
31 | + | |
32 | + beforeEach(angular.mock.module("templates")); | |
33 | + | |
34 | + beforeEach((done) => { | |
35 | + let cls = createClass({ | |
36 | + template: htmlTemplate, | |
37 | + directives: [DiscussionBlockComponent], | |
38 | + providers: providers, | |
39 | + properties: {} | |
40 | + }); | |
41 | + helper = new ComponentTestHelper<DiscussionBlockComponent>(cls, done); | |
42 | + }); | |
43 | + | |
44 | + it("get discussions from the block service", () => { | |
45 | + expect(helper.component.documents).toEqual([{ name: "article1" }]); | |
46 | + }); | |
47 | + | |
48 | + it("go to article page when open a document", () => { | |
49 | + let block = helper.component; | |
50 | + block.openDocument({ path: "path", profile: { identifier: "identifier" } }); | |
51 | + expect(state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "identifier" }); | |
52 | + }); | |
53 | + }); | |
54 | +}); | ... | ... |
src/plugins/comment_paragraph/block/discussion/discussion-block.component.ts
1 | 1 | import {Component, Inject, Input} from "ng-forward"; |
2 | -import {ArticleService} from "../../../../lib/ng-noosfero-api/http/article.service"; | |
2 | +import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; | |
3 | 3 | |
4 | 4 | @Component({ |
5 | 5 | selector: "noosfero-comment-paragraph-plugin-discussion-block", |
6 | 6 | templateUrl: 'plugins/comment_paragraph/block/discussion/discussion-block.html' |
7 | 7 | }) |
8 | -@Inject(ArticleService, "$state") | |
8 | +@Inject(BlockService, "$state") | |
9 | 9 | export class DiscussionBlockComponent { |
10 | 10 | |
11 | 11 | @Input() block: any; |
12 | 12 | @Input() owner: any; |
13 | 13 | |
14 | - profile: any; | |
15 | - documents: any; | |
14 | + profile: noosfero.Profile; | |
15 | + documents: Array<noosfero.Article>; | |
16 | 16 | |
17 | - documentsLoaded: boolean = false; | |
18 | - | |
19 | - constructor(private articleService: ArticleService, private $state: any) { } | |
17 | + constructor(private blockService: BlockService, private $state: any) { } | |
20 | 18 | |
21 | 19 | ngOnInit() { |
22 | 20 | this.profile = this.owner; |
23 | - this.documents = []; | |
24 | - | |
25 | - let limit = ((this.block && this.block.settings) ? this.block.settings.limit : null) || 50; | |
26 | - let params: any = { content_type: 'CommentParagraphPlugin::Discussion', per_page: limit, order: 'start_date DESC' }; | |
27 | - let now = new Date().toISOString(); | |
28 | - switch (this.block.settings['discussion_status']) { | |
29 | - case 0: | |
30 | - params['from_start_date'] = now; | |
31 | - break; | |
32 | - case 1: | |
33 | - params['until_start_date'] = now; | |
34 | - params['from_end_date'] = now; | |
35 | - break; | |
36 | - case 2: | |
37 | - params['until_end_date'] = now; | |
38 | - break; | |
39 | - } | |
40 | - console.log(this.block.settings['discussion_status']); | |
41 | - this.articleService.getByProfile(this.profile, params) | |
42 | - .then((result: noosfero.RestResult<noosfero.Article[]>) => { | |
43 | - this.documents = <noosfero.Article[]>result.data; | |
44 | - this.documentsLoaded = true; | |
45 | - }); | |
21 | + this.blockService.getApiContent(this.block).then((content: any) => { | |
22 | + this.documents = content.articles; | |
23 | + }); | |
46 | 24 | } |
47 | 25 | |
48 | 26 | openDocument(article: any) { | ... | ... |