initial commit
This commit is contained in:
40
test/nuxt/useLiveSessions.spec.js
Normal file
40
test/nuxt/useLiveSessions.spec.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mountSuspended, registerEndpoint } from '@nuxt/test-utils/runtime'
|
||||
import { defineComponent, h } from 'vue'
|
||||
import { useLiveSessions } from '../../app/composables/useLiveSessions.js'
|
||||
|
||||
describe('useLiveSessions', () => {
|
||||
it('fetches sessions from API and returns sessions ref', async () => {
|
||||
registerEndpoint('/api/live', () => [
|
||||
{ id: 's1', label: 'Live 1', hasStream: true, lat: 37, lng: -122 },
|
||||
])
|
||||
registerEndpoint('/api/me', () => ({ id: '1', identifier: 'u', role: 'member' }), { method: 'GET' })
|
||||
const TestComponent = defineComponent({
|
||||
setup() {
|
||||
const { sessions } = useLiveSessions()
|
||||
return () => h('div', { 'data-sessions': JSON.stringify(sessions.value) })
|
||||
},
|
||||
})
|
||||
const wrapper = await mountSuspended(TestComponent)
|
||||
await new Promise(r => setTimeout(r, 100))
|
||||
expect(wrapper.find('[data-sessions]').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('returns empty array when fetch fails', async () => {
|
||||
registerEndpoint('/api/live', () => {
|
||||
throw new Error('fetch failed')
|
||||
})
|
||||
registerEndpoint('/api/me', () => ({ id: '1', identifier: 'u', role: 'member' }), { method: 'GET' })
|
||||
const TestComponent = defineComponent({
|
||||
setup() {
|
||||
const { sessions } = useLiveSessions()
|
||||
return () => h('div', { 'data-sessions': JSON.stringify(sessions.value) })
|
||||
},
|
||||
})
|
||||
const wrapper = await mountSuspended(TestComponent)
|
||||
await new Promise(r => setTimeout(r, 150))
|
||||
const el = wrapper.find('[data-sessions]')
|
||||
expect(el.exists()).toBe(true)
|
||||
expect(JSON.parse(el.attributes('data-sessions'))).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user