Skip to content

Commit

Permalink
fix: Adjust tests for editLocallyAction
Browse files Browse the repository at this point in the history
Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
  • Loading branch information
nfebe committed Jul 31, 2024
1 parent 3d2592a commit d22fe6e
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions apps/files/src/actions/editLocallyAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@
import { action } from './editLocallyAction'
import { expect } from '@jest/globals'
import { File, Permission, View, FileAction } from '@nextcloud/files'
import * as ncDialogs from '@nextcloud/dialogs'
import { DialogBuilder, showError } from '@nextcloud/dialogs'
import axios from '@nextcloud/axios'

const dialogBuilder = {
setName: jest.fn().mockReturnThis(),
setText: jest.fn().mockReturnThis(),
setButtons: jest.fn().mockReturnThis(),
build: jest.fn().mockReturnValue({
show: jest.fn().mockResolvedValue(true),
}),
} as unknown as DialogBuilder

jest.mock('@nextcloud/dialogs', () => ({
DialogBuilder: jest.fn(() => dialogBuilder),
showError: jest.fn(),
}))

const view = {
id: 'files',
name: 'Files',
Expand All @@ -16,7 +30,8 @@ const view = {
// Mock webroot variable
beforeAll(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any)._oc_webroot = ''
(window as any)._oc_webroot = '';
(window as any).OCA = { Viewer: { open: jest.fn() } }
})

describe('Edit locally action conditions tests', () => {
Expand Down Expand Up @@ -44,7 +59,7 @@ describe('Edit locally action enabled tests', () => {
expect(action.enabled!([file], view)).toBe(true)
})

test('Disabled for non-dav ressources', () => {
test('Disabled for non-dav resources', () => {
const file = new File({
id: 1,
source: 'https://domain.com/data/foobar.txt',
Expand Down Expand Up @@ -76,7 +91,7 @@ describe('Edit locally action enabled tests', () => {
expect(action.enabled!([file1, file2], view)).toBe(false)
})

test('Disabled for files', () => {
test('Disabled for directories', () => {
const file = new File({
id: 1,
source: 'https://cloud.domain.com/remote.php/dav/files/admin/Foo/',
Expand Down Expand Up @@ -104,8 +119,11 @@ describe('Edit locally action enabled tests', () => {

describe('Edit locally action execute tests', () => {
test('Edit locally opens proper URL', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({ data: { ocs: { data: { token: 'foobar' } } } }))
jest.spyOn(ncDialogs, 'showError')
jest.spyOn(axios, 'post').mockImplementation(async () => ({
data: { ocs: { data: { token: 'foobar' } } }
}))
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -117,17 +135,20 @@ describe('Edit locally action execute tests', () => {

const exec = await action.exec(file, view, '/')

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(0)
expect(mockedShowError).toBeCalledTimes(0)
expect(window.location.href).toBe('nc://open/test@localhost/foobar.txt?token=foobar')
})

test('Edit locally fails and show error', async () => {
test('Edit locally fails and shows error', async () => {
jest.spyOn(axios, 'post').mockImplementation(async () => ({}))
jest.spyOn(ncDialogs, 'showError')
const mockedShowError = jest.mocked(showError)
const spyDialogBuilder = jest.spyOn(dialogBuilder, 'build')

const file = new File({
id: 1,
Expand All @@ -139,12 +160,14 @@ describe('Edit locally action execute tests', () => {

const exec = await action.exec(file, view, '/')

expect(spyDialogBuilder).toBeCalled()

// Silent action
expect(exec).toBe(null)
expect(axios.post).toBeCalledTimes(1)
expect(axios.post).toBeCalledWith('http://localhost/ocs/v2.php/apps/files/api/v1/openlocaleditor?format=json', { path: '/foobar.txt' })
expect(ncDialogs.showError).toBeCalledTimes(1)
expect(ncDialogs.showError).toBeCalledWith('Failed to redirect to client')
expect(mockedShowError).toBeCalledTimes(1) // Use showError directly
expect(mockedShowError).toBeCalledWith('Failed to redirect to client') // Use showError directly
expect(window.location.href).toBe('http://localhost/')
})
})

0 comments on commit d22fe6e

Please sign in to comment.