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.

viewInFolderAction.spec.ts 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 './viewInFolderAction'
  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('View in folder action conditions tests', () => {
  32. test('Default values', () => {
  33. expect(action).toBeInstanceOf(FileAction)
  34. expect(action.id).toBe('view-in-folder')
  35. expect(action.displayName([], view)).toBe('View in folder')
  36. expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>')
  37. expect(action.default).toBeUndefined()
  38. expect(action.order).toBe(80)
  39. })
  40. })
  41. describe('View in folder action enabled tests', () => {
  42. test('Enabled for files', () => {
  43. const file = new File({
  44. id: 1,
  45. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  46. owner: 'admin',
  47. mime: 'text/plain',
  48. permissions: Permission.ALL,
  49. })
  50. expect(action.enabled).toBeDefined()
  51. expect(action.enabled!([file], view)).toBe(true)
  52. })
  53. test('Disabled without permissions', () => {
  54. const file = new File({
  55. id: 1,
  56. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  57. owner: 'admin',
  58. mime: 'text/plain',
  59. permissions: Permission.NONE,
  60. })
  61. expect(action.enabled).toBeDefined()
  62. expect(action.enabled!([file], view)).toBe(false)
  63. })
  64. test('Disabled for non-dav ressources', () => {
  65. const file = new File({
  66. id: 1,
  67. source: 'https://domain.com/foobar.txt',
  68. owner: 'admin',
  69. mime: 'text/plain',
  70. })
  71. expect(action.enabled).toBeDefined()
  72. expect(action.enabled!([file], view)).toBe(false)
  73. })
  74. test('Disabled if more than one node', () => {
  75. const file1 = new File({
  76. id: 1,
  77. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foo.txt',
  78. owner: 'admin',
  79. mime: 'text/plain',
  80. })
  81. const file2 = new File({
  82. id: 1,
  83. source: 'https://cloud.domain.com/remote.php/dav/files/admin/bar.txt',
  84. owner: 'admin',
  85. mime: 'text/plain',
  86. })
  87. expect(action.enabled).toBeDefined()
  88. expect(action.enabled!([file1, file2], view)).toBe(false)
  89. })
  90. test('Disabled for folders', () => {
  91. const folder = new Folder({
  92. id: 1,
  93. source: 'https://cloud.domain.com/remote.php/dav/files/admin/FooBar/',
  94. owner: 'admin',
  95. permissions: Permission.READ,
  96. })
  97. expect(action.enabled).toBeDefined()
  98. expect(action.enabled!([folder], view)).toBe(false)
  99. })
  100. })
  101. describe('View in folder action execute tests', () => {
  102. test('View in folder', async () => {
  103. const goToRouteMock = jest.fn()
  104. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  105. const file = new File({
  106. id: 1,
  107. source: 'https://cloud.domain.com/remote.php/dav/files/admin/foobar.txt',
  108. owner: 'admin',
  109. mime: 'text/plain',
  110. permissions: Permission.READ,
  111. })
  112. const exec = await action.exec(file, view, '/')
  113. // Silent action
  114. expect(exec).toBe(null)
  115. expect(goToRouteMock).toBeCalledTimes(1)
  116. expect(goToRouteMock).toBeCalledWith(null, { fileid: 1, view: 'files' }, { dir: '/' })
  117. })
  118. test('View in (sub) folder', async () => {
  119. const goToRouteMock = jest.fn()
  120. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  121. const file = new File({
  122. id: 1,
  123. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/Bar/foobar.txt',
  124. root: '/files/admin',
  125. owner: 'admin',
  126. mime: 'text/plain',
  127. permissions: Permission.READ,
  128. })
  129. const exec = await action.exec(file, view, '/')
  130. // Silent action
  131. expect(exec).toBe(null)
  132. expect(goToRouteMock).toBeCalledTimes(1)
  133. expect(goToRouteMock).toBeCalledWith(null, { fileid: 1, view: 'files' }, { dir: '/Foo/Bar' })
  134. })
  135. test('View in folder fails without node', async () => {
  136. const goToRouteMock = jest.fn()
  137. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  138. const exec = await action.exec(null as unknown as Node, view, '/')
  139. expect(exec).toBe(false)
  140. expect(goToRouteMock).toBeCalledTimes(0)
  141. })
  142. test('View in folder fails without File', async () => {
  143. const goToRouteMock = jest.fn()
  144. window.OCP = { Files: { Router: { goToRoute: goToRouteMock } } }
  145. const folder = new Folder({
  146. id: 1,
  147. source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
  148. owner: 'admin',
  149. })
  150. const exec = await action.exec(folder, view, '/')
  151. expect(exec).toBe(false)
  152. expect(goToRouteMock).toBeCalledTimes(0)
  153. })
  154. })