newfilemenuSpec.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @copyright 2015 Vincent Petry <pvince81@owncloud.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. * @author Vincent Petry <vincent@nextcloud.com>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. describe('OCA.Files.NewFileMenu', function() {
  24. var FileList = OCA.Files.FileList;
  25. var menu, fileList, $uploadField, $trigger;
  26. beforeEach(function() {
  27. // dummy upload button
  28. var $container = $('<div id="app-content-files"></div>');
  29. $uploadField = $('<input id="file_upload_start"></input>');
  30. $trigger = $('<a href="#">Menu</a>');
  31. $container.append($uploadField).append($trigger);
  32. $('#testArea').append($container);
  33. fileList = new FileList($container);
  34. menu = new OCA.Files.NewFileMenu({
  35. fileList: fileList
  36. });
  37. menu.showAt($trigger);
  38. });
  39. afterEach(function() {
  40. OC.hideMenus();
  41. fileList = null;
  42. menu = null;
  43. });
  44. describe('rendering', function() {
  45. it('renders menu items', function() {
  46. var $items = menu.$el.find('.menuitem');
  47. expect($items.length).toEqual(2);
  48. // label points to the file_upload_start item
  49. var $item = $items.eq(0);
  50. expect($item.is('label')).toEqual(true);
  51. expect($item.attr('for')).toEqual('file_upload_start');
  52. });
  53. });
  54. describe('New file/folder', function() {
  55. var $input;
  56. var createDirectoryStub;
  57. beforeEach(function() {
  58. createDirectoryStub = sinon.stub(FileList.prototype, 'createDirectory');
  59. menu.$el.find('.menuitem').eq(1).click();
  60. $input = menu.$el.find('form.filenameform input');
  61. });
  62. afterEach(function() {
  63. createDirectoryStub.restore();
  64. });
  65. it('sets default text in field', function() {
  66. // text + submit
  67. expect($input.length).toEqual(2);
  68. expect($input.val()).toEqual('New folder');
  69. });
  70. it('prevents entering invalid file names', function() {
  71. $input.val('..');
  72. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  73. $input.closest('form').submit();
  74. expect(createDirectoryStub.notCalled).toEqual(true);
  75. });
  76. it('prevents entering file names that already exist', function() {
  77. var inListStub = sinon.stub(fileList, 'inList').returns(true);
  78. $input.val('existing.txt');
  79. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  80. $input.closest('form').submit();
  81. expect(createDirectoryStub.notCalled).toEqual(true);
  82. inListStub.restore();
  83. });
  84. it('creates directory when clicking on create directory field', function() {
  85. $input = menu.$el.find('form.filenameform input');
  86. $input.val('some folder');
  87. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  88. $input.closest('form').submit();
  89. expect(createDirectoryStub.calledOnce).toEqual(true);
  90. expect(createDirectoryStub.getCall(0).args[0]).toEqual('some folder');
  91. });
  92. });
  93. describe('custom entries', function() {
  94. var oldPlugins;
  95. var plugin;
  96. var actionStub;
  97. beforeEach(function() {
  98. oldPlugins = _.extend({}, OC.Plugins._plugins);
  99. actionStub = sinon.stub();
  100. plugin = {
  101. attach: function(menu) {
  102. menu.addMenuEntry({
  103. id: 'file',
  104. displayName: t('files_texteditor', 'Text file'),
  105. templateName: t('files_texteditor', 'New text file.txt'),
  106. iconClass: 'icon-filetype-text',
  107. fileType: 'file',
  108. actionHandler: actionStub
  109. });
  110. }
  111. };
  112. OC.Plugins.register('OCA.Files.NewFileMenu', plugin);
  113. menu = new OCA.Files.NewFileMenu({
  114. fileList: fileList
  115. });
  116. menu.showAt($trigger);
  117. });
  118. afterEach(function() {
  119. OC.Plugins._plugins = oldPlugins;
  120. });
  121. it('renders custom menu items', function() {
  122. expect(menu.$el.find('.menuitem').length).toEqual(3);
  123. expect(menu.$el.find('.menuitem[data-action=file]').length).toEqual(1);
  124. });
  125. it('calls action handler when clicking on custom item', function() {
  126. menu.$el.find('.menuitem').eq(2).click();
  127. var $input = menu.$el.find('form.filenameform input');
  128. $input.val('some name');
  129. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  130. $input.closest('form').submit();
  131. expect(actionStub.calledOnce).toEqual(true);
  132. expect(actionStub.getCall(0).args[0]).toEqual('some name');
  133. });
  134. it('switching fields removes the previous form', function() {
  135. menu.$el.find('.menuitem').eq(2).click();
  136. expect(menu.$el.find('form').length).toEqual(1);
  137. });
  138. });
  139. });