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
|
/**
* SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2015 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
describe('OCA.Files.NewFileMenu', function() {
var FileList = OCA.Files.FileList;
var menu, fileList, $uploadField, $trigger;
beforeEach(function() {
// dummy upload button
var $container = $('<div id="app-content-files"></div>');
$uploadField = $('<input id="file_upload_start"></input>');
$trigger = $('<a href="#">Menu</a>');
$container.append($uploadField).append($trigger);
$('#testArea').append($container);
fileList = new FileList($container);
menu = new OCA.Files.NewFileMenu({
fileList: fileList
});
menu.showAt($trigger);
});
afterEach(function() {
OC.hideMenus();
fileList = null;
menu = null;
});
describe('rendering', function() {
it('renders menu items', function() {
var $items = menu.$el.find('.menuitem');
expect($items.length).toEqual(2);
// label points to the file_upload_start item
var $item = $items.eq(0);
expect($item.is('label')).toEqual(true);
expect($item.attr('for')).toEqual('file_upload_start');
});
});
describe('New file/folder', function() {
var $input;
var createDirectoryStub;
beforeEach(function() {
createDirectoryStub = sinon.stub(FileList.prototype, 'createDirectory');
menu.$el.find('.menuitem').eq(1).click();
$input = menu.$el.find('form.filenameform input');
});
afterEach(function() {
createDirectoryStub.restore();
});
it('sets default text in field', function() {
// text + submit
expect($input.length).toEqual(2);
expect($input.val()).toEqual('New folder');
});
it('prevents entering invalid file names', function() {
$input.val('..');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();
expect(createDirectoryStub.notCalled).toEqual(true);
});
it('prevents entering file names that already exist', function() {
var inListStub = sinon.stub(fileList, 'inList').returns(true);
$input.val('existing.txt');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();
expect(createDirectoryStub.notCalled).toEqual(true);
inListStub.restore();
});
it('creates directory when clicking on create directory field', function() {
$input = menu.$el.find('form.filenameform input');
$input.val('some folder');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();
expect(createDirectoryStub.calledOnce).toEqual(true);
expect(createDirectoryStub.getCall(0).args[0]).toEqual('some folder');
});
});
describe('custom entries', function() {
var oldPlugins;
var plugin;
var actionStub;
beforeEach(function() {
oldPlugins = _.extend({}, OC.Plugins._plugins);
actionStub = sinon.stub();
plugin = {
attach: function(menu) {
menu.addMenuEntry({
id: 'file',
displayName: t('files_texteditor', 'Text file'),
templateName: t('files_texteditor', 'New text file.txt'),
iconClass: 'icon-filetype-text',
fileType: 'file',
actionHandler: actionStub
});
}
};
OC.Plugins.register('OCA.Files.NewFileMenu', plugin);
menu = new OCA.Files.NewFileMenu({
fileList: fileList
});
menu.showAt($trigger);
});
afterEach(function() {
OC.Plugins._plugins = oldPlugins;
});
it('renders custom menu items', function() {
expect(menu.$el.find('.menuitem').length).toEqual(3);
expect(menu.$el.find('.menuitem[data-action=file]').length).toEqual(1);
});
it('calls action handler when clicking on custom item', function() {
menu.$el.find('.menuitem').eq(2).click();
var $input = menu.$el.find('form.filenameform input');
$input.val('some name');
$input.trigger(new $.Event('keyup', {keyCode: 13}));
$input.closest('form').submit();
expect(actionStub.calledOnce).toEqual(true);
expect(actionStub.getCall(0).args[0]).toEqual('some name');
});
it('switching fields removes the previous form', function() {
menu.$el.find('.menuitem').eq(2).click();
expect(menu.$el.find('form').length).toEqual(1);
});
});
});
|