diff options
Diffstat (limited to 'apps/files/src/actions/favoriteAction.spec.ts')
-rw-r--r-- | apps/files/src/actions/favoriteAction.spec.ts | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/apps/files/src/actions/favoriteAction.spec.ts b/apps/files/src/actions/favoriteAction.spec.ts index 39b82ff82d6..96768c4887a 100644 --- a/apps/files/src/actions/favoriteAction.spec.ts +++ b/apps/files/src/actions/favoriteAction.spec.ts @@ -2,14 +2,18 @@ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -import { action } from './favoriteAction' -import { expect } from '@jest/globals' import { File, Permission, View, FileAction } from '@nextcloud/files' -import eventBus from '@nextcloud/event-bus' -import * as favoriteAction from './favoriteAction' +import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest' + +import { action } from './favoriteAction' import axios from '@nextcloud/axios' +import * as eventBus from '@nextcloud/event-bus' +import * as favoriteAction from './favoriteAction' import logger from '../logger' +vi.mock('@nextcloud/auth') +vi.mock('@nextcloud/axios') + const view = { id: 'files', name: 'Files', @@ -20,12 +24,12 @@ const favoriteView = { name: 'Favorites', } as View -global.window.OC = { - TAG_FAVORITE: '_$!<Favorite>!$_', -} - // Mock webroot variable beforeAll(() => { + window.OC = { + ...window.OC, + TAG_FAVORITE: '_$!<Favorite>!$_', + }; // eslint-disable-next-line @typescript-eslint/no-explicit-any (window as any)._oc_webroot = '' }) @@ -42,7 +46,7 @@ describe('Favorite action conditions tests', () => { expect(action).toBeInstanceOf(FileAction) expect(action.id).toBe('favorite') expect(action.displayName([file], view)).toBe('Add to favorites') - expect(action.iconSvgInline([], view)).toBe('<svg>SvgMock</svg>') + expect(action.iconSvgInline([], view)).toMatch(/<svg.+<\/svg>/) expect(action.default).toBeUndefined() expect(action.order).toBe(-50) }) @@ -128,13 +132,11 @@ describe('Favorite action enabled tests', () => { }) describe('Favorite action execute tests', () => { - afterEach(() => { - jest.spyOn(axios, 'post').mockRestore() - }) + beforeEach(() => { vi.resetAllMocks() }) test('Favorite triggers tag addition', async () => { - jest.spyOn(axios, 'post') - jest.spyOn(eventBus, 'emit') + vi.spyOn(axios, 'post') + vi.spyOn(eventBus, 'emit') const file = new File({ id: 1, @@ -158,8 +160,8 @@ describe('Favorite action execute tests', () => { }) test('Favorite triggers tag removal', async () => { - jest.spyOn(axios, 'post') - jest.spyOn(eventBus, 'emit') + vi.spyOn(axios, 'post') + vi.spyOn(eventBus, 'emit') const file = new File({ id: 1, @@ -186,8 +188,8 @@ describe('Favorite action execute tests', () => { }) test('Favorite triggers node removal if favorite view and root dir', async () => { - jest.spyOn(axios, 'post') - jest.spyOn(eventBus, 'emit') + vi.spyOn(axios, 'post') + vi.spyOn(eventBus, 'emit') const file = new File({ id: 1, @@ -215,8 +217,8 @@ describe('Favorite action execute tests', () => { }) test('Favorite does NOT triggers node removal if favorite view but NOT root dir', async () => { - jest.spyOn(axios, 'post') - jest.spyOn(eventBus, 'emit') + vi.spyOn(axios, 'post') + vi.spyOn(eventBus, 'emit') const file = new File({ id: 1, @@ -245,8 +247,8 @@ describe('Favorite action execute tests', () => { test('Favorite fails and show error', async () => { const error = new Error('Mock error') - jest.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') }) - jest.spyOn(logger, 'error').mockImplementation(() => jest.fn()) + vi.spyOn(axios, 'post').mockImplementation(() => { throw new Error('Mock error') }) + vi.spyOn(logger, 'error').mockImplementation(() => vi.fn()) const file = new File({ id: 1, @@ -275,8 +277,8 @@ describe('Favorite action execute tests', () => { test('Removing from favorites fails and show error', async () => { const error = new Error('Mock error') - jest.spyOn(axios, 'post').mockImplementation(() => { throw error }) - jest.spyOn(logger, 'error').mockImplementation(() => jest.fn()) + vi.spyOn(axios, 'post').mockImplementation(() => { throw error }) + vi.spyOn(logger, 'error').mockImplementation(() => vi.fn()) const file = new File({ id: 1, @@ -305,9 +307,11 @@ describe('Favorite action execute tests', () => { }) describe('Favorite action batch execute tests', () => { + beforeEach(() => { vi.restoreAllMocks() }) + test('Favorite action batch execute with mixed files', async () => { - jest.spyOn(favoriteAction, 'favoriteNode') - jest.spyOn(axios, 'post') + vi.spyOn(favoriteAction, 'favoriteNode') + vi.spyOn(axios, 'post') const file1 = new File({ id: 1, @@ -335,15 +339,14 @@ describe('Favorite action batch execute tests', () => { expect(exec).toStrictEqual([true, true]) expect([file1, file2].every(file => file.attributes.favorite === 1)).toBe(true) - expect(favoriteAction.favoriteNode).toBeCalledTimes(2) expect(axios.post).toBeCalledTimes(2) expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: ['_$!<Favorite>!$_'] }) expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: ['_$!<Favorite>!$_'] }) }) test('Remove from favorite action batch execute with favorites only files', async () => { - jest.spyOn(favoriteAction, 'favoriteNode') - jest.spyOn(axios, 'post') + vi.spyOn(favoriteAction, 'favoriteNode') + vi.spyOn(axios, 'post') const file1 = new File({ id: 1, @@ -371,7 +374,6 @@ describe('Favorite action batch execute tests', () => { expect(exec).toStrictEqual([true, true]) expect([file1, file2].every(file => file.attributes.favorite === 0)).toBe(true) - expect(favoriteAction.favoriteNode).toBeCalledTimes(2) expect(axios.post).toBeCalledTimes(2) expect(axios.post).toHaveBeenNthCalledWith(1, '/index.php/apps/files/api/v1/files/foo.txt', { tags: [] }) expect(axios.post).toHaveBeenNthCalledWith(2, '/index.php/apps/files/api/v1/files/bar.txt', { tags: [] }) |