auth-store.spec.ts
2,585 bytes
| 1 | import { describe, it, expect, beforeEach } from 'vitest' |
|---|---|
| 2 | import { setActivePinia, createPinia } from 'pinia' |
| 3 | import { nextTick } from 'vue' |
| 4 | import { useAuthStore } from '@/stores/auth' |
| 5 | |
| 6 | describe('auth store', () => { |
| 7 | beforeEach(() => { |
| 8 | // Fresh Pinia + clean localStorage for each test. |
| 9 | // (vitest.setup.ts already clears localStorage, but we also need a fresh store.) |
| 10 | setActivePinia(createPinia()) |
| 11 | }) |
| 12 | |
| 13 | it('starts empty when localStorage has nothing', () => { |
| 14 | const store = useAuthStore() |
| 15 | expect(store.jwt).toBeNull() |
| 16 | expect(store.refreshToken).toBeNull() |
| 17 | expect(store.userName).toBeNull() |
| 18 | expect(store.isAuthenticated).toBe(false) |
| 19 | }) |
| 20 | |
| 21 | it('hydrates from localStorage on creation', () => { |
| 22 | localStorage.setItem('jwt', 'pre-existing-jwt') |
| 23 | localStorage.setItem('refreshToken', 'pre-existing-refresh') |
| 24 | localStorage.setItem('userName', 'Alice Alpha') |
| 25 | |
| 26 | const store = useAuthStore() |
| 27 | expect(store.jwt).toBe('pre-existing-jwt') |
| 28 | expect(store.refreshToken).toBe('pre-existing-refresh') |
| 29 | expect(store.userName).toBe('Alice Alpha') |
| 30 | expect(store.isAuthenticated).toBe(true) |
| 31 | }) |
| 32 | |
| 33 | it('isAuthenticated reflects whether jwt is present', () => { |
| 34 | const store = useAuthStore() |
| 35 | expect(store.isAuthenticated).toBe(false) |
| 36 | |
| 37 | store.jwt = 'new-jwt' |
| 38 | expect(store.isAuthenticated).toBe(true) |
| 39 | |
| 40 | store.jwt = null |
| 41 | expect(store.isAuthenticated).toBe(false) |
| 42 | }) |
| 43 | |
| 44 | it('syncs jwt changes to localStorage', async () => { |
| 45 | const store = useAuthStore() |
| 46 | store.jwt = 'token-abc' |
| 47 | await nextTick() |
| 48 | expect(localStorage.getItem('jwt')).toBe('token-abc') |
| 49 | |
| 50 | store.jwt = null |
| 51 | await nextTick() |
| 52 | expect(localStorage.getItem('jwt')).toBeNull() |
| 53 | }) |
| 54 | |
| 55 | it('syncs refreshToken and userName changes to localStorage', async () => { |
| 56 | const store = useAuthStore() |
| 57 | store.refreshToken = 'r1' |
| 58 | store.userName = 'Bob Beta' |
| 59 | await nextTick() |
| 60 | expect(localStorage.getItem('refreshToken')).toBe('r1') |
| 61 | expect(localStorage.getItem('userName')).toBe('Bob Beta') |
| 62 | }) |
| 63 | |
| 64 | it('logout() clears all three fields', async () => { |
| 65 | const store = useAuthStore() |
| 66 | store.jwt = 'j' |
| 67 | store.refreshToken = 'r' |
| 68 | store.userName = 'u' |
| 69 | |
| 70 | store.logout() |
| 71 | await nextTick() |
| 72 | |
| 73 | expect(store.jwt).toBeNull() |
| 74 | expect(store.refreshToken).toBeNull() |
| 75 | expect(store.userName).toBeNull() |
| 76 | expect(store.isAuthenticated).toBe(false) |
| 77 | expect(localStorage.getItem('jwt')).toBeNull() |
| 78 | expect(localStorage.getItem('refreshToken')).toBeNull() |
| 79 | expect(localStorage.getItem('userName')).toBeNull() |
| 80 | }) |
| 81 | }) |
| 82 | |