Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add layout properties to the UI store and cookie to store them (#1955)

Olga Bulat8221e6d4dddc
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 6064f9769..871e3f05c 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -66,6 +66,7 @@ repos:
rev: v2.6.0
hooks:
- id: prettier
+ exclude: \.vue$
additional_dependencies:
diff --git a/src/composables/use-browser-detection.ts b/src/composables/use-browser-detection.ts
index ca20dc894..3019e5d4c 100644
--- a/src/composables/use-browser-detection.ts
+++ b/src/composables/use-browser-detection.ts
@@ -5,7 +5,7 @@ export const useBrowserDetection = () => {
return app.$ua
}
-export const useBrowserIsMobile = () => {
+export const useBrowserIsMobile = (): boolean => {
const browser = useBrowserDetection()
if (browser === null) {
return false
diff --git a/src/composables/use-layout.ts b/src/composables/use-layout.ts
new file mode 100644
index 000000000..d090e5f49
--- /dev/null
+++ b/src/composables/use-layout.ts
@@ -0,0 +1,42 @@
+import { computed, watch } from '@nuxtjs/composition-api'
+
+import { useUiStore } from '~/stores/ui'
+import { useFeatureFlagStore } from '~/stores/feature-flag'
+import { isMinScreen } from '~/composables/use-media-query'
+
+/**
+ * This composable updates the UI store when the screen width changes or
+ * when the SSR layout settings are different from the cookie settings.
+ *
+ * The threshold for switching between mobile and desktop layout is
+ * `lg` for the `new_header` and `md` for the `old_header`.
+ */
+export function useLayout() {
+ const uiStore = useUiStore()
+ const featureFlagStore = useFeatureFlagStore()
+
+ const isNewHeaderEnabled = computed(() => featureFlagStore.isOn('new_header'))
+
+ // `isMobile` is set in the middleware for each server request.
+ const shouldPassInSSR = uiStore.isDesktopLayout
+ const desktopBreakpoint = computed(() =>
+ isNewHeaderEnabled.value ? 'lg' : 'md'
+ )
+
+ const isDesktopLayout = isMinScreen(desktopBreakpoint, { shouldPassInSSR })
+
+ watch(isDesktopLayout, (isDesktop) => {
+ updateLayout(isDesktop)
+ })
+
+ const updateLayout = (isDesktop: boolean) => {
+ if (isDesktop !== uiStore.isDesktopLayout) {
+ uiStore.updateBreakpoint(isDesktop)
+ }
+ }
+
+ return {
+ isDesktopLayout,
+ updateLayout,
+ }
+}
diff --git a/src/composables/use-media-query.ts b/src/composables/use-media-query.ts
index b47ae2002..d50a9fa18 100644
--- a/src/composables/use-media-query.ts
+++ b/src/composables/use-media-query.ts
@@ -1,12 +1,16 @@
/* this implementation is from https://github.com/vueuse/vueuse/packages/core/useMediaQuery/
which, in turn, is ported from https://github.com/logaretm/vue-use-web by Abdelrahman Awad */
-import { ref, watchEffect } from '@nuxtjs/composition-api'
+import { computed, ref, watchEffect } from '@nuxtjs/composition-api'
+
+import { resolveUnref } from '@vueuse/core'
import { SCREEN_SIZES, Breakpoint } from '~/constants/screens'
import { defaultWindow } from '~/constants/window'
import { tryOnScopeDispose } from '~/utils/try-on-scope-dispose'
import { useSupported } from '~/composables/use-supported'
+import type { MaybeComputedRef } from '@vueuse/core'
+
interface Options {
shouldPassInSSR?: boolean
window?: Window
@@ -16,7 +20,7 @@ interface Options {
* Reactive Media Query.
*/
export function useMediaQuery(
- query: string,
+ query: MaybeComputedRef<string>,
options: Options = { shouldPassInSSR: false }
) {
const { window = defaultWindow } = options
@@ -49,7 +53,7 @@ export function useMediaQuery(
cleanup()
- mediaQuery = window.matchMedia(query)
+ mediaQuery = window.matchMedia(resolveUnref(query))
matches.value = mediaQuery.matches
if ('addEventListener' in mediaQuery) {
@@ -67,20 +71,29 @@ export function useMediaQuery(
return matches
}
+const isBpXs = (bp: Breakpoint): bp is 'xs' => bp === 'xs'
+
/**
* Check whether the current screen meets
* or exceeds the provided breakpoint size.
*/
-export const isMinScreen = (breakpointName: Breakpoint, options?: Options) => {
- if (breakpointName === 'xs') {
+export const isMinScreen = (
+ breakpointName: MaybeComputedRef<Breakpoint>,
+ options?: Options
+) => {
+ const resolvedBp = resolveUnref(breakpointName)
+
+ if (isBpXs(resolvedBp)) {
// `xs` is the "minimum" so it is always true
return ref(true)
}
- return useMediaQuery(
- `(min-width: ${SCREEN_SIZES.get(breakpointName)}px)`,
- options
- )
+ const query = computed(() => {
+ const sizeInPx = SCREEN_SIZES.get(resolvedBp)
+ return `(min-width: ${sizeInPx}px)`
+ })
+
+ return useMediaQuery(query, options)
}
/**
diff --git a/src/middleware/middleware.ts b/src/middleware/middleware.ts
index 5aeb47a39..f2e9b954c 100644
--- a/src/middleware/middleware.ts
+++ b/src/middleware/middleware.ts
@@ -1,9 +1,11 @@
import { sendWindowMessage } from '~/utils/send-message'
+
import { useNavigationStore } from '~/stores/navigation'
import { useProviderStore } from '~/stores/provider'
import { useFeatureFlagStore } from '~/stores/feature-flag'
+import { useUiStore } from '~/stores/ui'
-import type { Middleware } from '@nuxt/types'
+import type { Context, Middleware } from '@nuxt/types'
/**
* In embedded mode, the app sends its url
@@ -18,7 +20,13 @@ import type { Middleware } from '@nuxt/types'
* Currently, one event type is used:
* - `urlChange` sends the relative path of the URL on every URL change.
*/
-const middleware: Middleware = async ({ app, query, route, $pinia }) => {
+const middleware: Middleware = async ({
+ $cookies,
+ $ua,
+ query,
+ route,
+ $pinia,
+}: Context) => {
/* Nav store */
const navigationStore = useNavigationStore($pinia)
@@ -44,7 +52,14 @@ const middleware: Middleware = async ({ app, query, route, $pinia }) => {
/* Feature flag store */
const featureFlagStore = useFeatureFlagStore($pinia)
- featureFlagStore.initFromCookies(app.$cookies.get('features') ?? {})
+ featureFlagStore.initFromCookies($cookies.get('features') ?? {})
featureFlagStore.initFromQuery(query)
+
+ /* UI store */
+
+ const uiStore = useUiStore($pinia)
+ const isMobileUa = $ua ? $ua.isMobile : false
+ $cookies.set('uiIsMobileUa', isMobileUa)
+ uiStore.initFromCookies($cookies.getAll() ?? {})
}
export default middleware
diff --git a/src/pages/preferences.vue b/src/pages/preferences.vue
index 285b81e71..1acb1dc1d 100644
--- a/src/pages/preferences.vue
+++ b/src/pages/preferences.vue
@@ -88,7 +88,7 @@ export default defineComponent({
VCheckbox,
},
setup() {
- const { app } = useContext()
+ const { $cookies } = useContext()
const featureFlagStore = useFeatureFlagStore()
const flags = computed(() => featureFlagStore.flags)
@@ -106,7 +106,7 @@ export default defineComponent({
checked: boolean
}) => {
featureFlagStore.toggleFeature(name, checked ? ON : OFF)
- app.$cookies.set('features', featureFlagStore.flagStateMap)
+ $cookies.set('features', featureFlagStore.flagStateMap)
}
return {
diff --git a/src/plugins/ua-parse.ts b/src/plugins/ua-parse.ts
index 91f1aab42..cbe231725 100644
--- a/src/plugins/ua-parse.ts
+++ b/src/plugins/ua-parse.ts
@@ -1,4 +1,4 @@
-import useragent from 'express-useragent'
+import useragent, { Details as UADetails } from 'express-useragent'
import type { Plugin } from '@nuxt/types'
@@ -10,7 +10,7 @@ const uaParsePlugin: Plugin = (context, inject) => {
} else if (typeof navigator !== 'undefined') {
userAgent = navigator.userAgent
}
- let ua
+ let ua: UADetails | null
if (typeof userAgent == 'string') {
ua = useragent.parse(userAgent)
} else {
diff --git a/src/stores/ui.ts b/src/stores/ui.ts
index 2e7f2f7f4..0491a1ad2 100644
--- a/src/stores/ui.ts
+++ b/src/stores/ui.ts
@@ -1,23 +1,75 @@
import { defineStore } from 'pinia'
-export type SnackbarState = 'not_shown' | 'visible' | 'dismissed'
+import type { OpenverseCookieState, SnackbarState } from '~/types/cookies'
+import { isProd } from '~/utils/node-env'
+
+import type { CookieSerializeOptions } from 'cookie'
export interface UiState {
/**
* whether to show the instructions snackbar.
*/
instructionsSnackbarState: SnackbarState
+ /**
+ * whether the filters are shown (sidebar on desktop or modal on mobile).
+ * This is the inner value, components should use the un-prefixed getter.
+ */
+ innerFilterVisible: boolean
+ /**
+ * (only for desktop layout) whether the filters were dismissed. If true,
+ * the filters should be closed on SSR for the first load on desktop layout.
+ */
+ isFilterDismissed: boolean
+ /**
+ * whether the site layout is desktop (or mobile).
+ */
+ isDesktopLayout: boolean
+ /**
+ * whether the request user agent is mobile or not.
+ */
+ isMobileUa: boolean
+}
+
+const cookieOptions: CookieSerializeOptions = {
+ path: '/',
+ sameSite: 'strict',
+ maxAge: 60 * 60 * 24 * 60, // 60 days
+ secure: isProd,
}
export const useUiStore = defineStore('ui', {
state: (): UiState => ({
instructionsSnackbarState: 'not_shown',
+ innerFilterVisible: false,
+ isFilterDismissed: false,
+ isDesktopLayout: false,
+ isMobileUa: true,
}),
getters: {
- areInstructionsVisible(state) {
+ areInstructionsVisible(state): boolean {
return state.instructionsSnackbarState === 'visible'
},
+ /**
+ * On desktop, we only hide the filters sidebar if it was
+ * specifically dismissed on the desktop layout.
+ *
+ * The filters state could diverge if the layout changed from desktop to mobile:
+ * closing the filters on mobile sets `isFilterVisible` to false, but does not
+ * save the `isFilterDismissed` because it only applies on the desktop layout.
+ *
+ * This getter correct the state if it diverged, and then returns the visibility state.
+ */
+ isFilterVisible(state: UiState): boolean {
+ if (
+ state.isDesktopLayout &&
+ !state.isFilterDismissed &&
+ !state.innerFilterVisible
+ ) {
+ state.innerFilterVisible = true
+ }
+ return state.innerFilterVisible
+ },
},
actions: {
@@ -26,8 +78,66 @@ export const useUiStore = defineStore('ui', {
this.instructionsSnackbarState = 'visible'
}
},
+
hideInstructionsSnackbar() {
this.instructionsSnackbarState = 'dismissed'
},
+ /**
+ * Given a list of key value pairs of UI state parameters and their states,
+ * populate the store state to match the cookie.
+ *
+ * @param cookies - mapping of UI state parameters and their states.
+ */
+ initFromCookies(cookies: OpenverseCookieState) {
+ this.isDesktopLayout = cookies.uiIsDesktopLayout ?? false
+ this.isFilterDismissed = cookies.uiIsFilterDismissed ?? false
+ this.isMobileUa = cookies.uiIsMobileUa ?? false
+ this.innerFilterVisible = this.isDesktopLayout
+ ? !this.isFilterDismissed
+ : false
+ },
+
+ /**
+ * If the breakpoint is different from the state, updates the state, and saves it into app cookies.
+ *
+ * @param isDesktopLayout - whether the layout is desktop (`lg` with the `new_header`
+ * and `md` with the `old_header`).
+ */
+ updateBreakpoint(isDesktopLayout: boolean) {
+ if (this.isDesktopLayout !== isDesktopLayout) {
+ this.isDesktopLayout = isDesktopLayout
+ this.$nuxt.$cookies.set(
+ 'uiIsDesktopLayout',
+ this.isDesktopLayout,
+ cookieOptions
+ )
+ }
+ },
+
+ /**
+ * Sets the filter state based on the `visible` parameter.
+ * If the filter state is changed on desktop, updates the `isFilterDismissed`
+ * 'ui' cookie value.
+ *
+ * @param visible - whether the filters should be visible.
+ */
+ setFiltersState(visible: boolean) {
+ this.innerFilterVisible = visible
+ if (this.isDesktopLayout) {
+ this.isFilterDismissed = !visible
+ this.$nuxt.$cookies.set(
+ 'uiIsFilterDismissed',
+ this.isFilterDismissed,
+ cookieOptions
+ )
+ }
+ },
+
+ /**
+ * Toggles filter state and saves the new state in a cookie.
+ */
+ toggleFilters() {
+ this.setFiltersState(!this.isFilterVisible)
+ },
},
})
diff --git a/src/types/cookies.ts b/src/types/cookies.ts
new file mode 100644
index 000000000..fd7320b50
--- /dev/null
+++ b/src/types/cookies.ts
@@ -0,0 +1,29 @@
+import type { FeatureState } from '~/constants/feature-flag'
+
+export type SnackbarState = 'not_shown' | 'visible' | 'dismissed'
+
+/**
+ * The cookies that Openverse uses to store the UI state.
+ */
+export interface OpenverseCookieState {
+ /**
+ * The state of the instructions snackbar for audio component.
+ */
+ uiInstructionsSnackbarState?: SnackbarState
+ /**
+ * Whether the filters were dismissed on desktop layout.
+ */
+ uiIsFilterDismissed?: boolean
+ /**
+ * Whether the site layout is desktop (or mobile).
+ */
+ uiIsDesktopLayout?: boolean
+ /**
+ * Whether the request user agent is mobile or not.
+ */
+ uiIsMobileUa?: boolean
+ /**
+ * The state of the feature flags.
+ */
+ features?: Record<string, FeatureState>
+}
diff --git a/test/unit/specs/components/related-audios.spec.js b/test/unit/specs/components/related-audios.spec.js
index 224ea0698..e0f5298ab 100644
--- a/test/unit/specs/components/related-audios.spec.js
+++ b/test/unit/specs/components/related-audios.spec.js
@@ -1,6 +1,8 @@
import { createLocalVue, mount } from '@vue/test-utils'
import VueI18n from 'vue-i18n'
+import { PiniaVuePlugin, createPinia } from '~~/test/unit/test-utils/pinia'
+
import VRelatedAudio from '~/components/VAudioDetails/VRelatedAudio.vue'
import render from '../../test-utils/render'
@@ -12,12 +14,16 @@ localVue.use(VueI18n)
localVue.prototype.$nuxt = {
nbFetching: 0,
}
+localVue.use(PiniaVuePlugin)
const doRender = async () => {
+ const pinia = createPinia()
+
return render(
VRelatedAudio,
{
localVue,
+ pinia,
propsData: {
media: audioResults,
fetchState: { isFetching: false, isError: false },
diff --git a/test/unit/specs/stores/ui-store.spec.js b/test/unit/specs/stores/ui-store.spec.js
new file mode 100644
index 000000000..27e0dae54
--- /dev/null
+++ b/test/unit/specs/stores/ui-store.spec.js
@@ -0,0 +1,240 @@
+import { setActivePinia, createPinia } from '~~/test/unit/test-utils/pinia'
+
+import { useUiStore } from '~/stores/ui'
+
+const initialState = {
+ instructionsSnackbarState: 'not_shown',
+ innerFilterVisible: false,
+ isFilterDismissed: false,
+ isDesktopLayout: false,
+ isMobileUa: true,
+}
+
+const VISIBLE_AND_DISMISSED = {
+ innerFilterVisible: true,
+ isFilterDismissed: true,
+}
+const NOT_VISIBLE_AND_DISMISSED = {
+ innerFilterVisible: false,
+ isFilterDismissed: true,
+}
+const VISIBLE_AND_NOT_DISMISSED = {
+ innerFilterVisible: true,
+ isFilterDismissed: false,
+}
+const NOT_VISIBLE_AND_NOT_DISMISSED = {
+ innerFilterVisible: false,
+ isFilterDismissed: false,
+}
+
+describe('Ui Store', () => {
+ beforeEach(() => {
+ setActivePinia(createPinia())
+ })
+ describe('state', () => {
+ it('sets the initial state correctly', () => {
+ const uiStore = useUiStore()
+ for (const key of Object.keys(initialState)) {
+ expect(uiStore[key]).toEqual(initialState[key])
+ }
+ })
+ })
+
+ describe('getters', () => {
+ test.each`
+ status | isVisible
+ ${'not_shown'} | ${false}
+ ${'visible'} | ${true}
+ ${'dismissed'} | ${false}
+ `(
+ 'areInstructionsVisible return $isVisible when status is $status',
+ ({ status, isVisible }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({ instructionsSnackbarState: status })
+
+ expect(uiStore.areInstructionsVisible).toEqual(isVisible)
+ }
+ )
+
+ test.each`
+ isDesktopLayout | innerFilterVisible | isFilterDismissed | isVisible
+ ${true} | ${true} | ${true} | ${true}
+ ${true} | ${true} | ${false} | ${true}
+ ${true} | ${false} | ${true} | ${false}
+ ${true} | ${false} | ${false} | ${true}
+ ${false} | ${true} | ${true} | ${true}
+ ${false} | ${true} | ${false} | ${true}
+ ${false} | ${false} | ${true} | ${false}
+ ${false} | ${false} | ${false} | ${false}
+ `(
+ 'isFilterVisible return $isVisible when isDesktopLayout is $isDesktopLayout, innerFilterVisible is $innerFilterVisible, and isFilterDismissed is $isFilterDismissed',
+ ({
+ innerFilterVisible,
+ isFilterDismissed,
+ isDesktopLayout,
+ isVisible,
+ }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({
+ isDesktopLayout,
+ innerFilterVisible,
+ isFilterDismissed,
+ })
+
+ expect(uiStore.isFilterVisible).toEqual(isVisible)
+ }
+ )
+ })
+
+ describe('actions', () => {
+ it('initFromCookies sets initial state without cookie', () => {
+ const uiStore = useUiStore()
+ uiStore.initFromCookies({})
+ for (const key of Object.keys(initialState)) {
+ // isMobileUa is set to true only if we explicitly get a mobile UA
+ // from cookie or the browser request
+ if (key === 'isMobileUa') {
+ expect(uiStore[key]).toEqual(false)
+ } else {
+ expect(uiStore[key]).toEqual(initialState[key])
+ }
+ }
+ })
+
+ it('initFromCookies sets initial state with a desktop cookie', () => {
+ const uiStore = useUiStore()
+ uiStore.initFromCookies({
+ uiIsDesktopLayout: true,
+ uiIsFilterDismissed: true,
+ })
+
+ expect(uiStore.instructionsSnackbarState).toEqual('not_shown')
+ expect(uiStore.isDesktopLayout).toEqual(true)
+ expect(uiStore.isMobileUa).toEqual(false)
+ expect(uiStore.isFilterVisible).toEqual(false)
+ expect(uiStore.isFilterDismissed).toEqual(true)
+ })
+
+ it('initFromCookies sets initial state with a mobile cookie', () => {
+ const uiStore = useUiStore()
+ uiStore.initFromCookies({
+ uiIsMobileUa: true,
+ uiIsFilterDismissed: false,
+ })
+
+ expect(uiStore.instructionsSnackbarState).toEqual('not_shown')
+ expect(uiStore.isDesktopLayout).toEqual(false)
+ expect(uiStore.isMobileUa).toEqual(true)
+ expect(uiStore.isFilterDismissed).toEqual(false)
+ expect(uiStore.isFilterVisible).toEqual(false)
+ })
+ })
+
+ test.each`
+ before | after
+ ${'not_shown'} | ${'visible'}
+ ${'visible'} | ${'visible'}
+ ${'dismissed'} | ${'dismissed'}
+ `(
+ 'showInstructionsSnackbar changes instructionsSnackbarState from $before to $after',
+ ({ before, after }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({ instructionsSnackbarState: before })
+ uiStore.showInstructionsSnackbar()
+
+ expect(uiStore.instructionsSnackbarState).toEqual(after)
+ }
+ )
+
+ test.each`
+ before | after
+ ${'not_shown'} | ${'dismissed'}
+ ${'visible'} | ${'dismissed'}
+ ${'dismissed'} | ${'dismissed'}
+ `(
+ 'hideInstructionsSnackbar changes instructionsSnackbarState from $before to $after',
+ ({ before, after }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({ instructionsSnackbarState: before })
+ uiStore.hideInstructionsSnackbar()
+
+ expect(uiStore.instructionsSnackbarState).toEqual(after)
+ }
+ )
+
+ test.each`
+ initialState | isDesktopLayout | expected
+ ${[true, false]} | ${false} | ${{ isDesktopLayout: false, isMobileUa: false }}
+ ${[false, true]} | ${true} | ${{ isDesktopLayout: true, isMobileUa: true }}
+ `(
+ 'updateBreakpoint gets isDesktopLayout $isDesktopLayout and returns $expected',
+ ({ initialState, isDesktopLayout, expected }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({
+ isDesktopLayout: initialState[0],
+ isMobileUa: initialState[1],
+ })
+ uiStore.updateBreakpoint(isDesktopLayout)
+ const actualOutput = {
+ isDesktopLayout: uiStore.isDesktopLayout,
+ isMobileUa: uiStore.isMobileUa,
+ }
+
+ expect(actualOutput).toEqual(expected)
+ }
+ )
+
+ test.each`
+ isDesktopLayout | currentState | visible | expectedState
+ ${true} | ${VISIBLE_AND_NOT_DISMISSED} | ${true} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${true} | ${VISIBLE_AND_DISMISSED} | ${false} | ${NOT_VISIBLE_AND_DISMISSED}
+ ${true} | ${NOT_VISIBLE_AND_DISMISSED} | ${true} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${true} | ${NOT_VISIBLE_AND_DISMISSED} | ${false} | ${NOT_VISIBLE_AND_DISMISSED}
+ ${false} | ${VISIBLE_AND_NOT_DISMISSED} | ${true} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${VISIBLE_AND_NOT_DISMISSED} | ${false} | ${NOT_VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_NOT_DISMISSED} | ${true} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_DISMISSED} | ${true} | ${VISIBLE_AND_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_DISMISSED} | ${false} | ${NOT_VISIBLE_AND_DISMISSED}
+ `(
+ 'setFiltersState sets state to $expectedState when visible is $visible and isDesktopLayout is $isDesktopLayout',
+ ({ isDesktopLayout, currentState, visible, expectedState }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({
+ isDesktopLayout,
+ ...currentState,
+ })
+
+ uiStore.setFiltersState(visible)
+
+ expect(uiStore.isFilterVisible).toEqual(expectedState.innerFilterVisible)
+ expect(uiStore.isFilterDismissed).toEqual(expectedState.isFilterDismissed)
+ }
+ )
+
+ test.each`
+ isDesktopLayout | currentState | expectedState
+ ${true} | ${VISIBLE_AND_NOT_DISMISSED} | ${NOT_VISIBLE_AND_DISMISSED}
+ ${true} | ${VISIBLE_AND_DISMISSED} | ${NOT_VISIBLE_AND_DISMISSED}
+ ${true} | ${NOT_VISIBLE_AND_DISMISSED} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${true} | ${NOT_VISIBLE_AND_DISMISSED} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${VISIBLE_AND_NOT_DISMISSED} | ${NOT_VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_NOT_DISMISSED} | ${VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_DISMISSED} | ${VISIBLE_AND_DISMISSED}
+ ${false} | ${VISIBLE_AND_NOT_DISMISSED} | ${NOT_VISIBLE_AND_NOT_DISMISSED}
+ ${false} | ${NOT_VISIBLE_AND_DISMISSED} | ${VISIBLE_AND_DISMISSED}
+ `(
+ 'toggleFilters sets state to $expectedState when isDesktopLayout is $isDesktopLayout',
+ ({ isDesktopLayout, currentState, expectedState }) => {
+ const uiStore = useUiStore()
+ uiStore.$patch({
+ isDesktopLayout,
+ ...currentState,
+ })
+
+ uiStore.toggleFilters()
+
+ expect(uiStore.isFilterVisible).toEqual(expectedState.innerFilterVisible)
+ expect(uiStore.isFilterDismissed).toEqual(expectedState.isFilterDismissed)
+ }
+ )
+})
diff --git a/test/unit/test-utils/pinia.js b/test/unit/test-utils/pinia.js
index 0f46d3abf..af72ad554 100644
--- a/test/unit/test-utils/pinia.js
+++ b/test/unit/test-utils/pinia.js
@@ -9,6 +9,10 @@ export const createPinia = () =>
captureException: jest.fn(),
captureEvent: jest.fn(),
},
+ $cookies: {
+ set: jest.fn(),
+ get: jest.fn(),
+ },
},
}))
diff --git a/typings/nuxt__types/index.d.ts b/typings/nuxt__types/index.d.ts
index 80b89a595..8f942f981 100644
--- a/typings/nuxt__types/index.d.ts
+++ b/typings/nuxt__types/index.d.ts
@@ -1,13 +1,44 @@
import '@nuxt/types'
import '@nuxtjs/i18n'
+import { CookieSerializeOptions } from 'cookie'
+
+import type { OpenverseCookieState } from '~/types/cookies'
+
import type { Details as UADetails } from 'express-useragent'
+export interface SetParams<Key extends keyof OpenverseCookieState> {
+ name: Key
+ value: OpenverseCookieState[Key]
+ opts?: CookieSerializeOptions
+}
+
+export interface NuxtCookies {
+ set: <Key extends keyof OpenverseCookieState>(
+ name: Key,
+ value: OpenverseCookieState[Key],
+ opts?: CookieSerializeOptions
+ ) => void
+ setAll: (cookieArray: SetParams[]) => void
+ get: <Key extends keyof OpenverseCookieState>(
+ name: Key,
+ opts?: GetOptions
+ ) => OpenverseCookieState[Key]
+ getAll: (opts?: GetOptions) => OpenverseCookieState
+ remove: <Key extends keyof OpenverseCookieState>(
+ name: Key,
+ opts?: CookieSerializeOptions
+ ) => void
+ removeAll: () => void
+}
+
declare module '@nuxt/types' {
export interface Context {
$ua: UADetails | null
+ $cookies: NuxtCookies
}
export interface NuxtAppOptions {
$ua: UADetails | null
+ $cookies: NuxtCookies
}
}