Files

86 lines
2.4 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
const mockPageProps = {
auth: { user: null as any },
flash: { success: null, error: null, message: null, status: null },
authConfig: {
appName: 'TestApp',
features: {
registration: true,
password_reset: true,
remember_me: true,
email_verification: false,
},
login: { title: 'Login', description: '', icon: '', submit_label: 'Sign in' },
register: { title: 'Register', description: '', icon: '', submit_label: 'Sign up' },
forgotPassword: { title: 'Forgot', description: '', icon: '', submit_label: 'Send' },
resetPassword: { title: 'Reset', description: '', icon: '', submit_label: 'Reset' },
providers: [],
legal: { terms_url: null, privacy_url: null, show_in_register: false },
},
}
vi.mock('@inertiajs/vue3', () => ({
usePage: () => ({ props: mockPageProps }),
}))
const { useAuth } = await import('../useAuth')
describe('useAuth', () => {
it('returns null user when not authenticated', () => {
mockPageProps.auth.user = null
const { user, isAuthenticated } = useAuth()
expect(user.value).toBeNull()
expect(isAuthenticated.value).toBe(false)
})
it('returns user when authenticated', () => {
mockPageProps.auth.user = {
id: 1,
username: 'testuser',
first_name: 'Test',
last_name: 'User',
full_name: 'Test User',
email: 'test@example.com',
email_verified_at: '2026-01-01',
created_at: '2026-01-01',
updated_at: '2026-01-01',
}
const { user, isAuthenticated } = useAuth()
expect(user.value).not.toBeNull()
expect(user.value!.username).toBe('testuser')
expect(isAuthenticated.value).toBe(true)
})
it('returns flash messages', () => {
mockPageProps.flash.success = 'It worked!'
const { flash } = useAuth()
expect(flash.value.success).toBe('It worked!')
})
it('returns auth config', () => {
const { config } = useAuth()
expect(config.value.appName).toBe('TestApp')
expect(config.value.features.registration).toBe(true)
expect(config.value.features.email_verification).toBe(false)
})
it('returns providers from config', () => {
mockPageProps.authConfig.providers = [
{ key: 'github', label: 'GitHub', icon: 'i-simple-icons-github' },
]
const { config } = useAuth()
expect(config.value.providers).toHaveLength(1)
expect(config.value.providers[0].key).toBe('github')
})
})