Skip to content
Open
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
47 changes: 47 additions & 0 deletions cypress/component/store/tablesStore.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { createPinia, setActivePinia } from 'pinia'
import { useTablesStore } from '../../../src/store/store.js'

const hasStrayIndex = collection => Object.hasOwn(collection, '-1')

describe('tables store setters', () => {
beforeEach(() => {
setActivePinia(createPinia())
})

it('setTable replaces in place and leaves an unlisted table out', () => {
const store = useTablesStore()
store.setTables([{ id: 1, title: 'A' }, { id: 3, title: 'C' }])

store.setTable({ id: 3, title: 'C renamed' })
expect(store.tables.map(table => table.title)).to.deep.equal(['A', 'C renamed'])

store.setTable({ id: 2, title: 'removed in the meantime' })
expect(store.tables.map(table => table.id)).to.deep.equal([1, 3])
expect(hasStrayIndex(store.tables)).to.equal(false)
})

it('setView replaces in place and leaves an unlisted view out', () => {
const store = useTablesStore()
store.setViews([{ id: 1, title: 'A' }, { id: 3, title: 'C' }])

store.setView({ id: 3, title: 'C renamed' })
expect(store.views.map(view => view.title)).to.deep.equal(['A', 'C renamed'])

store.setView({ id: 2, title: 'removed in the meantime' })
expect(store.views.map(view => view.id)).to.deep.equal([1, 3])
expect(hasStrayIndex(store.views)).to.equal(false)
})

it('setContext appends an unlisted application so a load by URL stays visible', () => {
const store = useTablesStore()
store.setContexts([{ id: 1, name: 'A' }])

store.setContext({ id: 2, name: 'B' })
expect(store.contexts.map(context => context.id)).to.deep.equal([1, 2])
expect(hasStrayIndex(store.contexts)).to.equal(false)
})
})
65 changes: 41 additions & 24 deletions src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@
},
setTable(table) {
const index = this.tables.findIndex(t => t.id === table.id)
this.tables.splice(index, 1, table)
if (index !== -1) {
this.tables[index] = table
}
},
setView(view) {
const index = this.views.findIndex(v => v.id === view.id)
this.views[index] = view
if (index !== -1) {
this.views[index] = view
}
},
setContext(context) {
const index = this.contexts.findIndex(c => c.id === context.id)
Expand Down Expand Up @@ -169,7 +173,7 @@
return true
},

/**

Check warning on line 176 in src/store/store.js

View workflow job for this annotation

GitHub Actions / NPM lint

Missing JSDoc @PARAM "tables" declaration
* We have two sources of views, this coming from tables endpoint, and those
* that were shared directly or through contexts.
*/
Expand Down Expand Up @@ -227,7 +231,7 @@
views.push(res.data)
this.setViews(views)
const table = this.tables.find(t => t.id === res.data.tableId)
table.views.push(res.data)
table?.views.push(res.data)

return res.data.id
},
Expand All @@ -242,11 +246,8 @@
return false
}

const view = res.data
const views = this.views
const index = views.findIndex(v => v.id === view.id)
views[index] = view
this.setViews([...views])
this.setView(res.data)
this.setViews([...this.views])
return true
},

Expand All @@ -260,7 +261,9 @@

const views = this.views
const index = views.findIndex(v => v.id === viewId)
views.splice(index, 1)
if (index !== -1) {
views.splice(index, 1)
}
this.setViews([...views])
return true
},
Expand All @@ -269,12 +272,7 @@
let res = null
try {
res = await axios.get(generateUrl('/apps/tables/view/table/' + tableId))
// Set Views
const views = this.views
res.data.forEach(view => {
const index = views.findIndex(v => v.id === view.id)
this.views[index] = view
})
res.data.forEach(view => this.setView(view))
} catch (e) {
displayError(e, t('tables', 'Could not reload view.'))
return false
Expand Down Expand Up @@ -309,10 +307,7 @@
return false
}

const table = res.data
const tables = this.tables
const index = tables.findIndex(t => t.id === table.id)
this.tables[index] = table
this.setTable(res.data)
return true
},

Expand All @@ -325,6 +320,9 @@
}

const index = this.views.findIndex(v => v.id === id)
if (index === -1) {
return true
}
const view = this.views[index]
view.favorite = true
this.setView(view)
Expand All @@ -341,6 +339,9 @@
}

const index = this.views.findIndex(v => v.id === id)
if (index === -1) {
return true
}
const view = this.views[index]
view.favorite = false
this.setView(view)
Expand All @@ -357,6 +358,9 @@
}

const index = this.tables.findIndex(t => t.id === id)
if (index === -1) {
return true
}
const table = this.tables[index]
table.favorite = true
this.setTable(table)
Expand All @@ -373,6 +377,9 @@
}

const index = this.tables.findIndex(t => t.id === id)
if (index === -1) {
return true
}
const table = this.tables[index]
table.favorite = false
this.setTable(table)
Expand Down Expand Up @@ -467,7 +474,9 @@

const context = res.data.ocs.data
const index = this.contexts.findIndex(c => c.id === context.id)
this.contexts[index] = context
if (index !== -1) {
this.contexts[index] = context
}

return true
},
Expand All @@ -482,7 +491,9 @@

const tables = this.tables
const index = tables.findIndex(t => t.id === id)
tables.splice(index, 1)
if (index !== -1) {
tables.splice(index, 1)
}
this.setTables([...tables])
return true
},
Expand Down Expand Up @@ -631,7 +642,9 @@

const contexts = this.contexts
const index = contexts.findIndex(c => c.id === id)
contexts.splice(index, 1)
if (index !== -1) {
contexts.splice(index, 1)
}
this.setContexts([...contexts])
return true
},
Expand All @@ -645,7 +658,9 @@
}
const contexts = this.contexts
const index = contexts.findIndex(c => c.id === context.id)
contexts.splice(index, 1)
if (index !== -1) {
contexts.splice(index, 1)
}
this.setContexts([...contexts])
return true
},
Expand All @@ -660,7 +675,9 @@

const tables = this.tables
const index = tables.findIndex(t => t.id === tableId)
tables.splice(index, 1)
if (index !== -1) {
tables.splice(index, 1)
}
this.setTables([...tables])
return true
},
Expand Down
Loading