article.service.spec.ts
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {ArticleService} from "./article.service";
describe("Services", () => {
describe("Article Service", () => {
let $httpBackend: ng.IHttpBackendService;
let articleService: ArticleService;
beforeEach(angular.mock.module("main", ($translateProvider: angular.translate.ITranslateProvider) => {
$translateProvider.translations('en', {});
}));
beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ArticleService_: ArticleService) => {
$httpBackend = _$httpBackend_;
articleService = _ArticleService_;
}));
describe("Succesfull requests", () => {
it("should remove article", (done) => {
let articleId = 1;
$httpBackend.expectDELETE(`/api/v1/articles/${articleId}`).respond(200, { success: "true" });
articleService.remove(<noosfero.Article>{ id: articleId });
$httpBackend.flush();
$httpBackend.verifyNoOutstandingExpectation();
done();
});
it("should return article children", (done) => {
let articleId = 1;
$httpBackend.expectGET(`/api/v1/articles/${articleId}/children`).respond(200, { articles: [{ name: "article1" }] });
articleService.getChildren(<noosfero.Article>{ id: articleId }).then((result: noosfero.RestResult<noosfero.Article[]>) => {
expect(result.data).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(<noosfero.Profile>{ id: profileId }).then((result: noosfero.RestResult<noosfero.Article[]>) => {
expect(result.data).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(<noosfero.Profile>{ id: profileId }, { path: 'test' }).then((result: noosfero.RestResult<noosfero.Article[]>) => {
expect(result.data).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(<noosfero.Article>{ id: articleId }, { path: 'test' }).then((result: noosfero.RestResult<noosfero.Article[]>) => {
expect(result.data).toEqual([{ name: "article1" }]);
done();
});
$httpBackend.flush();
});
it("should create an article in a profile", (done) => {
let profileId = 1;
let article: noosfero.Article = <any>{ id: null };
$httpBackend.expectPOST(`/api/v1/profiles/${profileId}/articles`, { article: article }).respond(200, { article: { id: 2 } });
articleService.createInProfile(<noosfero.Profile>{ id: profileId }, article).then((result: noosfero.RestResult<noosfero.Article>) => {
expect(result.data).toEqual({ id: 2 });
done();
});
$httpBackend.flush();
});
it("should search for articles in environment", (done) => {
let profileId = 1;
let article: noosfero.Article = <any>{ id: null };
$httpBackend.expectGET(`/api/v1/search/article?query=query`).respond(200, { articles: [{ id: 2 }] });
articleService.search({ query: 'query' }).then((result: noosfero.RestResult<noosfero.Article[]>) => {
expect(result.data).toEqual([{ id: 2 }]);
done();
});
$httpBackend.flush();
});
});
});
});