openFolderAction.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 { action } from './openFolderAction'
  23. import { expect } from '@jest/globals'
  24. import { File, Folder, Node, Permission } from '@nextcloud/files'
  25. import { FileAction } from '../services/FileAction'
  26. import type { Navigation } from '../services/Navigation'
  27. const view = {
  28. id: 'files',
  29. name: 'Files',
  30. } as Navigation
  31. describe('Open folder action conditions tests', () => {
  32. test('Default values', () => {
  33. const folder = new Folder({
  34. id: 1,
  35. source: 'https://cloud.domain.com/remote.php/dav/files/admin/FooBar/',
  36. owner: 'admin',
  37. permissions: Permission.READ,
  38. })
  39. expect(action).toBeInstanceOf(FileAction)
  40. expect(action.id).toBe('open-folder')
  41. expect(action.displayName([folder], view)).toBe('Open folder FooBar')
  42. expect(action.iconSvgInline([], view)).toBe('SvgMock')
  43. expect(action.default).toBe(true)
  44. expect(action.order).toBe(-100)
  45. })
  46. })
  47. describe('Open folder action enabled tests', () => {
  48. test('Enabled for folders', () => {
  49. const folder = new Folder({
  50. id: 1,
  51. source: 'https://cloud.domain.com/remote.php/dav/files/admin/FooBar/',
  52. owner: 'admin',
  53. permissions: Permission.READ,
  54. })
  55. expect(action.enabled).toBeDefined()
  56. expect(action.enabled!([folder], view)).toBe(true)
  57. })
  58. test('Disabled for non-dav ressources', () => {
  59. const folder = new Folder({
  60. id: 1,
  61. source: 'https://domain.com/data/FooBar/',
  62. owner: 'admin',
  63. permissions: Permission.NONE,
  64. })
  65. expect(action.enabled).toBeDefined()
  66. expect(action.enabled!([folder], view)).toBe(false)
  67. })
  68. test('Disabled if more than one node', () => {
  69. const folder1 = new Folder({
  70. id: 1,
  71. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  72. owner: 'admin',
  73. permissions: Permission.READ,
  74. })
  75. const folder2 = new Folder({
  76. id: 2,
  77. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Bar/',
  78. owner: 'admin',
  79. permissions: Permission.READ,
  80. })
  81. expect(action.enabled).toBeDefined()
  82. expect(action.enabled!([folder1, folder2], view)).toBe(false)
  83. })
  84. test('Disabled for files', () => {
  85. const file = new File({
  86. id: 1,
  87. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  88. owner: 'admin',
  89. mime: 'text/plain',
  90. })
  91. expect(action.enabled).toBeDefined()
  92. expect(action.enabled!([file], view)).toBe(false)
  93. })
  94. test('Disabled without READ permissions', () => {
  95. const folder = new Folder({
  96. id: 1,
  97. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  98. owner: 'admin',
  99. permissions: Permission.NONE,
  100. })
  101. expect(action.enabled).toBeDefined()
  102. expect(action.enabled!([folder], view)).toBe(false)
  103. })
  104. })
  105. describe('Open folder action execute tests', () => {
  106. test('Open folder', async () => {
  107. const goToRouteMock = jest.fn()
  108. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  109. const folder = new Folder({
  110. id: 1,
  111. source: 'https://cloud.domain.com/remote.php/dav/files/admin/FooBar/',
  112. owner: 'admin',
  113. permissions: Permission.READ,
  114. })
  115. const exec = await action.exec(folder, view, '/')
  116. // Silent action
  117. expect(exec).toBe(null)
  118. expect(goToRouteMock).toBeCalledTimes(1)
  119. expect(goToRouteMock).toBeCalledWith(null, null, { dir: '/FooBar' })
  120. })
  121. test('Open folder fails without node', async () => {
  122. const goToRouteMock = jest.fn()
  123. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  124. const exec = await action.exec(null as unknown as Node, view, '/')
  125. expect(exec).toBe(false)
  126. expect(goToRouteMock).toBeCalledTimes(0)
  127. })
  128. test('Open folder fails without Folder', async () => {
  129. const goToRouteMock = jest.fn()
  130. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  131. const file = new File({
  132. id: 1,
  133. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  134. owner: 'admin',
  135. mime: 'text/plain',
  136. })
  137. const exec = await action.exec(file, view, '/')
  138. expect(exec).toBe(false)
  139. expect(goToRouteMock).toBeCalledTimes(0)
  140. })
  141. })