Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not be able to test persist function in testing #378

Open
3 tasks done
AeliGo opened this issue Feb 5, 2025 · 0 comments
Open
3 tasks done

Not be able to test persist function in testing #378

AeliGo opened this issue Feb 5, 2025 · 0 comments
Labels
✨ enhancement New feature or request

Comments

@AeliGo
Copy link

AeliGo commented Feb 5, 2025

Clear and concise description of the problem

import { beforeEach, describe, expect, it, vi } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useThemeStore } from '@/stores/themeStore'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'

describe('useThemeStore', () => {
  const setItemFn = vi.fn()
  const getItemFn = vi.fn()

  beforeEach(() => {
    vi.stubGlobal('localStorage', {
      getItem: getItemFn,
      setItem: setItemFn,
      clear: vi.fn(),
    })

    const pinia = createPinia()
    pinia.use(piniaPluginPersistedstate)
    setActivePinia(pinia)
  })

  it('should have "light" as the default theme', () => {
    const store = useThemeStore()
    expect(store.currentTheme).toBe('light')
  })

  it('should change the theme when setTheme is called', () => {
    const store = useThemeStore()
    store.setTheme('dark')
    expect(store.currentTheme).toBe('dark')
  })

  it.skip('should persist the theme in local storage', async () => {
    const store = useThemeStore()
    store.setTheme('dark')
    expect(setItemFn).toHaveBeenCalledWith(
      'theme',
      JSON.stringify({ currentTheme: 'dark' }),
    )
  })
})

localStorage.setItem not be called

Suggested solution

Please provide a way to test persist function

Alternative

No response

Additional context

No response

Validations

@AeliGo AeliGo added the ✨ enhancement New feature or request label Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant