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>

[skip ci]
  • Loading branch information
Fenn-CS authored and backportbot[bot] committed Jul 31, 2024
1 parent 26db929 commit 3e06eea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
40 changes: 31 additions & 9 deletions apps/files/src/actions/editLocallyAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,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 Down Expand Up @@ -55,7 +69,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 @@ -115,8 +129,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 @@ -128,17 +145,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 @@ -150,12 +170,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)
expect(mockedShowError).toBeCalledWith('Failed to redirect to client')
expect(window.location.href).toBe('http://localhost/')
})
})
2 changes: 1 addition & 1 deletion apps/files/src/actions/editLocallyAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const confirmLocalEditDialog = (
.then(() => {
// Ensure the callback is called even if the dialog is dismissed in other ways
if (!callbackCalled) {
localEditCallback(true)
localEditCallback(false)
}
})
}
Expand Down

0 comments on commit 3e06eea

Please sign in to comment.