profile.service.spec.ts
7.86 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {ProfileService} from "./profile.service";
describe("Services", () => {
describe("Profile Service", () => {
let $httpBackend: ng.IHttpBackendService;
let profileService: ProfileService;
let $rootScope: ng.IRootScopeService;
beforeEach(angular.mock.module("main", ($translateProvider: angular.translate.ITranslateProvider) => {
$translateProvider.translations('en', {});
}));
beforeEach(inject((_$httpBackend_: ng.IHttpBackendService, _ProfileService_: ProfileService, _$rootScope_: ng.IRootScopeService) => {
$httpBackend = _$httpBackend_;
profileService = _ProfileService_;
$rootScope = _$rootScope_;
}));
describe("Succesfull requests", () => {
it("should return profile by its identifier", (done) => {
let identifier = 'profile1';
$httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, [{ name: "profile1" }]);
profileService.getByIdentifier(identifier).then((profile: noosfero.Profile) => {
expect(profile).toEqual({ name: "profile1" });
done();
});
$httpBackend.flush();
});
it("should reject the promise if the profile wasn't found", (done) => {
let identifier = 'profile1';
$httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, []);
profileService.getByIdentifier(identifier).catch(() => {
done();
});
$httpBackend.flush();
});
it("should return the members of a profile", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/members`).respond(200, [{ name: "profile1" }]);
profileService.getProfileMembers(profileId).then((response: restangular.IResponse) => {
expect(response.data[0]).toEqual({ name: "profile1" });
done();
});
$httpBackend.flush();
});
it("should return the boxes of a profile", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/boxes`).respond(200, [{ position: 1 }]);
profileService.getBoxes(profileId).then((response: restangular.IResponse) => {
expect(response.data[0]).toEqual({ position: 1 });
done();
});
$httpBackend.flush();
});
it("should return activities of a profile", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/activities`).respond(200, [{ verb: "create_article" }]);
profileService.getActivities(profileId).then((response: restangular.IResponse) => {
expect(response.data[0]).toEqual({ verb: "create_article" });
done();
});
$httpBackend.flush();
});
it("should resolve the current profile", (done) => {
let profile = { id: 1, identifier: "profile1" };
profileService.getCurrentProfile().then((currentProfile: noosfero.Profile) => {
expect(currentProfile).toEqual(currentProfile);
done();
});
profileService.setCurrentProfile(<any>profile);
$rootScope.$apply();
});
it("should return the profile home page", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/home_page`).respond(200, { article: { path: "/something" } });
profileService.getHomePage(profileId).then((response: restangular.IResponse) => {
expect(response.data.article).toEqual({ path: "/something" });
done();
});
$httpBackend.flush();
});
it("should find the profile by identifier, set and resolve the current profile", (done) => {
let identifier = 'profile1';
$httpBackend.expectGET(`/api/v1/profiles?identifier=${identifier}`).respond(200, [{ name: "profile1" }]);
profileService.setCurrentProfileByIdentifier(identifier).then((profile: noosfero.Profile) => {
expect(profile).toEqual({ name: "profile1" });
profileService.getCurrentProfile().then((profile: noosfero.Profile) => {
expect(profile).toEqual({ name: "profile1" });
done();
});
});
$httpBackend.flush();
});
it("should update the profile attributes", (done) => {
let profileId = 1;
$httpBackend.expectPOST(`/api/v1/profiles/${profileId}`).respond(200, { profile: { custom_header: "something" } });
profileService.update(<any>{ id: profileId, custom_header: "something" }).then((response: restangular.IResponse) => {
expect(response.data.profile.custom_header).toEqual("something");
done();
});
$httpBackend.flush();
});
it("should return the profile members", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/members`).respond(200, { people: [{ id: 2 }] });
profileService.getMembers(<any>{ id: profileId }).then((response: restangular.IResponse) => {
expect(response.data.people).toEqual([{ id: 2 }]);
done();
});
$httpBackend.flush();
});
it("should return true if the person is a profile member", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/members`).respond(200, { people: [{ id: 2 }] });
profileService.isMember(<any>{ id: 2 }, <any>{ id: profileId }).then((response: restangular.IResponse) => {
expect(response).toEqual(true);
done();
});
$httpBackend.flush();
});
it("should return false if the person is a profile member", (done) => {
let profileId = 1;
$httpBackend.expectGET(`/api/v1/profiles/${profileId}/members`).respond(200, { people: [] });
profileService.isMember(<any>{ id: 2 }, <any>{ id: profileId }).then((response: restangular.IResponse) => {
expect(response).toEqual(false);
done();
});
$httpBackend.flush();
});
it("should add member to profile", (done) => {
let profileId = 1;
$httpBackend.expectPOST(`/api/v1/profiles/${profileId}/members`).respond(200, { pending: false });
profileService.addMember(<any>{ id: 2 }, <any>{ id: profileId }).then((response: restangular.IResponse) => {
expect(response.data.pending).toEqual(false);
done();
});
$httpBackend.flush();
});
it("should remove member from profile", (done) => {
let profileId = 1;
$httpBackend.expectDELETE(`/api/v1/profiles/${profileId}/members`).respond(200, { person: { id: 2 } });
profileService.removeMember(<any>{ id: 2 }, <any>{ id: profileId }).then((response: restangular.IResponse) => {
expect(response.data.person).toEqual({ id: 2 });
done();
});
$httpBackend.flush();
});
});
});
});