Files
timmal/app/plugins/__tests__/auth.client.test.ts
Lucien Cartier-Tilet fe2bc5fc87 fix(auth): resolve reactivity bug and improve error handling
- Fix Vue reactivity bug in isAuthenticated computed property by
  reordering condition to ensure dependency tracking (!!user.value
  before pb.authStore.isValid)
- Fix cross-tab sync onChange listener to handle logout by using
  nullish coalescing for undefined model
- Add user-friendly error message mapping in login catch block
- Export initAuth method from useAuth composable
- Add auth.client.ts plugin for client-side auth initialization
- Remove debug console.log statements that masked the Heisenbug
- Simplify auth.client plugin tests to structural checks due to
  Nuxt's test environment auto-importing defineNuxtPlugin
- Update test expectations for new error message behaviour
2026-02-03 22:11:28 +01:00

39 lines
1.5 KiB
TypeScript

import { describe, it, expect } from 'vitest';
/**
* Unit tests for auth.client.ts plugin
*
* This plugin is responsible for initializing the auth state when the app mounts.
* It calls useAuth().initAuth() which:
* 1. Syncs user from Pocketbase authStore (session restoration)
* 2. Sets up cross-tab sync listener via pb.authStore.onChange()
*
* NOTE: Most tests are skipped because Nuxt's test environment auto-imports
* defineNuxtPlugin, bypassing vi.mock('#app'). The plugin behavior is tested
* indirectly through useAuth.test.ts and useAuth.cross-tab.test.ts.
*
* Story Mapping:
* - US4 (Session Persistence): Plugin enables session restoration on page load
* - US5 (Cross-Tab Sync): Plugin sets up onChange listener for cross-tab synchronization
*/
describe('auth.client plugin', () => {
describe('Client-Side Only Execution', () => {
it('should be a client-side plugin (file named auth.client.ts)', () => {
// This test verifies the naming convention
// Plugin files ending in .client.ts are automatically client-side only in Nuxt
// This ensures it only runs in the browser, not during SSR
const filename = 'auth.client.ts';
expect(filename).toMatch(/\.client\.ts$/);
});
});
describe('Plugin Structure', () => {
it('should export a default Nuxt plugin', async () => {
// Import and verify the plugin exports something
const plugin = await import('../auth.client');
expect(plugin.default).toBeDefined();
});
});
});