-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdocuments-test.js
359 lines (302 loc) · 11.1 KB
/
documents-test.js
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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import {
visit,
currentURL,
click,
fillIn,
triggerEvent,
settled,
waitFor,
} from "@ember/test-helpers";
import { setupApplicationTest } from "dummy/tests/helpers";
import { setupMirage } from "ember-cli-mirage/test-support";
import { setLocale, t } from "ember-intl/test-support";
import * as fileSaver from "file-saver";
import { module, test } from "qunit";
import { stub } from "sinon";
import setupRequestAssertions from "../helpers/assert-request";
module("Acceptance | documents", function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
setupRequestAssertions(hooks);
// Mirage relationships are not working properly
test("document grid displays documents", async function (assert) {
const documents = this.server.createList("document", 5);
const file = this.server.create("file", {
variant: "original",
downloadUrl: "test-thumbnail",
});
file.update({
renderings: [
this.server.create("file", {
variant: "thumbnail",
downloadUrl: "test-thumbnail",
}),
],
});
documents[1].update({
files: [file],
});
await visit("/");
await click("[data-test-toggle-side-panel]");
assert.dom("[data-test-document]").exists({ count: 5 });
assert
.dom("[data-test-document-container]:first-child [data-test-title]")
.hasText(documents[0].title);
assert
.dom("[data-test-document-container]:first-child [data-test-thumbnail]")
.doesNotExist();
assert
.dom("[data-test-document-container]:nth-child(2) [data-test-title]")
.hasText(documents[1].title);
assert
.dom("[data-test-document-container]:nth-child(2) [data-test-thumbnail]")
.hasAttribute("data-src", "test-thumbnail");
});
test("select document in the grid view", async function (assert) {
const [document] = this.server.createList("document", 2);
await visit("/");
await click("[data-test-toggle-side-panel]");
assert.dom("[data-test-single-doc-details]").isNotVisible();
assert
.dom("[data-test-document-container]:first-child [data-test-document]")
.doesNotHaveClass("document-card--selected");
await click(
"[data-test-document-container]:first-child [data-test-document]",
);
assert.strictEqual(
currentURL(),
`/?document=${document.id}&listView=false`,
"url is set to currently selected document",
);
assert.dom("[data-test-single-doc-details]").isVisible();
assert
.dom("[data-test-document-container]:first-child [data-test-document]")
.hasClass("document-card--selected");
assert
.dom("[data-test-single-doc-details] [data-test-title]")
.hasText(document.title);
await click("[data-test-close]");
assert.dom("[data-test-document-side-panel]").hasClass("closed");
});
test("document detail edit title", async function (assert) {
const document = this.server.create("document");
await visit(`/`);
await click("[data-test-toggle-side-panel]");
setLocale("en");
await click(
"[data-test-document-container]:first-child [data-test-document]",
);
assert
.dom("[data-test-single-doc-details] [data-test-title]")
.hasText(document.title);
assert.dom("[data-test-title-input]").doesNotExist();
await click("[data-test-single-doc-details] [data-test-edit-title]");
assert.dom("[data-test-title-input]").hasValue(document.title);
await fillIn("[data-test-title-input]", "new title");
this.assertRequest("PATCH", "/api/v1/documents/:id", (request) => {
assert.strictEqual(
request.params.id,
document.id,
"patching the correct document",
);
assert.strictEqual(
JSON.parse(request.requestBody).data.attributes.title,
"new title",
"new title is set",
);
});
await click("[data-test-single-doc-details] [data-test-save]");
assert.dom("[data-test-title-input]").doesNotExist();
});
test("document detail delete", async function (assert) {
const document = this.server.create("document");
await visit(`/`);
await click("[data-test-document-list-item]:first-of-type");
setTimeout(() => {}, 2000);
this.assertRequest("DELETE", "/api/v1/documents/:id", (request) => {
assert.strictEqual(
request.params.id,
document.id,
"deleting the correct document",
);
});
await click("[data-test-single-doc-details] [data-test-delete]");
await click("[data-test-delete-confirm]");
assert.strictEqual(currentURL(), "/", "document is removed from url");
assert.dom("[data-test-document]").doesNotExist();
});
test("upload file", async function (assert) {
this.server.create("category");
await visit("/?category=1");
setLocale("en");
assert.dom("[data-test-document]").doesNotExist();
this.assertRequest("POST", "/api/v1/documents", (request) => {
request.requestBody
.get("data")
.text()
.then((data) => {
assert.strictEqual(
JSON.parse(data).title,
"test-file.txt",
"correct title is set",
);
});
});
await triggerEvent("[data-test-upload] [data-test-input]", "change", {
files: [
new File(["Ember Rules!"], "test-file.txt", { type: "text/plain" }),
],
});
assert.dom("[data-test-document-list-item]").exists({ count: 1 });
});
test("replace file", async function (assert) {
const document = this.server.create("document");
await visit(`/?document=${document.id}`);
assert.dom("[data-test-file]").doesNotExist();
this.assertRequest("POST", "/api/v1/files", (request) => {
const name = request.requestBody.get("name");
const variant = request.requestBody.get("variant");
assert.strictEqual(name, "test-file.txt");
assert.strictEqual(variant, "original");
});
await triggerEvent("[data-test-replace]", "change", {
files: [new File(["Ember Rules!"], "test-file.txt")],
});
assert.dom("[data-test-file]").exists({ count: 1 });
});
test("context menu delete", async function (assert) {
this.server.createList("document", 5);
await visit("/");
await click("[data-test-toggle-side-panel]");
assert.dom("[data-test-document]").exists({ count: 5 });
await click(
"[data-test-document]:first-child [data-test-context-menu-trigger]",
);
this.assertRequest("DELETE", "/api/v1/documents/:id", (request) => {
assert.strictEqual(
request.params.id,
"1",
"deleting the correct document",
);
});
await click(
"[data-test-document]:first-child [data-test-context-menu] [data-test-delete]",
);
await click("[data-test-delete-confirm]");
assert.dom("[data-test-document]").exists({ count: 4 });
});
test("downloading multiple documents as a zip", async function (assert) {
this.server
.createList("document", 5)
.forEach((document) => this.server.create("file", { document }));
const fileSaverStub = stub(fileSaver, "saveAs");
await visit("/");
await click("[data-test-toggle-side-panel]");
await click("[data-test-document]", { shiftKey: true });
await click(
"[data-test-document-container]:nth-child(3) [data-test-document]",
{ shiftKey: true },
);
assert
.dom("[data-test-download-button]")
.hasText(t("alexandria.document-download.button", { numDocs: 3 }));
this.assertRequest("GET", "/api/v1/files/multi", (request) => {
assert.strictEqual(
request.queryParams["filter[files]"],
"1,2,3",
"requesting the correct documents as a zip",
);
});
await click("[data-test-download-button]");
// eslint-disable-next-line ember/no-settled-after-test-helper
await settled();
assert.true(fileSaverStub.args[0][0] instanceof Blob);
assert.strictEqual(fileSaverStub.args[0][1], "Download-3-files.zip");
});
test("selecting documents with CTRL A", async function (assert) {
this.server.createList("document", 3);
await visit("/");
window.dispatchEvent(
new KeyboardEvent("keydown", { key: "a", ctrlKey: true }),
);
await triggerEvent(window, "keydown", "a", {
ctrlKey: true,
});
assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 3 });
});
test.skip("deselecting documents with Escape", async function (assert) {
this.server.createList("document", 3);
await visit("/");
await click("[data-test-document-list-item]:first-child");
assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 1 });
// window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
await triggerEvent("[data-test-document-list-item]", "keydown", "Escape");
// eslint-disable-next-line ember/no-settled-after-test-helper
await settled();
assert
.dom("[data-test-document-list-item].document-list-item--selected")
.doesNotExist();
});
test.each(
`trigger document delete with keyboard shortcut`,
["single", "multiple"],
async function (assert, type) {
const documents = this.server.createList("document", 3);
function isVisible(element) {
if (!element) {
return false;
}
const style = window.getComputedStyle(element);
return style.display !== "none";
}
await visit("/");
assert.dom(".uk-modal").doesNotExist();
if (type === "single") {
// select first document and check the single document details
await click("[data-test-document-list-item]:first-child");
assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 1 });
assert.dom("[data-test-single-doc-details]").exists();
} else {
// select multiple documents and check the multi document details
await click("[data-test-document-list-item]:first-child", {
shiftKey: true,
});
await click("[data-test-document-list-item]:nth-child(3)", {
shiftKey: true,
});
assert
.dom("[data-test-document-list-item].document-list-item--selected")
.exists({ count: 3 });
assert.dom("[data-test-multi-doc-details]").exists();
}
// assert modal visibility on keyboard shortcut
await waitFor(".uk-modal");
assert.dom(".uk-modal").exists({ count: 1 });
const modal = document.querySelector(".uk-modal");
assert.false(isVisible(modal));
window.dispatchEvent(new KeyboardEvent("keydown", { key: "Delete" }));
await triggerEvent(window, "keydown", "Delete");
assert.ok(isVisible(modal));
// assert modal body text
if (type === "single") {
assert
.dom(modal)
.containsText(
`Do you really want to delete the document "${documents[0].title}" and its complete history?`,
);
} else {
assert
.dom(modal)
.containsText(
"Do you really want to delete 3 documents and their complete history?",
);
}
},
);
});