Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion __tests__/view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { describe, expect, test } from 'vitest'
import { describe, expect, test, vi } from 'vitest'

import { View } from '../lib/navigation/view.ts'
import { Folder } from '../lib/index.ts'
import { subscribe } from '@nextcloud/event-bus'

describe('Invalid View creation', () => {
test('Invalid id', () => {
Expand Down Expand Up @@ -201,3 +202,50 @@ describe('View creation', () => {
await expect(view.loadChildViews?.({} as unknown as View)).resolves.toBe(undefined)
})
})

describe('View update', () => {
test('Update a View', () => {
const view = new View({
id: 'test',
name: 'Test',
caption: 'Test caption',
emptyTitle: 'Test empty title',
emptyCaption: 'Test empty caption',
getContents: () => Promise.reject(new Error()),
hidden: true,
icon: '<svg></svg>',
order: 1,
params: {},
columns: [],
emptyView: () => {},
parent: 'parent',
sticky: false,
expanded: false,
defaultSortKey: 'key',
loadChildViews: async () => {},
})

const spy = vi.fn()
subscribe('files:view:updated', spy)

view.update({
name: 'Updated Test',
order: 2,
icon: '<svg>updated</svg>',
caption: 'Updated caption',
emptyTitle: 'Updated empty title',
emptyCaption: 'Updated empty caption',
})

expect(view.name).toBe('Updated Test')
expect(view.order).toBe(2)
expect(view.icon).toBe('<svg>updated</svg>')
expect(view.caption).toBe('Updated caption')
expect(view.emptyTitle).toBe('Updated empty title')
expect(view.emptyCaption).toBe('Updated empty caption')

expect(() => view.update({ id: 'new-id' })).toThrowError('The view ID is immutable and cannot be changed after creation')

expect(spy).toHaveBeenCalledOnce()
})
})
19 changes: 19 additions & 0 deletions lib/navigation/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import type { Folder } from '../node/folder.ts'
import type { Node } from '../node/node.ts'

import { emit } from '@nextcloud/event-bus'
import isSvg from 'is-svg'

import { Column } from './column.ts'

export type ContentsWithRoot = {
Expand Down Expand Up @@ -190,6 +192,23 @@ export class View implements ViewData {
return this._view.loadChildViews
}

/**
* Allows to update the view data.
* This will throw an error if the view is not valid.
* Warning: the view ID is immutable and cannot be changed after creation.
* @param {Partial<ViewData>} view the view data to update
*/
update(view: Partial<ViewData>) {
if (view.id && view.id !== this._view.id) {
throw new Error('The view ID is immutable and cannot be changed after creation')
}

isValidView({ ...this._view, ...view })
Object.assign(this._view, view)

emit('files:view:updated', this)
}

}

/**
Expand Down