You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sidebarAction.spec.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import type { Navigation } from '../services/Navigation'
  23. import { expect } from '@jest/globals'
  24. import { File, Permission } from '@nextcloud/files'
  25. import { action } from './sidebarAction'
  26. import { FileAction } from '../services/FileAction'
  27. import logger from '../logger'
  28. const view = {
  29. id: 'files',
  30. name: 'Files',
  31. } as Navigation
  32. describe('Open sidebar action conditions tests', () => {
  33. test('Default values', () => {
  34. expect(action).toBeInstanceOf(FileAction)
  35. expect(action.id).toBe('details')
  36. expect(action.displayName([], view)).toBe('Open details')
  37. expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
  38. expect(action.default).toBeUndefined()
  39. expect(action.order).toBe(-50)
  40. })
  41. })
  42. describe('Open sidebar action enabled tests', () => {
  43. test('Enabled for ressources within user root folder', () => {
  44. window.OCA = { Files: { Sidebar: {} } }
  45. const file = new File({
  46. id: 1,
  47. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  48. owner: 'admin',
  49. mime: 'text/plain',
  50. permissions: Permission.ALL,
  51. })
  52. expect(action.enabled).toBeDefined()
  53. expect(action.enabled!([file], view)).toBe(true)
  54. })
  55. test('Disabled without permissions', () => {
  56. window.OCA = { Files: { Sidebar: {} } }
  57. const file = new File({
  58. id: 1,
  59. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  60. owner: 'admin',
  61. mime: 'text/plain',
  62. permissions: Permission.NONE,
  63. })
  64. expect(action.enabled).toBeDefined()
  65. expect(action.enabled!([file], view)).toBe(false)
  66. })
  67. test('Disabled if more than one node', () => {
  68. window.OCA = { Files: { Sidebar: {} } }
  69. const file1 = new File({
  70. id: 1,
  71. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
  72. owner: 'admin',
  73. mime: 'text/plain',
  74. })
  75. const file2 = new File({
  76. id: 1,
  77. source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
  78. owner: 'admin',
  79. mime: 'text/plain',
  80. })
  81. expect(action.enabled).toBeDefined()
  82. expect(action.enabled!([file1, file2], view)).toBe(false)
  83. })
  84. test('Disabled if no Sidebar', () => {
  85. window.OCA = {}
  86. const file = new File({
  87. id: 1,
  88. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  89. owner: 'admin',
  90. mime: 'text/plain',
  91. })
  92. expect(action.enabled).toBeDefined()
  93. expect(action.enabled!([file], view)).toBe(false)
  94. })
  95. test('Disabled for non-dav ressources', () => {
  96. window.OCA = { Files: { Sidebar: {} } }
  97. const file = new File({
  98. id: 1,
  99. source: 'https://domain.com/documents/admin/foobar.txt',
  100. owner: 'admin',
  101. mime: 'text/plain',
  102. })
  103. expect(action.enabled).toBeDefined()
  104. expect(action.enabled!([file], view)).toBe(false)
  105. })
  106. })
  107. describe('Open sidebar action exec tests', () => {
  108. test('Open sidebar', async () => {
  109. const openMock = jest.fn()
  110. window.OCA = { Files: { Sidebar: { open: openMock } } }
  111. const goToRouteMock = jest.fn()
  112. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  113. const file = new File({
  114. id: 1,
  115. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  116. owner: 'admin',
  117. mime: 'text/plain',
  118. })
  119. const exec = await action.exec(file, view, '/')
  120. // Silent action
  121. expect(exec).toBe(null)
  122. expect(openMock).toBeCalledWith('/foobar.txt')
  123. expect(goToRouteMock).toBeCalledWith(
  124. null,
  125. { view: view.id, fileid: 1 },
  126. { dir: '/' },
  127. true,
  128. )
  129. })
  130. test('Open sidebar fails', async () => {
  131. const openMock = jest.fn(() => { throw new Error('Mock error') })
  132. window.OCA = { Files: { Sidebar: { open: openMock } } }
  133. jest.spyOn(logger, 'error').mockImplementation(() => jest.fn())
  134. const file = new File({
  135. id: 1,
  136. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  137. owner: 'admin',
  138. mime: 'text/plain',
  139. })
  140. const exec = await action.exec(file, view, '/')
  141. expect(exec).toBe(false)
  142. expect(openMock).toBeCalledTimes(1)
  143. expect(logger.error).toBeCalledTimes(1)
  144. })
  145. })