Commit 3151a6e39770c2882845203f5827fac8bca5cad3

Authored by Victor Costa
1 parent e882651c

Added tests for article service

src/lib/ng-noosfero-api/http/article.service.spec.ts 0 → 100644
... ... @@ -0,0 +1,76 @@
  1 +import {Article} from "../../../app/models/interfaces";
  2 +import {ArticleService} from "./article.service";
  3 +
  4 +
  5 +describe("Services", () => {
  6 +
  7 + describe("Article Service", () => {
  8 +
  9 + let $httpBackend: ng.IHttpBackendService;
  10 + let articleService: ArticleService;
  11 +
  12 + beforeEach(angular.mock.module("noosferoApp"));
  13 +
  14 + beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ArticleService_: ArticleService) => {
  15 + $httpBackend = _$httpBackend_;
  16 + articleService = _ArticleService_;
  17 + }));
  18 +
  19 +
  20 + describe("Succesfull requests", () => {
  21 +
  22 + it("should return article children", (done) => {
  23 + let articleId = 1;
  24 + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children`).respond(200, { articles: [{ name: "article1" }] });
  25 + articleService.getChildren(articleId).then((response: restangular.IResponse) => {
  26 + expect(response.data.articles).toEqual([{ name: "article1" }]);
  27 + done();
  28 + });
  29 + $httpBackend.flush();
  30 + });
  31 +
  32 + it("should get articles by profile", (done) => {
  33 + let profileId = 1;
  34 + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles`).respond(200, { articles: [{ name: "article1" }] });
  35 + articleService.getByProfile(profileId).then((response: restangular.IResponse) => {
  36 + expect(response.data.articles).toEqual([{ name: "article1" }]);
  37 + done();
  38 + });
  39 + $httpBackend.flush();
  40 + });
  41 +
  42 + it("should get articles by profile with additional filters", (done) => {
  43 + let profileId = 1;
  44 + $httpBackend.expectGET(`/api/v1/profiles/${profileId}/articles?path=test`).respond(200, { articles: [{ name: "article1" }] });
  45 + articleService.getByProfile(profileId, { path: 'test' }).then((response: restangular.IResponse) => {
  46 + expect(response.data.articles).toEqual([{ name: "article1" }]);
  47 + done();
  48 + });
  49 + $httpBackend.flush();
  50 + });
  51 +
  52 + it("should get article children with additional filters", (done) => {
  53 + let articleId = 1;
  54 + $httpBackend.expectGET(`/api/v1/articles/${articleId}/children?path=test`).respond(200, { articles: [{ name: "article1" }] });
  55 + articleService.getChildren(articleId, { path: 'test' }).then((response: restangular.IResponse) => {
  56 + expect(response.data.articles).toEqual([{ name: "article1" }]);
  57 + done();
  58 + });
  59 + $httpBackend.flush();
  60 + });
  61 +
  62 + it("should create an article in a profile", (done) => {
  63 + let profileId = 1;
  64 + let article: Article = { id: null };
  65 + $httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { articles: [{ id: 2 }] });
  66 + articleService.create(profileId, article).then((response: restangular.IResponse) => {
  67 + expect(response.data.articles).toEqual([{ id: 2 }]);
  68 + done();
  69 + });
  70 + $httpBackend.flush();
  71 + });
  72 + });
  73 +
  74 +
  75 + });
  76 +});
... ...
src/lib/ng-noosfero-api/http/article.service.ts
... ... @@ -16,16 +16,16 @@ export class ArticleService {
16 16 );
17 17 }
18 18  
19   - get(articleId: number) {
20   - return this.Restangular.one('articles', articleId);
  19 + getByProfile(profileId: number, params?: any) {
  20 + return this.Restangular.one('profiles', profileId).customGET('articles', params);
21 21 }
22 22  
23   - getByProfile(profileId: number, filters: any) {
24   - return this.Restangular.one('profiles', profileId).customGET('articles', filters);
  23 + getChildren(articleId: number, params?: any) {
  24 + return this.get(articleId).customGET('children', params);
25 25 }
26 26  
27   - getChildren(articleId: number, options: any = {}) {
28   - return this.get(articleId).customGET('children', options);
  27 + private get(articleId: number) {
  28 + return this.Restangular.one('articles', articleId);
29 29 }
30 30  
31 31 }
... ...