This commit is contained in:
Dmitry 2025-12-03 04:40:47 +03:00
parent 24bf36f431
commit e7ea64dc36
3 changed files with 37 additions and 29 deletions

2
.gitignore vendored
View file

@ -42,3 +42,5 @@ app.*.map.json
/android/app/debug /android/app/debug
/android/app/profile /android/app/profile
/android/app/release /android/app/release
.isar

View file

@ -1,8 +1,8 @@
import { describe, it, expect, vi, beforeEach } from 'vitest' import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { MockedFunction } from 'vitest'
import { authApi } from './auth' import { authApi } from './auth'
import { adminApiClient } from './client'
// Mock the adminApiClient // Mock the adminApiClient module
vi.mock('./client', () => ({ vi.mock('./client', () => ({
adminApiClient: { adminApiClient: {
post: vi.fn(), post: vi.fn(),
@ -10,7 +10,13 @@ vi.mock('./client', () => ({
}, },
})) }))
const mockedAdminApiClient = vi.mocked(adminApiClient) // Import the mocked module
import { adminApiClient } from './client'
import type { AxiosResponse } from 'axios'
// Type the mocked functions
const mockPost = adminApiClient.post as MockedFunction<typeof adminApiClient.post>
const mockGet = adminApiClient.get as MockedFunction<typeof adminApiClient.get>
describe('authApi', () => { describe('authApi', () => {
beforeEach(() => { beforeEach(() => {
@ -21,18 +27,18 @@ describe('authApi', () => {
it('should call adminApiClient.post with correct endpoint', async () => { it('should call adminApiClient.post with correct endpoint', async () => {
const mockResponse = { const mockResponse = {
data: { success: true, message: 'Code sent' }, data: { success: true, message: 'Code sent' },
} } as AxiosResponse
mockedAdminApiClient.post.mockResolvedValue(mockResponse) mockPost.mockResolvedValue(mockResponse)
const result = await authApi.requestCode() const result = await authApi.requestCode()
expect(mockedAdminApiClient.post).toHaveBeenCalledWith('/api/v2/admin/auth/request-code') expect(mockPost).toHaveBeenCalledWith('/api/v2/admin/auth/request-code')
expect(result).toEqual({ success: true, message: 'Code sent' }) expect(result).toEqual({ success: true, message: 'Code sent' })
}) })
it('should handle API errors', async () => { it('should handle API errors', async () => {
const error = new Error('Network error') const error = new Error('Network error')
mockedAdminApiClient.post.mockRejectedValue(error) mockPost.mockRejectedValue(error)
await expect(authApi.requestCode()).rejects.toThrow('Network error') await expect(authApi.requestCode()).rejects.toThrow('Network error')
}) })
@ -46,12 +52,12 @@ describe('authApi', () => {
token: 'jwt-token', token: 'jwt-token',
user: { id: 1, name: 'Admin', admin: true }, user: { id: 1, name: 'Admin', admin: true },
}, },
} } as AxiosResponse
mockedAdminApiClient.post.mockResolvedValue(mockResponse) mockPost.mockResolvedValue(mockResponse)
const result = await authApi.verifyCode('123456') const result = await authApi.verifyCode('123456')
expect(mockedAdminApiClient.post).toHaveBeenCalledWith('/api/v2/admin/auth/verify-code', { expect(mockPost).toHaveBeenCalledWith('/api/v2/admin/auth/verify-code', {
code: '123456', code: '123456',
}) })
expect(result).toEqual(mockResponse.data) expect(result).toEqual(mockResponse.data)
@ -60,8 +66,8 @@ describe('authApi', () => {
it('should handle invalid code response', async () => { it('should handle invalid code response', async () => {
const mockResponse = { const mockResponse = {
data: { success: false, message: 'Invalid code' }, data: { success: false, message: 'Invalid code' },
} } as AxiosResponse
mockedAdminApiClient.post.mockResolvedValue(mockResponse) mockPost.mockResolvedValue(mockResponse)
const result = await authApi.verifyCode('invalid') const result = await authApi.verifyCode('invalid')
@ -76,20 +82,20 @@ describe('authApi', () => {
success: true, success: true,
user: { id: 1, name: 'Admin', admin: true }, user: { id: 1, name: 'Admin', admin: true },
}, },
} } as AxiosResponse
mockedAdminApiClient.get.mockResolvedValue(mockResponse) mockGet.mockResolvedValue(mockResponse)
const result = await authApi.getCurrentUser() const result = await authApi.getCurrentUser()
expect(mockedAdminApiClient.get).toHaveBeenCalledWith('/api/v2/admin/auth/me') expect(mockGet).toHaveBeenCalledWith('/api/v2/admin/auth/me')
expect(result).toEqual(mockResponse.data) expect(result).toEqual(mockResponse.data)
}) })
it('should handle unauthorized access', async () => { it('should handle unauthorized access', async () => {
const mockResponse = { const mockResponse = {
data: { success: false, message: 'Not authenticated' }, data: { success: false, message: 'Not authenticated' },
} } as AxiosResponse
mockedAdminApiClient.get.mockResolvedValue(mockResponse) mockGet.mockResolvedValue(mockResponse)
const result = await authApi.getCurrentUser() const result = await authApi.getCurrentUser()

View file

@ -74,9 +74,12 @@ describe('API Clients', () => {
describe('Error handling', () => { describe('Error handling', () => {
it('should handle 401 errors by clearing token and redirecting', async () => { it('should handle 401 errors by clearing token and redirecting', async () => {
const originalLocation = window.location // Mock window.location
delete (window as any).location const mockLocation = { href: '' }
window.location = { href: '' } as any Object.defineProperty(window, 'location', {
value: mockLocation,
writable: true,
})
const handleAuthError = (error: any) => { const handleAuthError = (error: any) => {
if (error.response?.status === 401) { if (error.response?.status === 401) {
@ -95,15 +98,15 @@ describe('API Clients', () => {
expect(localStorage.getItem('admin_token')).toBeNull() expect(localStorage.getItem('admin_token')).toBeNull()
expect(window.location.href).toBe('/login') expect(window.location.href).toBe('/login')
// Restore original location
window.location = originalLocation
}) })
it('should not redirect for non-401 errors', async () => { it('should not redirect for non-401 errors', async () => {
const originalLocation = window.location // Mock window.location
delete (window as any).location const mockLocation = { href: '' }
window.location = { href: '' } as any Object.defineProperty(window, 'location', {
value: mockLocation,
writable: true,
})
const handleAuthError = (error: any) => { const handleAuthError = (error: any) => {
if (error.response?.status === 401) { if (error.response?.status === 401) {
@ -119,9 +122,6 @@ describe('API Clients', () => {
expect(localStorage.getItem('admin_token')).toBeNull() // Should be cleared from beforeEach expect(localStorage.getItem('admin_token')).toBeNull() // Should be cleared from beforeEach
expect(window.location.href).toBe('') expect(window.location.href).toBe('')
// Restore original location
window.location = originalLocation
}) })
}) })
}) })