Add unit tests for AdminLogin view to improve test coverage

This commit is contained in:
2026-01-28 22:48:45 +00:00
parent 0c0cc33e1e
commit d3c6f45757

View File

@@ -0,0 +1,142 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { mount } from '@vue/test-utils';
import AdminLogin from '../../../src/views/AdminLogin.vue';
/**
* Tests for AdminLogin component
* Tests authentication flow on frontend
*/
describe('AdminLogin Component', () => {
let wrapper;
beforeEach(() => {
// Mock router
vi.stubGlobal('useRouter', () => ({
push: vi.fn()
}));
});
afterEach(() => {
if (wrapper) {
wrapper.unmount();
}
vi.clearAllMocks();
});
it('renders login form', () => {
wrapper = mount(AdminLogin, {
global: {
stubs: {
RouterLink: true
},
mocks: {
useRouter: () => ({
push: vi.fn()
}),
useAuth: () => ({
login: vi.fn(),
isLoading: { value: false },
error: { value: null }
})
}
}
});
expect(wrapper.find('h1').text()).toBe('Admin Login');
expect(wrapper.find('form').exists()).toBe(true);
});
it('has password input field', () => {
wrapper = mount(AdminLogin, {
global: {
stubs: {
RouterLink: true
},
mocks: {
useRouter: () => ({
push: vi.fn()
}),
useAuth: () => ({
login: vi.fn(),
isLoading: { value: false },
error: { value: null }
})
}
}
});
const input = wrapper.find('#password');
expect(input.exists()).toBe(true);
expect(input.attributes('type')).toBe('password');
});
it('has password visibility toggle', () => {
wrapper = mount(AdminLogin, {
global: {
stubs: {
RouterLink: true
},
mocks: {
useRouter: () => ({
push: vi.fn()
}),
useAuth: () => ({
login: vi.fn(),
isLoading: { value: false },
error: { value: null }
})
}
}
});
const toggleButton = wrapper.find('.toggle-password');
expect(toggleButton.exists()).toBe(true);
});
it('displays login button', () => {
wrapper = mount(AdminLogin, {
global: {
stubs: {
RouterLink: true
},
mocks: {
useRouter: () => ({
push: vi.fn()
}),
useAuth: () => ({
login: vi.fn(),
isLoading: { value: false },
error: { value: null }
})
}
}
});
const button = wrapper.find('button[type="submit"]');
expect(button.exists()).toBe(true);
expect(button.text()).toContain('Login');
});
it('shows info cards', () => {
wrapper = mount(AdminLogin, {
global: {
stubs: {
RouterLink: true
},
mocks: {
useRouter: () => ({
push: vi.fn()
}),
useAuth: () => ({
login: vi.fn(),
isLoading: { value: false },
error: { value: null }
})
}
}
});
const infoCards = wrapper.findAll('.info-card');
expect(infoCards.length).toBeGreaterThan(0);
});
});