1
0

newfilemenuSpec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. describe('OCA.Files.NewFileMenu', function() {
  7. var FileList = OCA.Files.FileList;
  8. var menu, fileList, $uploadField, $trigger;
  9. beforeEach(function() {
  10. // dummy upload button
  11. var $container = $('<div id="app-content-files"></div>');
  12. $uploadField = $('<input id="file_upload_start"></input>');
  13. $trigger = $('<a href="#">Menu</a>');
  14. $container.append($uploadField).append($trigger);
  15. $('#testArea').append($container);
  16. fileList = new FileList($container);
  17. menu = new OCA.Files.NewFileMenu({
  18. fileList: fileList
  19. });
  20. menu.showAt($trigger);
  21. });
  22. afterEach(function() {
  23. OC.hideMenus();
  24. fileList = null;
  25. menu = null;
  26. });
  27. describe('rendering', function() {
  28. it('renders menu items', function() {
  29. var $items = menu.$el.find('.menuitem');
  30. expect($items.length).toEqual(2);
  31. // label points to the file_upload_start item
  32. var $item = $items.eq(0);
  33. expect($item.is('label')).toEqual(true);
  34. expect($item.attr('for')).toEqual('file_upload_start');
  35. });
  36. });
  37. describe('New file/folder', function() {
  38. var $input;
  39. var createDirectoryStub;
  40. beforeEach(function() {
  41. createDirectoryStub = sinon.stub(FileList.prototype, 'createDirectory');
  42. menu.$el.find('.menuitem').eq(1).click();
  43. $input = menu.$el.find('form.filenameform input');
  44. });
  45. afterEach(function() {
  46. createDirectoryStub.restore();
  47. });
  48. it('sets default text in field', function() {
  49. // text + submit
  50. expect($input.length).toEqual(2);
  51. expect($input.val()).toEqual('New folder');
  52. });
  53. it('prevents entering invalid file names', function() {
  54. $input.val('..');
  55. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  56. $input.closest('form').submit();
  57. expect(createDirectoryStub.notCalled).toEqual(true);
  58. });
  59. it('prevents entering file names that already exist', function() {
  60. var inListStub = sinon.stub(fileList, 'inList').returns(true);
  61. $input.val('existing.txt');
  62. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  63. $input.closest('form').submit();
  64. expect(createDirectoryStub.notCalled).toEqual(true);
  65. inListStub.restore();
  66. });
  67. it('creates directory when clicking on create directory field', function() {
  68. $input = menu.$el.find('form.filenameform input');
  69. $input.val('some folder');
  70. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  71. $input.closest('form').submit();
  72. expect(createDirectoryStub.calledOnce).toEqual(true);
  73. expect(createDirectoryStub.getCall(0).args[0]).toEqual('some folder');
  74. });
  75. });
  76. describe('custom entries', function() {
  77. var oldPlugins;
  78. var plugin;
  79. var actionStub;
  80. beforeEach(function() {
  81. oldPlugins = _.extend({}, OC.Plugins._plugins);
  82. actionStub = sinon.stub();
  83. plugin = {
  84. attach: function(menu) {
  85. menu.addMenuEntry({
  86. id: 'file',
  87. displayName: t('files_texteditor', 'Text file'),
  88. templateName: t('files_texteditor', 'New text file.txt'),
  89. iconClass: 'icon-filetype-text',
  90. fileType: 'file',
  91. actionHandler: actionStub
  92. });
  93. }
  94. };
  95. OC.Plugins.register('OCA.Files.NewFileMenu', plugin);
  96. menu = new OCA.Files.NewFileMenu({
  97. fileList: fileList
  98. });
  99. menu.showAt($trigger);
  100. });
  101. afterEach(function() {
  102. OC.Plugins._plugins = oldPlugins;
  103. });
  104. it('renders custom menu items', function() {
  105. expect(menu.$el.find('.menuitem').length).toEqual(3);
  106. expect(menu.$el.find('.menuitem[data-action=file]').length).toEqual(1);
  107. });
  108. it('calls action handler when clicking on custom item', function() {
  109. menu.$el.find('.menuitem').eq(2).click();
  110. var $input = menu.$el.find('form.filenameform input');
  111. $input.val('some name');
  112. $input.trigger(new $.Event('keyup', {keyCode: 13}));
  113. $input.closest('form').submit();
  114. expect(actionStub.calledOnce).toEqual(true);
  115. expect(actionStub.getCall(0).args[0]).toEqual('some name');
  116. });
  117. it('switching fields removes the previous form', function() {
  118. menu.$el.find('.menuitem').eq(2).click();
  119. expect(menu.$el.find('form').length).toEqual(1);
  120. });
  121. });
  122. });