Skip to content

Commit 1b247ae

Browse files
committed
chore: adjust vitest infrastructure to properly run both suits
Signed-off-by: Ferdinand Thiessen <[email protected]>
1 parent bc9cdc5 commit 1b247ae

File tree

21 files changed

+111
-74
lines changed

21 files changed

+111
-74
lines changed

__mocks__

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/frontend/__mocks__

__tests__

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/frontend/__tests__

apps/files_trashbin/src/files_actions/restoreAction.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ vi.mock('@nextcloud/axios', async (origial) => ({ ...(await origial()), default:
2222
vi.mock('@nextcloud/auth')
2323

2424
const errorSpy = vi.spyOn(window.console, 'error').mockImplementation(() => {})
25-
beforeEach(() => errorSpy.mockClear())
25+
beforeEach(() => {
26+
vi.resetAllMocks()
27+
})
2628

2729
describe('files_trashbin: file actions - restore action', () => {
2830
it('has id set', () => {

apps/files_trashbin/src/files_listActions/emptyTrashAction.spec.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,24 @@
66
import * as ncDialogs from '@nextcloud/dialogs'
77
import * as ncEventBus from '@nextcloud/event-bus'
88
import { Folder } from '@nextcloud/files'
9-
import * as ncInitialState from '@nextcloud/initial-state'
109
import { beforeEach, describe, expect, it, vi } from 'vitest'
1110
import { trashbinView } from '../files_views/trashbinView.ts'
1211
import * as api from '../services/api.ts'
1312
import { emptyTrashAction } from './emptyTrashAction.ts'
1413

14+
const loadState = vi.hoisted(() => vi.fn((app, key, fallback) => {
15+
if (fallback) {
16+
return fallback
17+
}
18+
throw new Error()
19+
}))
20+
21+
vi.mock('@nextcloud/initial-state', () => ({
22+
loadState,
23+
}))
24+
25+
beforeEach(() => vi.resetAllMocks())
26+
1527
describe('files_trashbin: file list actions - empty trashbin', () => {
1628
it('has id set', () => {
1729
expect(emptyTrashAction.id).toBe('empty-trash')
@@ -27,7 +39,7 @@ describe('files_trashbin: file list actions - empty trashbin', () => {
2739
})
2840

2941
it('is enabled on trashbin view', () => {
30-
const spy = vi.spyOn(ncInitialState, 'loadState').mockImplementationOnce(() => ({ allow_delete: true }))
42+
loadState.mockImplementation(() => ({ allow_delete: true }))
3143

3244
const root = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/', root: '/trashbin/test/' })
3345
const nodes = [
@@ -36,12 +48,12 @@ describe('files_trashbin: file list actions - empty trashbin', () => {
3648

3749
expect(emptyTrashAction.enabled).toBeTypeOf('function')
3850
expect(emptyTrashAction.enabled!(trashbinView, nodes, root)).toBe(true)
39-
expect(spy).toHaveBeenCalled()
40-
expect(spy).toHaveBeenCalledWith('files_trashbin', 'config')
51+
expect(loadState).toHaveBeenCalled()
52+
expect(loadState).toHaveBeenCalledWith('files_trashbin', 'config')
4153
})
4254

4355
it('is not enabled on another view enabled', () => {
44-
vi.spyOn(ncInitialState, 'loadState').mockImplementationOnce(() => ({ allow_delete: true }))
56+
loadState.mockImplementation(() => ({ allow_delete: true }))
4557

4658
const root = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/', root: '/trashbin/test/' })
4759
const nodes = [
@@ -62,7 +74,7 @@ describe('files_trashbin: file list actions - empty trashbin', () => {
6274
})
6375

6476
it('is not enabled when deletion is forbidden', () => {
65-
const spy = vi.spyOn(ncInitialState, 'loadState').mockImplementationOnce(() => ({ allow_delete: false }))
77+
loadState.mockImplementation(() => ({ allow_delete: false }))
6678

6779
const root = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/', root: '/trashbin/test/' })
6880
const nodes = [
@@ -71,13 +83,12 @@ describe('files_trashbin: file list actions - empty trashbin', () => {
7183

7284
expect(emptyTrashAction.enabled).toBeTypeOf('function')
7385
expect(emptyTrashAction.enabled!(trashbinView, nodes, root)).toBe(false)
74-
expect(spy).toHaveBeenCalled()
75-
expect(spy).toHaveBeenCalledWith('files_trashbin', 'config')
86+
expect(loadState).toHaveBeenCalled()
87+
expect(loadState).toHaveBeenCalledWith('files_trashbin', 'config')
7688
})
7789

7890
it('is not enabled when not in trashbin root', () => {
79-
vi.spyOn(ncInitialState, 'loadState').mockImplementationOnce(() => ({ allow_delete: true }))
80-
91+
loadState.mockImplementation(() => ({ allow_delete: true }))
8192
const root = new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/other-folder', root: '/trashbin/test/' })
8293
const nodes = [
8394
new Folder({ owner: 'test', source: 'https://example.com/remote.php/dav/trashbin/test/folder', root: '/trashbin/test/' }),
@@ -101,6 +112,7 @@ describe('files_trashbin: file list actions - empty trashbin', () => {
101112
}
102113

103114
beforeEach(() => {
115+
vi.resetAllMocks()
104116
dialogBuilder = {
105117
setSeverity: vi.fn(() => dialogBuilder),
106118
setText: vi.fn(() => dialogBuilder),

build/frontend-legacy/vitest.config.mts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import vue from '@vitejs/plugin-vue2'
77
import { exec } from 'node:child_process'
88
import { resolve } from 'node:path'
99
import { promisify } from 'node:util'
10+
import { nodePolyfills } from 'vite-plugin-node-polyfills'
1011
import { defaultExclude, defineConfig } from 'vitest/config'
1112

1213
const gitIgnore: string[] = []
@@ -26,7 +27,13 @@ try {
2627
}
2728

2829
export default defineConfig({
29-
plugins: [vue()],
30+
plugins: [
31+
nodePolyfills({
32+
include: ['fs', 'path'],
33+
protocolImports: false,
34+
}),
35+
vue(),
36+
],
3037
root: import.meta.dirname,
3138
resolve: {
3239
preserveSymlinks: true,
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)