From 3151a6e39770c2882845203f5827fac8bca5cad3 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 8 Mar 2016 09:31:30 -0300 Subject: [PATCH] Added tests for article service --- src/lib/ng-noosfero-api/http/article.service.spec.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/ng-noosfero-api/http/article.service.ts | 12 ++++++------ 2 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 src/lib/ng-noosfero-api/http/article.service.spec.ts diff --git a/src/lib/ng-noosfero-api/http/article.service.spec.ts b/src/lib/ng-noosfero-api/http/article.service.spec.ts new file mode 100644 index 0000000..c112d24 --- /dev/null +++ b/src/lib/ng-noosfero-api/http/article.service.spec.ts @@ -0,0 +1,76 @@ +import {Article} from "../../../app/models/interfaces"; +import {ArticleService} from "./article.service"; + + +describe("Services", () => { + + describe("Article Service", () => { + + let $httpBackend: ng.IHttpBackendService; + let articleService: ArticleService; + + beforeEach(angular.mock.module("noosferoApp")); + + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ArticleService_: ArticleService) => { + $httpBackend = _$httpBackend_; + articleService = _ArticleService_; + })); + + + describe("Succesfull requests", () => { + + it("should return article children", (done) => { + let articleId = 1; + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children`).respond(200, { articles: [{ name: "article1" }] }); + articleService.getChildren(articleId).then((response: restangular.IResponse) => { + expect(response.data.articles).toEqual([{ name: "article1" }]); + done(); + }); + $httpBackend.flush(); + }); + + it("should get articles by profile", (done) => { + let profileId = 1; + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles`).respond(200, { articles: [{ name: "article1" }] }); + articleService.getByProfile(profileId).then((response: restangular.IResponse) => { + expect(response.data.articles).toEqual([{ name: "article1" }]); + done(); + }); + $httpBackend.flush(); + }); + + it("should get articles by profile with additional filters", (done) => { + let profileId = 1; + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles?path=test`).respond(200, { articles: [{ name: "article1" }] }); + articleService.getByProfile(profileId, { path: 'test' }).then((response: restangular.IResponse) => { + expect(response.data.articles).toEqual([{ name: "article1" }]); + done(); + }); + $httpBackend.flush(); + }); + + it("should get article children with additional filters", (done) => { + let articleId = 1; + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children?path=test`).respond(200, { articles: [{ name: "article1" }] }); + articleService.getChildren(articleId, { path: 'test' }).then((response: restangular.IResponse) => { + expect(response.data.articles).toEqual([{ name: "article1" }]); + done(); + }); + $httpBackend.flush(); + }); + + it("should create an article in a profile", (done) => { + let profileId = 1; + let article: Article = { id: null }; + $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { articles: [{ id: 2 }] }); + articleService.create(profileId, article).then((response: restangular.IResponse) => { + expect(response.data.articles).toEqual([{ id: 2 }]); + done(); + }); + $httpBackend.flush(); + }); + }); + + + }); +}); diff --git a/src/lib/ng-noosfero-api/http/article.service.ts b/src/lib/ng-noosfero-api/http/article.service.ts index c0a3acb..8b78098 100644 --- a/src/lib/ng-noosfero-api/http/article.service.ts +++ b/src/lib/ng-noosfero-api/http/article.service.ts @@ -16,16 +16,16 @@ export class ArticleService { ); } - get(articleId: number) { - return this.Restangular.one('articles', articleId); + getByProfile(profileId: number, params?: any) { + return this.Restangular.one('profiles', profileId).customGET('articles', params); } - getByProfile(profileId: number, filters: any) { - return this.Restangular.one('profiles', profileId).customGET('articles', filters); + getChildren(articleId: number, params?: any) { + return this.get(articleId).customGET('children', params); } - getChildren(articleId: number, options: any = {}) { - return this.get(articleId).customGET('children', options); + private get(articleId: number) { + return this.Restangular.one('articles', articleId); } } -- libgit2 0.21.2