basic-editor.component.spec.ts
3.61 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
import {quickCreateComponent} from "../../../spec/helpers";
import {BasicEditorComponent} from "./basic-editor.component";
describe("Article BasicEditor", () => {
let $rootScope: ng.IRootScopeService;
let $q: ng.IQService;
let articleServiceMock: any;
let profileServiceMock: any;
let $state: any;
let $stateParams: any;
let profile = { id: 1 };
let notification: any;
beforeEach(inject((_$rootScope_: ng.IRootScopeService, _$q_: ng.IQService) => {
$rootScope = _$rootScope_;
$q = _$q_;
}));
beforeEach(() => {
$state = jasmine.createSpyObj("$state", ["go"]);
$stateParams = jasmine.createSpyObj("$stateParams", ["parent_id", "profile"]);
notification = jasmine.createSpyObj("notification", ["success"]);
profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier"]);
articleServiceMock = jasmine.createSpyObj("articleServiceMock", ["createInParent", "get"]);
$stateParams.profile = jasmine.createSpy("profile").and.returnValue("profile");
let setCurrentProfileByIdentifierResponse = $q.defer();
setCurrentProfileByIdentifierResponse.resolve(profile);
let articleCreate = $q.defer();
articleCreate.resolve({ data: { path: "path", profile: { identifier: "profile" } } });
let articleGet = $q.defer();
articleGet.resolve({ data: { path: "parent-path", profile: { identifier: "profile" } } });
profileServiceMock.setCurrentProfileByIdentifier = jasmine.createSpy("setCurrentProfileByIdentifier").and.returnValue(setCurrentProfileByIdentifierResponse.promise);
articleServiceMock.createInParent = jasmine.createSpy("createInParent").and.returnValue(articleCreate.promise);
articleServiceMock.get = jasmine.createSpy("get").and.returnValue(articleGet.promise);
});
it("create an article in the current profile when save", done => {
let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams);
component.save();
$rootScope.$apply();
expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled();
expect(articleServiceMock.createInParent).toHaveBeenCalledWith($stateParams.parent_id, component.article);
done();
});
it("got to the new article page and display an alert when saving sucessfully", done => {
let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams);
component.save();
$rootScope.$apply();
expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "path", profile: "profile" });
expect(notification.success).toHaveBeenCalled();
done();
});
it("got to the parent article page when cancelled", done => {
let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams);
$rootScope.$apply();
component.cancel();
expect($state.go).toHaveBeenCalledWith("main.profile.page", { page: "parent-path", profile: $stateParams.profile });
done();
});
it("got to the profile home when cancelled and parent was not defined", done => {
let component: BasicEditorComponent = new BasicEditorComponent(articleServiceMock, profileServiceMock, $state, notification, $stateParams);
component.cancel();
expect($state.go).toHaveBeenCalledWith("main.profile.home", { profile: $stateParams.profile });
done();
});
});