Skip to content

Commit

Permalink
feat(#684): add druxtMenu/flushEntities mutation (#689)
Browse files Browse the repository at this point in the history
* feat(#684): add druxtMenu/flushEntities mutation

* chore(#684): ensure state.entities is object

* chore(#684): add tests

* chore(#684): update tests
  • Loading branch information
Decipher authored Jan 8, 2024
1 parent 96d693d commit 26b1bc6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/happy-ties-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"druxt-menu": minor
---

feat(#684): added druxtMenu/flushEntities Vuex mutation.
1 change: 1 addition & 0 deletions packages/menu/src/components/DruxtMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export default {
mounted() {
// If logged in and statically generated, re-fetch the menu.
if (this?.$auth?.loggedIn && this?.$store?.app?.context?.isStatic) {
this.$store.commit('druxtMenu/flushEntities', { prefix: this.lang })
const settings = this.$options.druxt.settings(this, this.component.settings)
this.$options.druxt.fetchData.call(this, settings)
}
Expand Down
29 changes: 28 additions & 1 deletion packages/menu/src/stores/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,21 @@ const DruxtMenuStore = ({ store }) => {
const entity = entities[index]
Vue.set(state.entities[prefix], entity.id, entity)
}
}
},

/**
* @name flushEntities
* @mutator {object} flushEntities=entities Removes JSON:API menu item entities from the Vuex state object.
* @param {flushEntitiesContext} context
*
* @example @lang js
* // Flush all menu entities.
* this.$store.commit('druxt/flushCollection', {})
*/
flushEntities (state, { prefix }) {
if (!prefix || typeof state.entities !== 'object') Vue.set(state, 'entities', {})
if (prefix) Vue.set(state.entities, prefix, {})
},
},

/**
Expand Down Expand Up @@ -120,3 +134,16 @@ export { DruxtMenuStore }
* @typedef {object} State
* @property {object} entities - The Drupal JSON:API Menu Item entities.
*/

/**
* Parameters for the `flushEntities` mutation.
*
* @typedef {object} flushEntitiesContext
*
* @param {string} [prefix] - (Optional) The JSON:API endpoint prefix or langcode.
*
* @example @lang js
* {
* prefix: 'en'
* }
*/
18 changes: 18 additions & 0 deletions packages/menu/test/stores/menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,22 @@ describe('DruxtStore', () => {
expect(store.$druxtMenu.get).toHaveBeenCalledWith('main', { test: true }, undefined)
store.dispatch('druxtMenu/get', 'name')
})

test('AddEntities', async () => {
expect(store.state.druxtMenu.entities).toStrictEqual({})
store.commit('druxtMenu/addEntities', { entities: [{ id: 'test' }] })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(1)
expect(store.state.druxtMenu.entities[undefined].test).toStrictEqual({ id: 'test' })
})

test('flushEntities', async () => {
expect(store.state.druxtMenu.entities).toStrictEqual({})
store.commit('druxtMenu/addEntities', { entities: [{ id: 'test' }] })
store.commit('druxtMenu/addEntities', { entities: [{ id: 'test2' }], prefix: 'es' })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(1)
store.commit('druxtMenu/flushEntities', { prefix: 'undefined' })
expect(Object.entries(store.state.druxtMenu.entities[undefined]).length).toBe(0)
store.commit('druxtMenu/flushEntities', {})
expect(Object.entries(store.state.druxtMenu.entities).length).toBe(0)
})
})

0 comments on commit 26b1bc6

Please sign in to comment.