Skip to content

Commit 8611545

Browse files
Pytalsusnux
authored andcommitted
test(files): Adapt favorite views spec
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 02e8165 commit 8611545

1 file changed

Lines changed: 68 additions & 26 deletions

File tree

apps/files/src/views/favorites.spec.ts

Lines changed: 68 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,42 @@
2020
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
*
2222
*/
23-
import { basename } from 'path'
23+
24+
import type { Folder as CFolder, Navigation } from '@nextcloud/files'
25+
2426
import { expect } from '@jest/globals'
25-
import { Folder, Navigation, getNavigation } from '@nextcloud/files'
27+
import * as filesUtils from '@nextcloud/files'
2628
import { CancelablePromise } from 'cancelable-promise'
27-
import eventBus from '@nextcloud/event-bus'
29+
import * as eventBus from '@nextcloud/event-bus'
2830
import * as initialState from '@nextcloud/initial-state'
31+
import { basename } from 'path'
2932

3033
import { action } from '../actions/favoriteAction'
3134
import * as favoritesService from '../services/Favorites'
3235
import { registerFavoritesView } from './favorites'
3336

37+
const { Folder, getNavigation } = filesUtils
38+
39+
jest.mock('@nextcloud/axios', () => ({
40+
post: jest.fn(),
41+
}))
42+
3443
jest.mock('webdav/dist/node/request.js', () => ({
3544
request: jest.fn(),
3645
}))
3746

38-
global.window.OC = {
47+
jest.mock('@nextcloud/files', () => ({
48+
__esModule: true,
49+
...jest.requireActual('@nextcloud/files'),
50+
}))
51+
52+
jest.mock('@nextcloud/event-bus', () => ({
53+
__esModule: true,
54+
...jest.requireActual('@nextcloud/event-bus'),
55+
}))
56+
57+
window.OC = {
58+
...window.OC,
3959
TAG_FAVORITE: '_$!<Favorite>!$_',
4060
}
4161

@@ -56,11 +76,12 @@ describe('Favorites view definition', () => {
5676
delete window._nc_navigation
5777
})
5878

59-
test('Default empty favorite view', () => {
79+
test('Default empty favorite view', async () => {
6080
jest.spyOn(eventBus, 'subscribe')
61-
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
81+
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
82+
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
6283

63-
registerFavoritesView()
84+
await registerFavoritesView()
6485
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
6586
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
6687

@@ -83,16 +104,31 @@ describe('Favorites view definition', () => {
83104
expect(favoritesView?.getContents).toBeDefined()
84105
})
85106

86-
test('Default with favorites', () => {
107+
test('Default with favorites', async () => {
87108
const favoriteFolders = [
88-
{ fileid: 1, path: '/foo' },
89-
{ fileid: 2, path: '/bar' },
90-
{ fileid: 3, path: '/foo/bar' },
109+
new Folder({
110+
id: 1,
111+
root: '/files/admin',
112+
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo',
113+
owner: 'admin',
114+
}),
115+
new Folder({
116+
id: 2,
117+
root: '/files/admin',
118+
source: 'http://nextcloud.local/remote.php/dav/files/admin/bar',
119+
owner: 'admin',
120+
}),
121+
new Folder({
122+
id: 3,
123+
root: '/files/admin',
124+
source: 'http://nextcloud.local/remote.php/dav/files/admin/foo/bar',
125+
owner: 'admin',
126+
}),
91127
]
92-
jest.spyOn(initialState, 'loadState').mockReturnValue(favoriteFolders)
93-
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
128+
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve(favoriteFolders))
129+
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
94130

95-
registerFavoritesView()
131+
await registerFavoritesView()
96132
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
97133
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
98134

@@ -110,7 +146,7 @@ describe('Favorites view definition', () => {
110146
expect(favoriteView?.order).toBe(index)
111147
expect(favoriteView?.params).toStrictEqual({
112148
dir: folder.path,
113-
fileid: folder.fileid.toString(),
149+
fileid: String(folder.fileid),
114150
view: 'favorites',
115151
})
116152
expect(favoriteView?.parent).toBe('favorites')
@@ -132,10 +168,10 @@ describe('Dynamic update of favourite folders', () => {
132168

133169
test('Add a favorite folder creates a new entry in the navigation', async () => {
134170
jest.spyOn(eventBus, 'emit')
135-
jest.spyOn(initialState, 'loadState').mockReturnValue([])
136-
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
171+
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
172+
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
137173

138-
registerFavoritesView()
174+
await registerFavoritesView()
139175
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
140176
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
141177

@@ -161,10 +197,17 @@ describe('Dynamic update of favourite folders', () => {
161197
test('Remove a favorite folder remove the entry from the navigation column', async () => {
162198
jest.spyOn(eventBus, 'emit')
163199
jest.spyOn(eventBus, 'subscribe')
164-
jest.spyOn(initialState, 'loadState').mockReturnValue([{ fileid: 42, path: '/Foo/Bar' }])
165-
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
166-
167-
registerFavoritesView()
200+
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([
201+
new Folder({
202+
id: 42,
203+
root: '/files/admin',
204+
source: 'http://nextcloud.local/remote.php/dav/files/admin/Foo/Bar',
205+
owner: 'admin',
206+
}),
207+
]))
208+
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
209+
210+
await registerFavoritesView()
168211
let favoritesView = Navigation.views.find(view => view.id === 'favorites')
169212
let favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
170213

@@ -201,11 +244,10 @@ describe('Dynamic update of favourite folders', () => {
201244

202245
test('Renaming a favorite folder updates the navigation', async () => {
203246
jest.spyOn(eventBus, 'emit')
204-
jest.spyOn(initialState, 'loadState').mockReturnValue([])
205-
jest.spyOn(favoritesService, 'getContents')
206-
.mockReturnValue(CancelablePromise.resolve({ folder: {} as Folder, contents: [] }))
247+
jest.spyOn(filesUtils, 'getFavoriteNodes').mockReturnValue(CancelablePromise.resolve([]))
248+
jest.spyOn(favoritesService, 'getContents').mockReturnValue(CancelablePromise.resolve({ folder: {} as CFolder, contents: [] }))
207249

208-
registerFavoritesView()
250+
await registerFavoritesView()
209251
const favoritesView = Navigation.views.find(view => view.id === 'favorites')
210252
const favoriteFoldersViews = Navigation.views.filter(view => view.parent === 'favorites')
211253

0 commit comments

Comments
 (0)