newfilemenuSpec.js 4.6 KB

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