article-default-view-component.spec.ts
5.31 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
import {Input, provide, Component} from 'ng-forward';
import {ArticleViewComponent, ArticleDefaultViewComponent} from './article-default-view.component';
import {ComponentTestHelper, createClass} from './../../spec/component-test-helper';
import * as helpers from "../../spec/helpers";
// this htmlTemplate will be re-used between the container components in this spec file
const htmlTemplate: string = '<noosfero-article [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-article>';
describe("Components", () => {
// the karma preprocessor html2js transform the templates html into js files which put
// the templates to the templateCache into the module templates
// we need to load the module templates here as the template for the
// component Noosfero ArtileView will be load on our tests
beforeEach(angular.mock.module("templates"));
describe("Article Default View Component", () => {
let helper: ComponentTestHelper<ArticleDefaultViewComponent>;
const defaultViewTemplate: string = '<noosfero-default-article [article]="ctrl.article" [profile]="ctrl.profile"></noosfero-default-article>';
let notificationService = helpers.mocks.notificationService;
let articleService: any = helpers.mocks.articleService;
let article = <noosfero.Article>{
id: 1,
profile: {
identifier: "1"
}
};
let state = <ng.ui.IStateService>jasmine.createSpyObj("state", ["go", "transitionTo"]);
let providers = [
provide('$state', { useValue: state }),
provide('ArticleService', { useValue: articleService }),
helpers.createProviderToValue('NotificationService', notificationService),
].concat(helpers.provideFilters("translateFilter"));
/**
* The beforeEach procedure will initialize the helper and parse
* the component according to the given providers. Unfortunetly, in
* this mode, the providers and properties given to the construtor
* can't be overriden.
*/
beforeEach((done) => {
// Create the component bed for the test. Optionally, this could be done
// in each test if one needs customization of these parameters per test
let cls = createClass({
template: defaultViewTemplate,
directives: [ArticleDefaultViewComponent],
providers: providers,
properties: {
article: article
}
});
helper = new ComponentTestHelper<ArticleDefaultViewComponent>(cls, done);
});
function getArticle() {
return this.article;
}
it("it should delete article when delete is activated", () => {
expect(helper.component.article).toEqual(article);
// Spy the state service
doDeleteArticle();
expect(state.transitionTo).toHaveBeenCalled();
});
it("hide button to delete article when user doesn't have permission", () => {
expect(helper.find(".article-toolbar .delete-article").attr('style')).toEqual("display: none; ");
});
it("hide button to edit article when user doesn't have permission", () => {
expect(helper.find(".article-toolbar .edit-article").attr('style')).toEqual("display: none; ");
});
it("show button to edit article when user has permission", () => {
(<any>helper.component['article'])['permissions'] = ['allow_edit'];
helper.detectChanges();
expect(helper.find(".article-toolbar .edit-article").attr('style')).toEqual('');
});
it("show button to delete article when user has permission", () => {
(<any>helper.component['article'])['permissions'] = ['allow_delete'];
helper.detectChanges();
expect(helper.find(".article-toolbar .delete-article").attr('style')).toEqual('');
});
/**
* Execute the delete method on the target component
*/
function doDeleteArticle() {
// Create a mock for the notification service confirmation
spyOn(helper.component.notificationService, 'confirmation').and.callFake(function(params: Function) {
});
// Create a mock for the ArticleService removeArticle method
spyOn(helper.component.articleService, 'remove').and.callFake(function(param: noosfero.Article) {
return {
catch: () => { }
};
});
helper.component.delete();
expect(notificationService.confirmation).toHaveBeenCalled();
helper.component.doDelete();
expect(articleService.remove).toHaveBeenCalled();
// After the component delete method execution, fire the
// ArticleEvent.removed event
simulateRemovedEvent();
}
/**
* Simulate the Notification Service confirmation and ArticleService
* notifyArticleRemovedListeners event
*/
function simulateRemovedEvent() {
helper.component.notificationService["confirmation"]({ title: "Title", message: "Message" }, () => { });
helper.component.articleService["modelRemovedEventEmitter"].next(article);
}
});
});