Beautiful Open-Source Form Builder
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import TwoFactorVerificationModal from '~/components/pages/auth/TwoFactorVerificationModal.vue'
// Mock the API
vi.mock('~/api/auth', () => ({
authApi: {
twoFactor: {
verify: vi.fn(),
},
},
}))
describe('TwoFactorVerificationModal', () => {
let authApi
beforeEach(async () => {
// Import mocked API
const apiModule = await import('~/api/auth')
authApi = apiModule.authApi
// Reset mocks
vi.clearAllMocks()
})
const createWrapper = (props = {}) => {
return mount(TwoFactorVerificationModal, {
props: {
show: true,
pendingAuthToken: 'test-token-123',
...props,
},
global: {
stubs: {
UModal: {
template: '<div class="modal"><slot name="header" /><slot name="body" /><slot name="footer" /></div>',
props: ['open', 'dismissible'],
},
UPinInput: {
template: '<input type="text" data-testid="pin-input" />',
props: ['modelValue', 'length', 'type', 'otp', 'autofocus', 'size', 'highlight'],
emits: ['update:modelValue', 'complete'],
},
UAlert: {
template: '<div class="alert" v-if="description">{{ description }}</div>',
props: ['color', 'variant', 'description'],
},
UButton: {
template: '<button><slot /></button>',
props: ['variant', 'size', 'color', 'block', 'loading'],
emits: ['click'],
},
TextInput: {
template: '<input data-testid="recovery-input" />',
props: ['modelValue', 'label', 'placeholder'],
emits: ['update:modelValue', 'keyup.enter'],
},
VTransition: {
template: '<div><slot /></div>',
props: ['name'],
},
},
},
})
}
describe('Rendering', () => {
it('should render modal when show is true and pendingAuthToken exists', () => {
const wrapper = createWrapper()
expect(wrapper.exists()).toBe(true)
expect(wrapper.find('.modal').exists()).toBe(true)
})
it('should render modal when show is false', () => {
const wrapper = createWrapper({ show: false })
expect(wrapper.exists()).toBe(true)
})
it('should render modal when pendingAuthToken is missing', () => {
const wrapper = createWrapper({ pendingAuthToken: null })
expect(wrapper.exists()).toBe(true)
})
})
describe('Code Verification', () => {
it('should call verify API when handleComplete is called with valid code', async () => {
const wrapper = createWrapper()
authApi.twoFactor.verify.mockResolvedValue({
token: 'verified-token',
user: { id: 1 },
})
// Call the method directly
await wrapper.vm.handleComplete(['1', '2', '3', '4', '5', '6'])
await wrapper.vm.$nextTick()
await new Promise(resolve => setTimeout(resolve, 100))
expect(authApi.twoFactor.verify).toHaveBeenCalledWith({
pending_auth_token: 'test-token-123',
code: '123456',
})
})
it('should not verify if code is invalid', async () => {
const wrapper = createWrapper()
// Try with invalid code (not 6 digits)
await wrapper.vm.handleComplete(['1', '2', '3'])
expect(authApi.twoFactor.verify).not.toHaveBeenCalled()
})
it('should emit verified event on successful verification', async () => {
const wrapper = createWrapper()
const response = {
token: 'verified-token',
user: { id: 1 },
}
authApi.twoFactor.verify.mockResolvedValue(response)
await wrapper.vm.handleComplete(['1', '2', '3', '4', '5', '6'])
await wrapper.vm.$nextTick()
await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.emitted('verified')).toBeTruthy()
expect(wrapper.emitted('verified')[0]).toEqual([response])
})
})
describe('Error Handling', () => {
it('should handle API errors with response data', async () => {
const wrapper = createWrapper()
authApi.twoFactor.verify.mockRejectedValue({
response: {
_data: {
message: 'Invalid code',
},
},
})
await wrapper.vm.handleComplete(['1', '2', '3', '4', '5', '6'])
await wrapper.vm.$nextTick()
await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.vm.error).toBe('Invalid code')
})
it('should handle network errors gracefully', async () => {
const wrapper = createWrapper()
authApi.twoFactor.verify.mockRejectedValue(new Error('Network error'))
await wrapper.vm.handleComplete(['1', '2', '3', '4', '5', '6'])
await wrapper.vm.$nextTick()
await new Promise(resolve => setTimeout(resolve, 100))
expect(wrapper.vm.error).toContain('Unable to verify code')
})
it('should show error when pendingAuthToken is missing', async () => {
const wrapper = createWrapper({ pendingAuthToken: null })
await wrapper.vm.verifyCode('123456')
expect(wrapper.vm.error).toContain('Authentication session expired')
expect(authApi.twoFactor.verify).not.toHaveBeenCalled()
})
})
describe('Recovery Code', () => {
it('should verify recovery code using verifyCode method', async () => {
const wrapper = createWrapper()
authApi.twoFactor.verify.mockResolvedValue({
token: 'verified-token',
user: { id: 1 },
})
// Call verifyCode directly with recovery code (this is what verifyRecoveryCode does internally)
// verifyCode validates the code format, so we need a valid format
await wrapper.vm.verifyCode('123456')
await wrapper.vm.$nextTick()
await new Promise(resolve => setTimeout(resolve, 100))
// Should call verify
expect(authApi.twoFactor.verify).toHaveBeenCalled()
})
})
describe('Cancel', () => {
it('should emit cancel event when handleCancel is called', () => {
const wrapper = createWrapper()
wrapper.vm.handleCancel()
expect(wrapper.emitted('cancel')).toBeTruthy()
})
it('should clear state on cancel', () => {
const wrapper = createWrapper()
// Set some state
wrapper.vm.code = ['1', '2', '3']
wrapper.vm.recoveryCode = 'test'
wrapper.vm.showRecoveryCode = true
wrapper.vm.handleCancel()
expect(wrapper.vm.code).toEqual([])
expect(wrapper.vm.recoveryCode).toBe('')
expect(wrapper.vm.showRecoveryCode).toBe(false)
})
})
describe('State Management', () => {
it('should reset state when modal opens', async () => {
const wrapper = createWrapper({ show: false })
// Set some state
wrapper.vm.error = 'Some error'
wrapper.vm.code = ['1', '2', '3']
// Open modal
await wrapper.setProps({ show: true })
await wrapper.vm.$nextTick()
// State should be reset (watch handler runs)
// Note: watch runs on next tick, so we need to wait
await new Promise(resolve => setTimeout(resolve, 50))
expect(wrapper.vm.error).toBe(null)
expect(wrapper.vm.code).toEqual([])
})
})
})