From 0ed29a198df43b672c6f731239628bdaf9b0ee40 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Thu, 24 Apr 2025 17:36:16 -0400 Subject: [PATCH] [Cleanup] Remove unnecessary null check on auth (#3610) --- .../error/__tests__/ReportIssuePanel.spec.ts | 8 ++++++ src/stores/firebaseAuthStore.ts | 27 +++++++++---------- .../tests/store/firebaseAuthStore.test.ts | 17 ------------ 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/components/dialog/content/error/__tests__/ReportIssuePanel.spec.ts b/src/components/dialog/content/error/__tests__/ReportIssuePanel.spec.ts index fe56fec69..7478a0480 100644 --- a/src/components/dialog/content/error/__tests__/ReportIssuePanel.spec.ts +++ b/src/components/dialog/content/error/__tests__/ReportIssuePanel.spec.ts @@ -142,6 +142,14 @@ vi.mock('@primevue/forms', () => ({ } })) +vi.mock('@/stores/firebaseAuthStore', () => ({ + useFirebaseAuthStore: () => ({ + currentUser: { + email: 'test@example.com' + } + }) +})) + describe('ReportIssuePanel', () => { beforeEach(() => { vi.clearAllMocks() diff --git a/src/stores/firebaseAuthStore.ts b/src/stores/firebaseAuthStore.ts index 138b9ab5b..9476fd71f 100644 --- a/src/stores/firebaseAuthStore.ts +++ b/src/stores/firebaseAuthStore.ts @@ -59,22 +59,21 @@ export const useFirebaseAuthStore = defineStore('firebaseAuth', () => { const userId = computed(() => currentUser.value?.uid) // Get auth from VueFire and listen for auth state changes - const auth = useFirebaseAuth() - if (auth) { - // Set persistence to localStorage (works in both browser and Electron) - void setPersistence(auth, browserLocalPersistence) + // From useFirebaseAuth docs: + // Retrieves the Firebase Auth instance. Returns `null` on the server. + // When using this function on the client in TypeScript, you can force the type with `useFirebaseAuth()!`. + const auth = useFirebaseAuth()! + // Set persistence to localStorage (works in both browser and Electron) + void setPersistence(auth, browserLocalPersistence) - onAuthStateChanged(auth, (user) => { - currentUser.value = user - isInitialized.value = true + onAuthStateChanged(auth, (user) => { + currentUser.value = user + isInitialized.value = true - // Reset balance when auth state changes - balance.value = null - lastBalanceUpdateTime.value = null - }) - } else { - error.value = 'Firebase Auth not available from VueFire' - } + // Reset balance when auth state changes + balance.value = null + lastBalanceUpdateTime.value = null + }) const showAuthErrorToast = () => { useToastStore().add({ diff --git a/tests-ui/tests/store/firebaseAuthStore.test.ts b/tests-ui/tests/store/firebaseAuthStore.test.ts index 8dd4c0274..6933dfea9 100644 --- a/tests-ui/tests/store/firebaseAuthStore.test.ts +++ b/tests-ui/tests/store/firebaseAuthStore.test.ts @@ -171,23 +171,6 @@ describe('useFirebaseAuthStore', () => { expect(store.error).toBe(null) }) - it('should handle auth initialization failure', async () => { - // Mock auth as null to simulate initialization failure - vi.mocked(vuefire.useFirebaseAuth).mockReturnValue(null) - - // Create a new store instance - setActivePinia(createPinia()) - const uninitializedStore = useFirebaseAuthStore() - - // Check that isInitialized is false - expect(uninitializedStore.isInitialized).toBe(false) - - // Verify store actions throw appropriate errors - await expect( - uninitializedStore.login('test@example.com', 'password') - ).rejects.toThrow('Firebase Auth not initialized') - }) - describe('login', () => { it('should login with valid credentials', async () => { const mockUserCredential = { user: mockUser }