initialize project with Vue 3, Vite, and Element Plus; set up basic routing, components, configuration, and project structure
This commit is contained in:
86
tests/session-sidebar.spec.js
Normal file
86
tests/session-sidebar.spec.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { mount, flushPromises } from '@vue/test-utils'
|
||||
import { createRouter, createMemoryHistory } from 'vue-router'
|
||||
import ElementPlus from 'element-plus'
|
||||
import SessionSidebar from '../src/components/SessionSidebar.vue'
|
||||
import LoginView from '../src/views/LoginView.vue'
|
||||
import axios from 'axios'
|
||||
|
||||
vi.mock('axios', () => ({
|
||||
default: {
|
||||
get: vi.fn(),
|
||||
},
|
||||
}))
|
||||
|
||||
const mockedAxios = axios
|
||||
|
||||
const makeRouter = () =>
|
||||
createRouter({
|
||||
history: createMemoryHistory(),
|
||||
routes: [
|
||||
{ path: '/', name: 'login', component: LoginView },
|
||||
{ path: '/welcome', name: 'welcome', component: LoginView },
|
||||
],
|
||||
})
|
||||
|
||||
describe('SessionSidebar', () => {
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('redirects to login without token', async () => {
|
||||
const router = makeRouter()
|
||||
router.push('/welcome')
|
||||
await router.isReady()
|
||||
|
||||
mount(SessionSidebar, {
|
||||
global: {
|
||||
plugins: [router, ElementPlus],
|
||||
},
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
expect(router.currentRoute.value.name).toBe('login')
|
||||
})
|
||||
|
||||
it('renders sessions and selects one', async () => {
|
||||
localStorage.setItem('ars-token', 'jwt-token')
|
||||
mockedAxios.get.mockResolvedValue({
|
||||
data: {
|
||||
data: [
|
||||
{
|
||||
session_id: 's1',
|
||||
session_name: 'Demo',
|
||||
status: 'OPEN',
|
||||
last_message_preview: 'Hi there',
|
||||
last_seq: 3,
|
||||
updated_at: '2025-02-14T10:00:00Z',
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
current_page: 1,
|
||||
per_page: 20,
|
||||
},
|
||||
})
|
||||
|
||||
const router = makeRouter()
|
||||
router.push('/welcome')
|
||||
await router.isReady()
|
||||
|
||||
const wrapper = mount(SessionSidebar, {
|
||||
global: {
|
||||
plugins: [router, ElementPlus],
|
||||
},
|
||||
})
|
||||
|
||||
await flushPromises()
|
||||
expect(mockedAxios.get).toHaveBeenCalledWith(
|
||||
'http://localhost:8000/api/sessions',
|
||||
expect.any(Object)
|
||||
)
|
||||
expect(wrapper.text()).toContain('Demo')
|
||||
await wrapper.find('.session-item').trigger('click')
|
||||
expect(localStorage.getItem('ars-current-session')).toBe('s1')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user