Newer
Older
describe("app.views.AspectCreate", function() {
beforeEach(function() {
app.events.off("aspect:create");
// disable jshint camelcase for i18n
/* jshint camelcase: false */
Diaspora.I18n.load({
aspects: {
make_aspect_list_visible: "Make contacts in this aspect visible to each other?",
name: "Name",
create: {
add_a_new_aspect: "Add a new aspect",
success: "Your new aspect <%= name %> was created",
failure: "Aspect creation failed."
}
}
});
/* jshint camelcase: true */
});
context("without a person id", function() {
beforeEach(function() {
this.view = new app.views.AspectCreate();
});
describe("#render", function() {
beforeEach(function() {
this.view.render();
});
it("should show the aspect creation form inside a modal", function() {
expect(this.view.$("#newAspectModal.modal").length).toBe(1);
expect(this.view.$("#newAspectModal form").length).toBe(1);
expect(this.view.$("#newAspectModal input#aspect_name").length).toBe(1);
expect(this.view.$("#newAspectModal input#aspect_contacts_visible").length).toBe(1);
expect(this.view.$("#newAspectModal .btn-primary").length).toBe(1);
});
it("shouldn't show a hidden person id input", function() {
expect(this.view.$("#newAspectModal input#aspect_person_id").length).toBe(0);
});
});
describe("#inputKeypress", function() {
beforeEach(function() {
this.view.render();
spyOn(this.view, "createAspect");
});
it("should call createAspect if the enter key was pressed", function() {
var e = $.Event("keypress", { which: 13 });
this.view.inputKeypress(e);
expect(this.view.createAspect).toHaveBeenCalled();
});
it("shouldn't call createAspect if another key was pressed", function() {
var e = $.Event("keypress", { which: 42 });
this.view.inputKeypress(e);
expect(this.view.createAspect).not.toHaveBeenCalled();
});
});
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
describe("#createAspect", function() {
beforeEach(function() {
this.view.render();
});
it("should send the correct name to the server", function() {
var name = "New aspect name";
this.view.$("input#aspect_name").val(name);
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
expect(obj.name).toBe(name);
});
it("should send the correct contacts_visible to the server", function() {
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.contacts_visible).toBeFalsy();
/* jshint camelcase: true */
this.view.$("input#aspect_contacts_visible").prop("checked", true);
this.view.createAspect();
obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.contacts_visible).toBeTruthy();
/* jshint camelcase: true */
});
it("should send person_id = null to the server", function() {
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.person_id).toBe(null);
/* jshint camelcase: true */
});
context("with a successfull request", function() {
beforeEach(function() {
this.response = {
status: 200,
responseText: JSON.stringify({id: 1337, name: "new name"})
};
});
it("should hide the modal", function() {
expect(this.view.$(".modal")).toHaveClass("in");
this.view.createAspect();
jasmine.Ajax.requests.mostRecent().respondWith(this.response);
expect(this.view.$(".modal")).not.toHaveClass("in");
});
it("should display a flash message", function() {
this.view.createAspect();
jasmine.Ajax.requests.mostRecent().respondWith(this.response);
expect($("[id^=\"flash\"]")).toBeSuccessFlashMessage(
Diaspora.I18n.t("aspects.create.success", {name: "new name"})
);
});
});
context("with a failing request", function() {
beforeEach(function() {
this.response = { status: 422 };
});
it("should hide the modal", function() {
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
this.view.$(".modal").modal("show");
expect(this.view.$(".modal")).toHaveClass("in");
this.view.createAspect();
jasmine.Ajax.requests.mostRecent().respondWith(this.response);
expect(this.view.$(".modal")).not.toHaveClass("in");
});
it("should display a flash message", function() {
this.view.createAspect();
jasmine.Ajax.requests.mostRecent().respondWith(this.response);
expect($("[id^=\"flash\"]")).toBeErrorFlashMessage(
Diaspora.I18n.t("aspects.create.failure")
);
});
});
});
});
context("with a person id", function() {
beforeEach(function() {
this.view = new app.views.AspectCreate({personId: "42"});
});
describe("#render", function() {
beforeEach(function() {
this.view.render();
});
it("should show the aspect creation form inside a modal", function() {
expect(this.view.$("#newAspectModal.modal").length).toBe(1);
expect(this.view.$("#newAspectModal form").length).toBe(1);
expect(this.view.$("#newAspectModal input#aspect_name").length).toBe(1);
expect(this.view.$("#newAspectModal input#aspect_contacts_visible").length).toBe(1);
expect(this.view.$("#newAspectModal .btn-primary").length).toBe(1);
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
});
it("should show a hidden person id input", function() {
expect(this.view.$("#newAspectModal input#aspect_person_id").length).toBe(1);
expect(this.view.$("#newAspectModal input#aspect_person_id").prop("value")).toBe("42");
});
});
describe("#createAspect", function() {
beforeEach(function() {
this.view.render();
});
it("should send the correct name to the server", function() {
var name = "New aspect name";
this.view.$("input#aspect_name").val(name);
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
expect(obj.name).toBe(name);
});
it("should send the correct contacts_visible to the server", function() {
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.contacts_visible).toBeFalsy();
/* jshint camelcase: true */
this.view.$("input#aspect_contacts_visible").prop("checked", true);
this.view.createAspect();
obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.contacts_visible).toBeTruthy();
/* jshint camelcase: true */
});
it("should send the correct person_id to the server", function() {
this.view.createAspect();
var obj = JSON.parse(jasmine.Ajax.requests.mostRecent().params);
/* jshint camelcase: false */
expect(obj.person_id).toBe("42");
/* jshint camelcase: true */
});
});
});
});