diff --git a/static/js/admin/src/ResultObjectActionMenu.js b/static/js/admin/src/ResultObjectActionMenu.js index 0c30b5d19c..b35bca9219 100644 --- a/static/js/admin/src/ResultObjectActionMenu.js +++ b/static/js/admin/src/ResultObjectActionMenu.js @@ -446,19 +446,19 @@ define('ResultObjectActionMenu', [ 'jquery', 'jquery-ui', 'StringUtilities', 'A })(); break; case "patronPermissions": - perms_editor_store.commit('setPermissionType', 'Patron'); - perms_editor_store.commit('setMetadata', metadata); - perms_editor_store.commit('setShowModal', true); - perms_editor_store.commit('setAlertHandler', self.options.alertHandler); - perms_editor_store.commit('setActionHandler', self.actionHandler); - perms_editor_store.commit('setResultObject', resultObject); - perms_editor_store.commit('setResultObjects', null); + perms_editor_store.setPermissionType('Patron'); + perms_editor_store.setMetadata(metadata); + perms_editor_store.setShowModal(true); + perms_editor_store.setAlertHandler(self.options.alertHandler); + perms_editor_store.setActionHandler(self.actionHandler); + perms_editor_store.setResultObject(resultObject); + perms_editor_store.setResultObjects(null); break; case "staffPermissions": - perms_editor_store.commit('setPermissionType', 'Staff'); - perms_editor_store.commit('setMetadata', metadata); - perms_editor_store.commit('setShowModal', true); - perms_editor_store.commit('setAlertHandler', self.options.alertHandler); + perms_editor_store.setPermissionType('Staff'); + perms_editor_store.setMetadata(metadata); + perms_editor_store.setShowModal(true); + perms_editor_store.setAlertHandler(self.options.alertHandler); break; } }, diff --git a/static/js/admin/src/action/UpdatePatronAccessBatchAction.js b/static/js/admin/src/action/UpdatePatronAccessBatchAction.js index f0e2e65b82..6868e82bab 100644 --- a/static/js/admin/src/action/UpdatePatronAccessBatchAction.js +++ b/static/js/admin/src/action/UpdatePatronAccessBatchAction.js @@ -15,23 +15,23 @@ define('UpdatePatronAccessBatchAction', [ 'jquery', 'AbstractBatchAction'], func UpdatePatronAccessBatchAction.prototype.execute = function() { let targets = this.getTargets(); - perms_editor_store.commit('setPermissionType', 'Patron'); - perms_editor_store.commit('setAlertHandler', this.context.view.$alertHandler); - perms_editor_store.commit('setActionHandler', this.context.actionHandler); + perms_editor_store.setPermissionType('Patron'); + perms_editor_store.setAlertHandler(this.context.view.$alertHandler); + perms_editor_store.setActionHandler(this.context.actionHandler); if (targets.length == 1) { - perms_editor_store.commit('setResultObject', targets[0]); - perms_editor_store.commit('setResultObjects', null); - perms_editor_store.commit('setMetadata', targets[0].metadata); + perms_editor_store.setResultObject(targets[0]); + perms_editor_store.setResultObjects(null); + perms_editor_store.setMetadata(targets[0].metadata); } else { - perms_editor_store.commit('setResultObject', null); - perms_editor_store.commit('setResultObjects', targets); - perms_editor_store.commit('setMetadata', { + perms_editor_store.setResultObject(null); + perms_editor_store.setResultObjects(targets); + perms_editor_store.setMetadata({ title: targets.length + " objects", id: null, type: null }); } - perms_editor_store.commit('setShowModal', true); + perms_editor_store.setShowModal(true); AbstractBatchAction.prototype.execute.call(this); } diff --git a/static/js/admin/vue-permissions-editor/package-lock.json b/static/js/admin/vue-permissions-editor/package-lock.json index b6e14a6141..476a3dad7d 100644 --- a/static/js/admin/vue-permissions-editor/package-lock.json +++ b/static/js/admin/vue-permissions-editor/package-lock.json @@ -13,13 +13,14 @@ "lodash.clonedeep": "^4.5.0", "lodash.findindex": "^4.6.0", "lodash.isempty": "^4.4.0", + "pinia": "^2.1.7", "vue": "^3.4.5", - "vue-router": "^4.2.5", - "vuex": "^4.0.2" + "vue-router": "^4.2.5" }, "devDependencies": { "@babel/plugin-transform-runtime": "^7.23.7", "@babel/preset-env": "^7.23.7", + "@pinia/testing": "0.1.3", "@testing-library/jest-dom": "^6.2.0", "@vitejs/plugin-vue": "^4.6.2", "@vue/compiler-sfc": "^3.4.5", @@ -2644,6 +2645,47 @@ "integrity": "sha512-XuySG1E38YScSJoMlqovLru4KTUNSjgVTIjyh7qMX6aNN5HY5Ct5LhRJdxO79JtTzKfzV/bnWpz+zquYrISsvw==", "dev": true }, + "node_modules/@pinia/testing": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/@pinia/testing/-/testing-0.1.3.tgz", + "integrity": "sha512-D2Ds2s69kKFaRf2KCcP1NhNZEg5+we59aRyQalwRm7ygWfLM25nDH66267U3hNvRUOTx8ofL24GzodZkOmB5xw==", + "dev": true, + "dependencies": { + "vue-demi": ">=0.14.5" + }, + "funding": { + "url": "https://github.com/sponsors/posva" + }, + "peerDependencies": { + "pinia": ">=2.1.5" + } + }, + "node_modules/@pinia/testing/node_modules/vue-demi": { + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.14.6.tgz", + "integrity": "sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==", + "dev": true, + "hasInstallScript": true, + "bin": { + "vue-demi-fix": "bin/vue-demi-fix.js", + "vue-demi-switch": "bin/vue-demi-switch.js" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + }, + "peerDependencies": { + "@vue/composition-api": "^1.0.0-rc.1", + "vue": "^3.0.0-0 || ^2.6.0" + }, + "peerDependenciesMeta": { + "@vue/composition-api": { + "optional": true + } + } + }, "node_modules/@pkgjs/parseargs": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz", @@ -7177,6 +7219,56 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/pinia": { + "version": "2.1.7", + "resolved": "https://registry.npmjs.org/pinia/-/pinia-2.1.7.tgz", + "integrity": "sha512-+C2AHFtcFqjPih0zpYuvof37SFxMQ7OEG2zV9jRI12i9BOy3YQVAHwdKtyyc8pDcDyIc33WCIsZaCFWU7WWxGQ==", + "dependencies": { + "@vue/devtools-api": "^6.5.0", + "vue-demi": ">=0.14.5" + }, + "funding": { + "url": "https://github.com/sponsors/posva" + }, + "peerDependencies": { + "@vue/composition-api": "^1.4.0", + "typescript": ">=4.4.4", + "vue": "^2.6.14 || ^3.3.0" + }, + "peerDependenciesMeta": { + "@vue/composition-api": { + "optional": true + }, + "typescript": { + "optional": true + } + } + }, + "node_modules/pinia/node_modules/vue-demi": { + "version": "0.14.6", + "resolved": "https://registry.npmjs.org/vue-demi/-/vue-demi-0.14.6.tgz", + "integrity": "sha512-8QA7wrYSHKaYgUxDA5ZC24w+eHm3sYCbp0EzcDwKqN3p6HqtTCGR/GVsPyZW92unff4UlcSh++lmqDWN3ZIq4w==", + "hasInstallScript": true, + "bin": { + "vue-demi-fix": "bin/vue-demi-fix.js", + "vue-demi-switch": "bin/vue-demi-switch.js" + }, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + }, + "peerDependencies": { + "@vue/composition-api": "^1.0.0-rc.1", + "vue": "^3.0.0-0 || ^2.6.0" + }, + "peerDependenciesMeta": { + "@vue/composition-api": { + "optional": true + } + } + }, "node_modules/pirates": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.5.tgz", @@ -8109,17 +8201,6 @@ "vue": "^3.2.0" } }, - "node_modules/vuex": { - "version": "4.0.2", - "resolved": "https://registry.npmjs.org/vuex/-/vuex-4.0.2.tgz", - "integrity": "sha512-M6r8uxELjZIK8kTKDGgZTYX/ahzblnzC4isU1tpmEuOIIKmV+TRdc+H4s8ds2NuZ7wpUTdGRzJRtoj+lI+pc0Q==", - "dependencies": { - "@vue/devtools-api": "^6.0.0-beta.11" - }, - "peerDependencies": { - "vue": "^3.0.2" - } - }, "node_modules/w3c-hr-time": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz", diff --git a/static/js/admin/vue-permissions-editor/package.json b/static/js/admin/vue-permissions-editor/package.json index a52f1adfec..9a2bd3ec4b 100644 --- a/static/js/admin/vue-permissions-editor/package.json +++ b/static/js/admin/vue-permissions-editor/package.json @@ -14,13 +14,14 @@ "lodash.clonedeep": "^4.5.0", "lodash.findindex": "^4.6.0", "lodash.isempty": "^4.4.0", + "pinia": "^2.1.7", "vue": "^3.4.5", - "vue-router": "^4.2.5", - "vuex": "^4.0.2" + "vue-router": "^4.2.5" }, "devDependencies": { "@babel/plugin-transform-runtime": "^7.23.7", "@babel/preset-env": "^7.23.7", + "@pinia/testing": "0.1.3", "@testing-library/jest-dom": "^6.2.0", "@vitejs/plugin-vue": "^4.6.2", "@vue/compiler-sfc": "^3.4.5", diff --git a/static/js/admin/vue-permissions-editor/src/components/embargo.vue b/static/js/admin/vue-permissions-editor/src/components/embargo.vue index 8366cd600f..4713363750 100644 --- a/static/js/admin/vue-permissions-editor/src/components/embargo.vue +++ b/static/js/admin/vue-permissions-editor/src/components/embargo.vue @@ -45,7 +45,8 @@ diff --git a/static/js/vue-cdr-access/src/main.js b/static/js/vue-cdr-access/src/main.js index 3aa89c8fc9..f418148308 100644 --- a/static/js/vue-cdr-access/src/main.js +++ b/static/js/vue-cdr-access/src/main.js @@ -1,9 +1,9 @@ import { createApp, h } from 'vue' import { createI18n } from 'vue-i18n' import { createHead, VueHeadMixin } from "@vueuse/head" +import { createPinia } from 'pinia' import App from './App.vue' import router from './router' -import store from './store' import translations from '@/translations'; import './assets/common-styles.css'; import './assets/nouislider.css'; // Imported here, otherwise it breaks component tests, as an invalid import @@ -23,6 +23,8 @@ if (document.getElementById('app') !== null && window.dcr_browse_records === und } }); + const pinia = createPinia(); + // Set this here as it gives a build error otherwise window.matomoSiteSrcUrl = import.meta.env.VITE_MATOMO_SITE_SRC_URL || ''; @@ -31,8 +33,8 @@ if (document.getElementById('app') !== null && window.dcr_browse_records === und return h(App); } }).mixin(VueHeadMixin) + .use(pinia) .use(head) - .use(store) .use(router) .use(i18n).mount('#app'); } \ No newline at end of file diff --git a/static/js/vue-cdr-access/src/mixins/displayUtils.js b/static/js/vue-cdr-access/src/mixins/displayUtils.js index fff1c942bf..c7241e0237 100644 --- a/static/js/vue-cdr-access/src/mixins/displayUtils.js +++ b/static/js/vue-cdr-access/src/mixins/displayUtils.js @@ -10,7 +10,7 @@ export default { } else { id = idOrObject.id; // Don't apply browse types to works and files - if (idOrObject.type == 'Work' || idOrObject.type == 'File') { + if (idOrObject.type === 'Work' || idOrObject.type === 'File') { browse_type = null; } } diff --git a/static/js/vue-cdr-access/src/mixins/fullRecordUtils.js b/static/js/vue-cdr-access/src/mixins/fullRecordUtils.js index ae24245af9..ea1f246d4b 100644 --- a/static/js/vue-cdr-access/src/mixins/fullRecordUtils.js +++ b/static/js/vue-cdr-access/src/mixins/fullRecordUtils.js @@ -2,7 +2,8 @@ import modalMetadata from '@/components/modalMetadata.vue'; import thumbnail from '@/components/full_record/thumbnail.vue'; import permissionUtils from "./permissionUtils"; import { formatInTimeZone } from 'date-fns-tz'; -import { mapState } from 'vuex'; +import { mapState } from 'pinia'; +import { useAccessStore } from '../stores/access'; export default { components: { modalMetadata, thumbnail }, @@ -20,7 +21,7 @@ export default { }, computed: { - ...mapState([ + ...mapState(useAccessStore, [ 'isLoggedIn', 'username' ]), diff --git a/static/js/vue-cdr-access/src/mixins/headerUtils.js b/static/js/vue-cdr-access/src/mixins/headerUtils.js index f76d705c8b..a9934134b1 100644 --- a/static/js/vue-cdr-access/src/mixins/headerUtils.js +++ b/static/js/vue-cdr-access/src/mixins/headerUtils.js @@ -1,4 +1,5 @@ -import { mapState } from 'vuex'; +import { mapState } from 'pinia'; +import { useAccessStore } from '../stores/access'; export default { data() { @@ -8,7 +9,7 @@ export default { }, computed: { - ...mapState([ + ...mapState(useAccessStore, [ 'isLoggedIn', 'username', 'viewAdmin' diff --git a/static/js/vue-cdr-access/src/mixins/routeUtils.js b/static/js/vue-cdr-access/src/mixins/routeUtils.js index 4b9b3e24ce..2c49ce780d 100644 --- a/static/js/vue-cdr-access/src/mixins/routeUtils.js +++ b/static/js/vue-cdr-access/src/mixins/routeUtils.js @@ -1,4 +1,6 @@ import isEmpty from 'lodash.isempty'; +import { mapState } from 'pinia'; +import { useAccessStore } from '../stores/access'; const UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i; export default { @@ -162,9 +164,10 @@ export default { }, computed: { - possibleFacetFields() { - return this.$store.state.possibleFacetFields; - }, + ...mapState(useAccessStore, [ + 'possibleFacetFields' + ]), + minimumCreatedYear() { if (this.min_created_year !== undefined) { return parseInt(this.min_created_year) diff --git a/static/js/vue-cdr-access/src/router.js b/static/js/vue-cdr-access/src/router.js index 8138019dd6..e8a6e1df0b 100644 --- a/static/js/vue-cdr-access/src/router.js +++ b/static/js/vue-cdr-access/src/router.js @@ -1,4 +1,4 @@ -import { createWebHistory, createRouter } from 'vue-router' +import { createWebHistory, createRouter } from 'vue-router'; import axios from 'axios'; import advancedSearch from "@/components/advancedSearch.vue"; import displayWrapper from "@/components/displayWrapper.vue"; @@ -7,7 +7,7 @@ import searchWrapper from "@/components/searchWrapper.vue"; import collectionBrowseWrapper from "@/components/collectionBrowseWrapper.vue"; import frontPage from "@/components/frontPage.vue"; import aboutRepository from "@/components/aboutRepository.vue"; -import store from './store' +import { useAccessStore } from './stores/access'; const UUID_REGEX = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; @@ -66,10 +66,12 @@ const router = createRouter({ }); router.beforeEach((to, from) => { + const store = useAccessStore(); + axios.head('/userInformation').then((response) => { - store.commit('setUsername', response.headers['username']); - store.commit('setIsLoggedIn'); - store.commit('setViewAdmin', response.headers['can-view-admin']); + store.setUsername(response.headers['username']); + store.setIsLoggedIn(); + store.setViewAdmin(response.headers['can-view-admin']); }).catch(error => { console.log(error); }); diff --git a/static/js/vue-cdr-access/src/store.js b/static/js/vue-cdr-access/src/store.js deleted file mode 100644 index 53cc0a5fa0..0000000000 --- a/static/js/vue-cdr-access/src/store.js +++ /dev/null @@ -1,44 +0,0 @@ -import {createStore} from 'vuex' - -const POSSIBLE_FACET_FIELDS = ['unit', 'collection', 'format', 'genre', 'language', 'subject', 'location', - 'createdYear', 'creatorContributor', 'publisher']; - -// Create a new store instance. -const store = createStore({ - state () { - return { - isLoggedIn: false, - possibleFacetFields: POSSIBLE_FACET_FIELDS.slice(), - username: '', - viewAdmin: false - } - }, - mutations: { - removePossibleFacetFields (state, removeFacets) { - state.possibleFacetFields = POSSIBLE_FACET_FIELDS.slice().filter(f => removeFacets.indexOf(f) < 0); - }, - resetPossibleFacetFields (state) { - state.possibleFacetFields = POSSIBLE_FACET_FIELDS.slice(); - }, - setIsLoggedIn (state) { - state.isLoggedIn = state.username !== undefined && state.username !== ''; - }, - setUsername (state, username) { - state.username = username || ''; - }, - setViewAdmin (state, viewAdmin) { - state.viewAdmin = viewAdmin === 'true'; - } - }, - actions: { - /** - * Used to reset the store back to its initial state, primarily for testing - * @param context - */ - resetState (context) { - context.commit("resetPossibleFacetFields"); - } - } -}); - -export default store; \ No newline at end of file diff --git a/static/js/vue-cdr-access/src/stores/access.js b/static/js/vue-cdr-access/src/stores/access.js new file mode 100644 index 0000000000..4ecf7d6a41 --- /dev/null +++ b/static/js/vue-cdr-access/src/stores/access.js @@ -0,0 +1,37 @@ +import { defineStore } from 'pinia' + +const POSSIBLE_FACET_FIELDS = ['unit', 'collection', 'format', 'genre', 'language', 'subject', 'location', + 'createdYear', 'creatorContributor', 'publisher']; + +export const useAccessStore = defineStore({ + id: 'access', + state: () => ({ + isLoggedIn: false, + possibleFacetFields: POSSIBLE_FACET_FIELDS.slice(), + username: '', + viewAdmin: false + }), + actions: { + removePossibleFacetFields (removeFacets) { + this.possibleFacetFields = POSSIBLE_FACET_FIELDS.slice().filter(f => removeFacets.indexOf(f) < 0); + }, + resetPossibleFacetFields () { + this.possibleFacetFields = POSSIBLE_FACET_FIELDS.slice(); + }, + setIsLoggedIn () { + this.isLoggedIn = this.username !== undefined && this.username !== ''; + }, + setUsername (username) { + this.username = username || ''; + }, + setViewAdmin (viewAdmin) { + this.viewAdmin = viewAdmin === 'true'; + }, + /** + * Used to reset the store back to its initial state, primarily for testing + */ + resetState () { + this.possibleFacetFields = POSSIBLE_FACET_FIELDS; + } + } +}); \ No newline at end of file diff --git a/static/js/vue-cdr-access/tests/unit/aboutRepository.spec.js b/static/js/vue-cdr-access/tests/unit/aboutRepository.spec.js index 53904b2ca2..b586dc1a43 100644 --- a/static/js/vue-cdr-access/tests/unit/aboutRepository.spec.js +++ b/static/js/vue-cdr-access/tests/unit/aboutRepository.spec.js @@ -1,13 +1,14 @@ import {RouterLinkStub, shallowMount} from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import { createTestingPinia } from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import aboutRepository from '@/components/aboutRepository.vue'; import displayWrapper from "@/components/displayWrapper.vue"; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; import moxios from "moxios"; -let wrapper, router; +let wrapper, router, store; describe('aboutRepository.vue', () => { const i18n = createI18n({ @@ -36,15 +37,19 @@ describe('aboutRepository.vue', () => { }); wrapper = shallowMount(aboutRepository, { global: { - plugins: [i18n, router, store], + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); }); afterEach(function () { + store.$reset(); moxios.uninstall(); }); diff --git a/static/js/vue-cdr-access/tests/unit/advancedSearch.spec.js b/static/js/vue-cdr-access/tests/unit/advancedSearch.spec.js index d68f8e45c1..b37f35f7df 100644 --- a/static/js/vue-cdr-access/tests/unit/advancedSearch.spec.js +++ b/static/js/vue-cdr-access/tests/unit/advancedSearch.spec.js @@ -1,13 +1,14 @@ import { shallowMount, RouterLinkStub } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import advancedSearch from '@/components/advancedSearch.vue'; import displayWrapper from "@/components/displayWrapper.vue"; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; import moxios from "moxios"; -let wrapper, router; +let wrapper, router, store; describe('advancedSearch.vue', () => { const i18n = createI18n({ @@ -36,15 +37,19 @@ describe('advancedSearch.vue', () => { }); wrapper = shallowMount(advancedSearch, { global: { - plugins: [i18n, router, store], + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); }); afterEach(function () { + store.$reset(); moxios.uninstall(); }); diff --git a/static/js/vue-cdr-access/tests/unit/analyticsUtils.spec.js b/static/js/vue-cdr-access/tests/unit/analyticsUtils.spec.js index 0cdb651b27..8cbb12ad44 100644 --- a/static/js/vue-cdr-access/tests/unit/analyticsUtils.spec.js +++ b/static/js/vue-cdr-access/tests/unit/analyticsUtils.spec.js @@ -1,14 +1,15 @@ import {mount, RouterLinkStub} from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import advancedSearch from '@/components/advancedSearch.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import analyticsUtils from "../../src/mixins/analyticsUtils"; -import store from '@/store'; import { createI18n } from "vue-i18n"; import translations from "@/translations"; import moxios from "moxios"; -let wrapper; +let wrapper, store; describe('analyticsUtils', () => { const i18n = createI18n({ locale: 'en', @@ -36,19 +37,22 @@ describe('analyticsUtils', () => { }); afterEach(() => { - moxios.uninstall() + store.$reset(); + moxios.uninstall(); }); it("sends pageviews to analytics platforms", () => { wrapper = mount(advancedSearch, { global: { - plugins: [router, store, i18n], + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); - + store = useAccessStore(); expect(pageView).toHaveBeenCalledWith("Advanced Search"); }); @@ -142,14 +146,16 @@ describe('analyticsUtils', () => { }; wrapper = mount(displayWrapper, { global: { - plugins: [store, i18n], + plugins: [i18n, createTestingPinia({ + stubActions: false + })], mocks: { $route }, stubs: { RouterLink: RouterLinkStub } } }); - + store = useAccessStore(); moxios.wait(() => { expect(pageEvent).toHaveBeenCalledWith(briefObj); expect(pageView).toHaveBeenCalledWith(briefObj.title); diff --git a/static/js/vue-cdr-access/tests/unit/browseSearch.spec.js b/static/js/vue-cdr-access/tests/unit/browseSearch.spec.js index 82d01c7629..646c541917 100644 --- a/static/js/vue-cdr-access/tests/unit/browseSearch.spec.js +++ b/static/js/vue-cdr-access/tests/unit/browseSearch.spec.js @@ -1,13 +1,14 @@ import { flushPromises, mount } from '@vue/test-utils'; import { createI18n } from 'vue-i18n'; import { createRouter, createWebHistory } from 'vue-router'; -import store from '@/store'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import browseSearch from '@/components/browseSearch.vue'; import displayWrapper from "@/components/displayWrapper.vue"; import translations from "@/translations"; const query = 'Test Collection'; -let wrapper, router; +let wrapper, router, store; describe('browseSearch.vue', () => { const i18n = createI18n({ @@ -29,19 +30,22 @@ describe('browseSearch.vue', () => { }); wrapper = mount(browseSearch, { global: { - plugins: [i18n, store, router] + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })] }, props: { objectType: 'Folder' } }); + store = useAccessStore(); await router.push('/record/1234'); wrapper.vm.$router.currentRoute.value.query.anywhere = ''; }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); @@ -70,9 +74,12 @@ describe('browseSearch.vue', () => { mocks: { $route }, - plugins: [i18n, store] + plugins: [i18n, createTestingPinia({ + stubActions: false + })] } }); + store = useAccessStore(); expect(wrapper.find('input').attributes('placeholder')).toBe('Search within this object'); }); @@ -88,13 +95,15 @@ describe('browseSearch.vue', () => { mocks: { $route }, - plugins: [i18n, store] + plugins: [i18n, createTestingPinia({ + stubActions: false + })] }, props: { objectType: 'Folder' } }); - + store = useAccessStore(); expect(wrapper.vm.search_query).toEqual('Test Folder'); }); }); \ No newline at end of file diff --git a/static/js/vue-cdr-access/tests/unit/browseSort.spec.js b/static/js/vue-cdr-access/tests/unit/browseSort.spec.js index 6071811619..d1f907fca9 100644 --- a/static/js/vue-cdr-access/tests/unit/browseSort.spec.js +++ b/static/js/vue-cdr-access/tests/unit/browseSort.spec.js @@ -1,13 +1,14 @@ import { mount, flushPromises } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import browseSort from '@/components/browseSort.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import searchWrapper from '@/components/searchWrapper.vue'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; -let wrapper, wrapper_search, router; +let wrapper, wrapper_search, router, store; describe('browseSort.vue', () => { const i18n = createI18n({ @@ -34,7 +35,7 @@ describe('browseSort.vue', () => { }); wrapper = mount(browseSort, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n] }, props: { browseType: 'display' @@ -43,12 +44,16 @@ describe('browseSort.vue', () => { wrapper_search = mount(browseSort, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { browseType: 'search' } }); + + store = useAccessStore(); }); it("shows the default, 'Relevance', option when mounted with no sort specified", () => { @@ -82,10 +87,9 @@ describe('browseSort.vue', () => { }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); wrapper = null; - wrapper_search.vm.$store.dispatch("resetState"); wrapper_search = null; router = null; + store.$reset(); }); }); diff --git a/static/js/vue-cdr-access/tests/unit/clearFilters.spec.js b/static/js/vue-cdr-access/tests/unit/clearFilters.spec.js index 23f7539af2..72a7894e4e 100644 --- a/static/js/vue-cdr-access/tests/unit/clearFilters.spec.js +++ b/static/js/vue-cdr-access/tests/unit/clearFilters.spec.js @@ -1,12 +1,13 @@ import { flushPromises, mount } from '@vue/test-utils'; import { createI18n } from 'vue-i18n'; import { createRouter, createWebHistory } from 'vue-router'; -import store from '@/store'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import displayWrapper from "@/components/displayWrapper.vue"; import clearFilters from "@/components/clearFilters.vue"; import translations from "@/translations"; -let wrapper, router; +let wrapper, router, store; describe('clearFilters.vue', () => { const i18n = createI18n({ @@ -28,19 +29,22 @@ describe('clearFilters.vue', () => { }); wrapper = mount(clearFilters, { global: { - plugins: [i18n, store, router] + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })] }, props: { filterParameters: {} } }); + store = useAccessStore(); await router.push('/record/1234'); wrapper.vm.$router.currentRoute.value.query.anywhere = ''; }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); diff --git a/static/js/vue-cdr-access/tests/unit/collectionFolder.spec.js b/static/js/vue-cdr-access/tests/unit/collectionFolder.spec.js index 34ff3af090..8f8ad294f1 100644 --- a/static/js/vue-cdr-access/tests/unit/collectionFolder.spec.js +++ b/static/js/vue-cdr-access/tests/unit/collectionFolder.spec.js @@ -1,8 +1,9 @@ import {mount, RouterLinkStub} from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import collectionFolder from '@/components/full_record/collectionFolder.vue'; import displayWrapper from '@/components/displayWrapper.vue'; -import store from '@/store'; import {createI18n} from 'vue-i18n'; import translations from '@/translations'; import cloneDeep from 'lodash.clonedeep'; @@ -96,7 +97,7 @@ const recordData = { resourceType: "Collection" }; -let wrapper, router; +let wrapper, router, store; describe('collectionFolder.vue', () => { const i18n = createI18n({ @@ -119,7 +120,9 @@ describe('collectionFolder.vue', () => { wrapper = mount(collectionFolder, { global: { - plugins: [i18n, router, store], + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } @@ -128,6 +131,12 @@ describe('collectionFolder.vue', () => { recordData: recordData } }); + + store = useAccessStore(); + }); + + afterEach(function () { + store.$reset(); }); it('displays a header', () => { diff --git a/static/js/vue-cdr-access/tests/unit/displayWrapper.spec.js b/static/js/vue-cdr-access/tests/unit/displayWrapper.spec.js index 40debf980c..6ada31aa7c 100644 --- a/static/js/vue-cdr-access/tests/unit/displayWrapper.spec.js +++ b/static/js/vue-cdr-access/tests/unit/displayWrapper.spec.js @@ -1,13 +1,14 @@ import {mount, flushPromises, RouterLinkStub} from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import { createTestingPinia } from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import displayWrapper from '@/components/displayWrapper.vue'; -import store from '@/store'; import moxios from "moxios"; import {createI18n} from "vue-i18n"; import translations from "@/translations"; import { response, briefObjectData } from "../fixtures/displayWrapperFixtures"; -let wrapper, router; +let wrapper, router, store; describe('displayWrapper.vue', () => { const i18n = createI18n({ @@ -31,6 +32,10 @@ describe('displayWrapper.vue', () => { }); }); + afterEach(() => { + store.$reset(); + }); + function mountApp(data_overrides = {}) { const default_data = { container_name: '', @@ -43,7 +48,9 @@ describe('displayWrapper.vue', () => { let data = {...default_data, ...data_overrides}; wrapper = mount(displayWrapper, { global: { - plugins: [router, store, i18n], + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } @@ -52,6 +59,7 @@ describe('displayWrapper.vue', () => { return data; } }); + store = useAccessStore(); } function stubQueryResponse(url_pattern, response) { @@ -207,8 +215,8 @@ describe('displayWrapper.vue', () => { await flushPromises(); // Verify that there are still other facets, but that the unit facet has been removed - expect(wrapper.vm.$store.state.possibleFacetFields.length).toBeGreaterThan(0); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('unit')).toEqual(-1); + expect(store.possibleFacetFields.length).toBeGreaterThan(0); + expect(store.possibleFacetFields.indexOf('unit')).toEqual(-1); // Verify that record list is displaying, indicating that a request was made which did not include unit facet expect(wrapper.find('#fullRecordSearchResultDisplay').exists()).toBe(true); }); @@ -226,9 +234,9 @@ describe('displayWrapper.vue', () => { await flushPromises(); // Verify that there are still other facets, but that the unit and collection facets have been removed - expect(wrapper.vm.$store.state.possibleFacetFields.length).toBeGreaterThan(0); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('unit')).toEqual(-1); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('collection')).toEqual(-1); + expect(store.possibleFacetFields.length).toBeGreaterThan(0); + expect(store.possibleFacetFields.indexOf('unit')).toEqual(-1); + expect(store.possibleFacetFields.indexOf('collection')).toEqual(-1); // Verify that record list is displaying, indicating that a request was made which did not include unwanted facets expect(wrapper.find('#fullRecordSearchResultDisplay').exists()).toBe(true); }); @@ -267,9 +275,9 @@ describe('displayWrapper.vue', () => { await flushPromises(); // Verify that there are still other facets, but that the unit and collection facets have been removed - expect(wrapper.vm.$store.state.possibleFacetFields.length).toBeGreaterThan(0); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('unit')).toEqual(-1); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('collection')).toEqual(-1); + expect(store.possibleFacetFields.length).toBeGreaterThan(0); + expect(store.possibleFacetFields.indexOf('unit')).toEqual(-1); + expect(store.possibleFacetFields.indexOf('collection')).toEqual(-1); // Verify that record list is displaying, indicating that a request was made which did not include unwanted facets expect(wrapper.find('#fullRecordSearchResultDisplay').exists()).toBe(true); }); @@ -303,16 +311,16 @@ describe('displayWrapper.vue', () => { await flushPromises(); // Verify that there are still other facets, but that the unit facet has been removed - let num_facets = wrapper.vm.$store.state.possibleFacetFields.length; + let num_facets = store.possibleFacetFields.length; expect(num_facets).toBeGreaterThan(0); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('unit')).toEqual(-1); + expect(store.possibleFacetFields.indexOf('unit')).toEqual(-1); // Trigger works only filter and make sure that the set of facets does not change await wrapper.find('#works-only').trigger('click'); await flushPromises(); - expect(wrapper.vm.$store.state.possibleFacetFields.length).toEqual(num_facets); - expect(wrapper.vm.$store.state.possibleFacetFields.indexOf('unit')).toEqual(-1); + expect(store.possibleFacetFields.length).toEqual(num_facets); + expect(store.possibleFacetFields.indexOf('unit')).toEqual(-1); expect(wrapper.vm.$route.query.facetSelect.indexOf('unit')).toEqual(-1); }); @@ -350,7 +358,7 @@ describe('displayWrapper.vue', () => { afterEach(() => { moxios.uninstall(); - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; // Reset the dom to avoid tags added persisting across tests diff --git a/static/js/vue-cdr-access/tests/unit/facets.spec.js b/static/js/vue-cdr-access/tests/unit/facets.spec.js index cc709f6ec7..d5c4ce1edf 100644 --- a/static/js/vue-cdr-access/tests/unit/facets.spec.js +++ b/static/js/vue-cdr-access/tests/unit/facets.spec.js @@ -1,15 +1,16 @@ import {flushPromises, mount} from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import facets from '@/components/facets.vue'; import searchWrapper from '@/components/searchWrapper.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import moxios from 'moxios'; -import store from '@/store'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; const end_year = new Date().getFullYear(); -let router, wrapper, collection, selected_facet, selected_sub_facet; +let router, wrapper, collection, selected_facet, selected_sub_facet, store; describe('facets.vue', () => { const i18n = createI18n({ @@ -37,7 +38,9 @@ describe('facets.vue', () => { wrapper = mount(facets, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { minCreatedYear: 2011, @@ -147,6 +150,7 @@ describe('facets.vue', () => { } } }); + store = useAccessStore(); let facet_list = wrapper.findAll('.facet-display a'); collection = facet_list[0]; @@ -155,7 +159,7 @@ describe('facets.vue', () => { }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); @@ -243,7 +247,9 @@ describe('facets.vue', () => { it("does not display facets with no returned results", () => { let emptyFacetWrapper = mount(facets, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { minSearchYear: 2011, @@ -284,6 +290,7 @@ describe('facets.vue', () => { } } }); + store = useAccessStore(); let facet_headers = emptyFacetWrapper.findAll('.facet-display h3'); let facet_list = emptyFacetWrapper.findAll('.facet-display li'); @@ -302,7 +309,9 @@ describe('facets.vue', () => { it("does not display date facets with no minimum search year set", () => { let emptyFacetWrapper = mount(facets, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { minCreatedYear: undefined, @@ -331,6 +340,7 @@ describe('facets.vue', () => { } } }); + store = useAccessStore(); let facet_headers = emptyFacetWrapper.findAll('.facet-display h3'); let facet_list = emptyFacetWrapper.findAll('.facet-display li'); @@ -486,7 +496,9 @@ describe('facets.vue', () => { it("selecting Unknown date does not select Unknown format", (done) => { wrapper = mount(facets, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { minCreatedYear: 2015, @@ -530,6 +542,7 @@ describe('facets.vue', () => { } } }); + store = useAccessStore(); moxios.wait(async () => { await router.push('/search/?createdYear=unknown'); diff --git a/static/js/vue-cdr-access/tests/unit/frontPage.spec.js b/static/js/vue-cdr-access/tests/unit/frontPage.spec.js index b3e9d8fc61..e024d9cdb9 100644 --- a/static/js/vue-cdr-access/tests/unit/frontPage.spec.js +++ b/static/js/vue-cdr-access/tests/unit/frontPage.spec.js @@ -1,13 +1,15 @@ import {shallowMount, RouterLinkStub} from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import frontPage from '@/components/frontPage.vue'; import displayWrapper from "@/components/displayWrapper.vue"; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; import moxios from "moxios"; -let wrapper, router; + +let wrapper, router, store; describe('frontPage.vue', () => { const i18n = createI18n({ @@ -36,15 +38,20 @@ describe('frontPage.vue', () => { }); wrapper = shallowMount(frontPage, { global: { - plugins: [i18n, router, store], + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + + store = useAccessStore(); }); afterEach(function () { + store.$reset(); moxios.uninstall(); }); diff --git a/static/js/vue-cdr-access/tests/unit/headerHome.spec.js b/static/js/vue-cdr-access/tests/unit/headerHome.spec.js index d9b8dc2655..1307e2e2ff 100644 --- a/static/js/vue-cdr-access/tests/unit/headerHome.spec.js +++ b/static/js/vue-cdr-access/tests/unit/headerHome.spec.js @@ -1,28 +1,26 @@ import {mount, RouterLinkStub} from '@vue/test-utils'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import dcrHeader from '@/components/header/headerHome.vue'; -let wrapper; +let wrapper, store; describe('headerHome.vue', () => { - const $store = { - state: { - isLoggedIn: false, - username: '', - viewAdmin: false - }, - commit: jest.fn() - }; - beforeEach(() => { wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); + }); + + afterEach(() => { + store.$reset(); }); it("loads headerHome on the home page", () => { @@ -38,49 +36,49 @@ describe('headerHome.vue', () => { }); it("logout button present when logged in", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: false - }, - commit: jest.fn() - }; const current_page = window.location; const logoutUrl = `https://${current_page.host}/Shibboleth.sso/Logout?return=https://sso.unc.edu/idp/logout.jsp?return_url=${encodeURIComponent(current_page)}`; wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user', + viewAdmin: false + } + }, + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); expect(wrapper.html()).toContain(logoutUrl); }); it("admin link present when user logged in as admin", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: true - }, - commit: jest.fn() - }; const adminUrl = `https://${window.location.host}/admin/`; wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user', + viewAdmin: true + } + }, + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); expect(wrapper.html()).toContain(adminUrl); }); }); \ No newline at end of file diff --git a/static/js/vue-cdr-access/tests/unit/headerSmall.spec.js b/static/js/vue-cdr-access/tests/unit/headerSmall.spec.js index 5fc53871b7..14898ce700 100644 --- a/static/js/vue-cdr-access/tests/unit/headerSmall.spec.js +++ b/static/js/vue-cdr-access/tests/unit/headerSmall.spec.js @@ -1,28 +1,26 @@ import {mount, RouterLinkStub} from '@vue/test-utils'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import dcrHeader from '@/components/header/headerSmall.vue'; -let wrapper; +let wrapper, store; describe('headerSmall.vue', () => { - const $store = { - state: { - isLoggedIn: false, - username: '', - viewAdmin: false - }, - commit: jest.fn() - }; - beforeEach(() => { wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); + }); + + afterEach(() => { + store.$reset(); }); it("loads the small header", () => { @@ -38,21 +36,20 @@ describe('headerSmall.vue', () => { }); it("logout button present when logged in", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: false - }, - commit: jest.fn() - }; const current_page = window.location; const logoutUrl = `https://${current_page.host}/Shibboleth.sso/Logout?return=https://sso.unc.edu/idp/logout.jsp?return_url=${encodeURIComponent(current_page)}`; wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user', + viewAdmin: false + } + }, + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } @@ -62,20 +59,19 @@ describe('headerSmall.vue', () => { }); it("admin link present when user logged in as admin", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: true - }, - commit: jest.fn() - }; const adminUrl = `https://${window.location.host}/admin/`; wrapper = mount(dcrHeader, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user', + viewAdmin: true + } + }, + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } diff --git a/static/js/vue-cdr-access/tests/unit/headerUtils.spec.js b/static/js/vue-cdr-access/tests/unit/headerUtils.spec.js index 81b3dc3b52..34e177d2f2 100644 --- a/static/js/vue-cdr-access/tests/unit/headerUtils.spec.js +++ b/static/js/vue-cdr-access/tests/unit/headerUtils.spec.js @@ -1,75 +1,49 @@ import {mount, RouterLinkStub} from '@vue/test-utils'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import headerSmall from '@/components/header/headerSmall.vue'; +let wrapper, store; describe('headerSmallUtils', () => { - it("jumpToAdminUrl is record-specific admin url", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: true - }, - commit: jest.fn() - }; - const current_page = window.location; - const testUrl = `https://${current_page.hostname}/record/73bc003c-9603-4cd9-8a65-93a22520ef6a`; - const adminUrl = `https://${current_page.hostname}/admin/list/73bc003c-9603-4cd9-8a65-93a22520ef6a`; - window.location = Object.assign(new URL(testUrl)); - const wrapper = mount(headerSmall, { + beforeEach(() => { + wrapper = mount(headerSmall, { global: { - mocks: { - $store - }, + plugins: [createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user', + viewAdmin: true + } + }, + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); + store = useAccessStore(); + }) + + afterEach(() => { + store.$reset(); + }); + + it("jumpToAdminUrl is record-specific admin url", () => { + const current_page = window.location; + const testUrl = `https://${current_page.hostname}/record/73bc003c-9603-4cd9-8a65-93a22520ef6a`; + const adminUrl = `https://${current_page.hostname}/admin/list/73bc003c-9603-4cd9-8a65-93a22520ef6a`; + window.location = Object.assign(new URL(testUrl)); expect(wrapper.html()).toContain(adminUrl); }); it("jumpToAdminUrl is admin url", () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: true - }, - commit: jest.fn() - }; const adminUrl = `https://${window.location.host}/admin/`; - const wrapper = mount(headerSmall, { - global: { - mocks: { - $store - }, - stubs: { - RouterLink: RouterLinkStub - } - } - }); expect(wrapper.html()).toContain(adminUrl); }); it("opens and closes the mobile menu", async () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user', - viewAdmin: true - }, - commit: jest.fn() - }; - const wrapper = mount(headerSmall, { - global: { - mocks: { - $store - }, - stubs: { - RouterLink: RouterLinkStub - } - } - }); // Default expect(wrapper.find('#navbar-burger').attributes('aria-expanded')).toEqual('false'); expect(wrapper.find('#navbar').classes()).not.toContain('active'); diff --git a/static/js/vue-cdr-access/tests/unit/matomoUtils.spec.js b/static/js/vue-cdr-access/tests/unit/matomoUtils.spec.js index db7d42fab0..fb87cd8a23 100644 --- a/static/js/vue-cdr-access/tests/unit/matomoUtils.spec.js +++ b/static/js/vue-cdr-access/tests/unit/matomoUtils.spec.js @@ -1,14 +1,15 @@ import {mount, RouterLinkStub} from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import { createTestingPinia } from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import App from '@/App.vue'; import matomoUtils from "../../src/mixins/matomoUtils"; import displayWrapper from '@/components/displayWrapper.vue'; -import store from '@/store'; import { createI18n } from "vue-i18n"; import translations from "@/translations"; -let wrapper; +let wrapper, store; describe('matomoUtils', () => { const i18n = createI18n({ locale: 'en', @@ -38,17 +39,23 @@ describe('matomoUtils', () => { jest.resetAllMocks(); }); + afterEach(() => { + store.$reset(); + }) + it("loads the matamo script", () => { wrapper = mount(App, { attachTo: '#root', global: { - plugins: [router, store, i18n], + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })], stubs: { RouterLink: RouterLinkStub } } }); - + store = useAccessStore(); expect(matomoSetup).toHaveBeenCalled(); }); }); \ No newline at end of file diff --git a/static/js/vue-cdr-access/tests/unit/notAvailable.spec.js b/static/js/vue-cdr-access/tests/unit/notAvailable.spec.js index 086dd8b1cc..5ef9dea617 100644 --- a/static/js/vue-cdr-access/tests/unit/notAvailable.spec.js +++ b/static/js/vue-cdr-access/tests/unit/notAvailable.spec.js @@ -1,10 +1,10 @@ import { shallowMount } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import { createTestingPinia } from '@pinia/testing'; import notAvailable from '@/components/error_pages/notAvailable.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; let wrapper, router; @@ -30,7 +30,9 @@ describe('notAvailable.vue', () => { wrapper = shallowMount(notAvailable, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] } }); }); diff --git a/static/js/vue-cdr-access/tests/unit/notFound.spec.js b/static/js/vue-cdr-access/tests/unit/notFound.spec.js index 60d7a0bac0..bcbfc7f2ed 100644 --- a/static/js/vue-cdr-access/tests/unit/notFound.spec.js +++ b/static/js/vue-cdr-access/tests/unit/notFound.spec.js @@ -1,13 +1,14 @@ import { shallowMount } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import notFound from '@/components/error_pages/notFound.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; -let wrapper, router; +let wrapper, router, store; describe('notFound.vue', () => { const i18n = createI18n({ @@ -30,9 +31,16 @@ describe('notFound.vue', () => { wrapper = shallowMount(notFound, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] } }); + store = useAccessStore(); + }); + + afterEach(() => { + store.$reset(); }); it('displays the DCR header and a "not found" message by default', () => { @@ -56,20 +64,20 @@ describe('notFound.vue', () => { }); it('does not display a login link if a user is already logged in"', async () => { - const $store = { - state: { - isLoggedIn: true, - username: 'testUser' - } - } - const wrapper = shallowMount(notFound, { + const wrapper = shallowMount(notFound, { global: { - plugins: [store, i18n], - mocks: { - $store - } + plugins: [store, i18n, createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'testUser' + } + }, + stubActions: false + }) + ] } - }) + }); const links = wrapper.findAll('a'); expect(links.length).toEqual(2); diff --git a/static/js/vue-cdr-access/tests/unit/pagination.spec.js b/static/js/vue-cdr-access/tests/unit/pagination.spec.js index de9851eccd..6731c15bc7 100644 --- a/static/js/vue-cdr-access/tests/unit/pagination.spec.js +++ b/static/js/vue-cdr-access/tests/unit/pagination.spec.js @@ -1,14 +1,15 @@ import { shallowMount, flushPromises } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import '@testing-library/jest-dom'; import pagination from '@/components/pagination.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import searchWrapper from '@/components/searchWrapper.vue'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; -let router, wrapper; +let router, wrapper, store; describe('pagination.vue', () => { const i18n = createI18n({ @@ -35,7 +36,9 @@ describe('pagination.vue', () => { }); wrapper = shallowMount(pagination, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { browseType: 'display', @@ -50,12 +53,13 @@ describe('pagination.vue', () => { } } }); + store = useAccessStore(); await router.push('/record/1234'); }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); diff --git a/static/js/vue-cdr-access/tests/unit/restrictedContent.spec.js b/static/js/vue-cdr-access/tests/unit/restrictedContent.spec.js index 3df743f626..56bbf9f4ac 100644 --- a/static/js/vue-cdr-access/tests/unit/restrictedContent.spec.js +++ b/static/js/vue-cdr-access/tests/unit/restrictedContent.spec.js @@ -1,5 +1,7 @@ import { mount } from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import restrictedContent from '@/components/full_record/restrictedContent.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import {createI18n} from 'vue-i18n'; @@ -337,7 +339,7 @@ const record = { resourceType: "File" } -let wrapper, router; +let wrapper, router, store; describe('restrictedContent.vue', () => { const i18n = createI18n({ @@ -362,46 +364,42 @@ describe('restrictedContent.vue', () => { ] }); - const $store = { - state: { - username: '' - }, - commit: jest.fn() - } - wrapper = mount(restrictedContent, { attachTo: '#root', global: { - plugins: [i18n, router], - mocks: { - $store - } + plugins: [i18n, router, createTestingPinia({ + stubActions: false + })] }, props: { recordData: record } }); + store = useAccessStore(); + }); + + afterEach(() => { + store.$reset(); }); it('does not show view options if a user is logged in', async () => { - const $store = { - state: { - isLoggedIn: true, - username: 'test_user' - }, - commit: jest.fn() - } wrapper = mount(restrictedContent, { global: { - plugins: [i18n, router], - mocks: { - $store - } + plugins: [i18n, router, createTestingPinia({ + initialState: { + access: { + isLoggedIn: true, + username: 'test_user' + } + }, + stubActions: false + })] }, props: { recordData: record } }); + store = useAccessStore(); expect(wrapper.find('.restricted-access').exists()).toBe(false); }); diff --git a/static/js/vue-cdr-access/tests/unit/routeUtils.spec.js b/static/js/vue-cdr-access/tests/unit/routeUtils.spec.js index d41eef341f..28babeb89f 100644 --- a/static/js/vue-cdr-access/tests/unit/routeUtils.spec.js +++ b/static/js/vue-cdr-access/tests/unit/routeUtils.spec.js @@ -1,13 +1,14 @@ import { shallowMount } from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import pagination from '@/components/pagination.vue' import displayWrapper from "@/components/displayWrapper.vue"; import routeUtils from '@/mixins/routeUtils.js'; -import store from '@/store'; const gallery = 'gallery-display'; const list_display = 'list-display'; -let wrapper, router; +let wrapper, router, store; describe('routeUtils', () => { beforeEach(async () => { @@ -24,14 +25,17 @@ describe('routeUtils', () => { // Set wrapper using any component that uses routeUtils mixin to avoid test warnings about missing template wrapper = shallowMount(pagination, { global: { - plugins: [router, store] + plugins: [router, createTestingPinia({ + stubActions: false + })] } }); + store = useAccessStore(); await router.push('/record/1234'); }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); diff --git a/static/js/vue-cdr-access/tests/unit/viewType.spec.js b/static/js/vue-cdr-access/tests/unit/viewType.spec.js index 8d9717d2b8..c20a36ce4e 100644 --- a/static/js/vue-cdr-access/tests/unit/viewType.spec.js +++ b/static/js/vue-cdr-access/tests/unit/viewType.spec.js @@ -1,12 +1,13 @@ import { shallowMount, flushPromises } from '@vue/test-utils' import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import viewType from '@/components/viewType.vue' import displayWrapper from "@/components/displayWrapper.vue"; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; -let wrapper, btns, router; +let wrapper, btns, router, store; describe('viewType.vue', () => { const i18n = createI18n({ @@ -29,15 +30,18 @@ describe('viewType.vue', () => { sessionStorage.clear(); wrapper = shallowMount(viewType, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] } }); + store = useAccessStore(); btns = wrapper.findAll('#browse-btns i'); }); afterEach(() => { - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); wrapper = null; router = null; }); diff --git a/static/js/vue-cdr-access/tests/unit/worksOnly.spec.js b/static/js/vue-cdr-access/tests/unit/worksOnly.spec.js index 85e860285f..99d5224fb3 100644 --- a/static/js/vue-cdr-access/tests/unit/worksOnly.spec.js +++ b/static/js/vue-cdr-access/tests/unit/worksOnly.spec.js @@ -1,13 +1,13 @@ import { shallowMount, flushPromises } from '@vue/test-utils'; import { createRouter, createWebHistory } from 'vue-router'; +import {createTestingPinia} from '@pinia/testing'; +import { useAccessStore } from '@/stores/access'; import worksOnly from '@/components/worksOnly.vue'; import displayWrapper from '@/components/displayWrapper.vue'; import {createI18n} from "vue-i18n"; import translations from "@/translations"; -import store from '@/store'; - -let wrapper, record_input, router; +let wrapper, record_input, router, store; describe('worksOnly.vue', () => { const i18n = createI18n({ @@ -29,7 +29,9 @@ describe('worksOnly.vue', () => { }); wrapper = shallowMount(worksOnly, { global: { - plugins: [router, store, i18n] + plugins: [router, i18n, createTestingPinia({ + stubActions: false + })] }, props: { adminUnit: false @@ -40,12 +42,11 @@ describe('worksOnly.vue', () => { } } }); + store = useAccessStore(); record_input = wrapper.find('input'); }); - afterEach(() => router = null); - it("updates route to only show works if button is checked for a gallery view", async () => { await router.push('/record/1234/?browse_type=gallery-display'); await wrapper.setData({ works_only: false }); @@ -84,7 +85,8 @@ describe('worksOnly.vue', () => { afterEach(() => { // Make sure box is unchecked wrapper.setData({ works_only: false }); - wrapper.vm.$store.dispatch("resetState"); + store.$reset(); + router = null; wrapper = null; }); }); \ No newline at end of file