feat: test vike button

This commit is contained in:
Moriz Wahl 2024-01-09 11:02:24 +01:00
parent c7db269664
commit 4dd2805eaa
2 changed files with 28 additions and 5 deletions

View File

@ -7,16 +7,16 @@ import { PageContext, VikePageContext } from '#types/PageContext'
import type { App, InjectionKey } from 'vue' import type { App, InjectionKey } from 'vue'
const key: InjectionKey<VikePageContext & PageContext> = Symbol('pageContext') export const vikePageContext: InjectionKey<VikePageContext & PageContext> = Symbol('pageContext')
function usePageContext() { function usePageContext() {
const pageContext = inject(key) const pageContext = inject(vikePageContext)
if (!pageContext) throw new Error('setPageContext() not called in parent') if (!pageContext) throw new Error('setPageContext() not called in parent')
return pageContext return pageContext
} }
function setPageContext(app: App, pageContext: VikePageContext & PageContext) { function setPageContext(app: App, pageContext: VikePageContext & PageContext) {
app.provide(key, pageContext) app.provide(vikePageContext, pageContext)
} }
export { usePageContext } export { usePageContext }

View File

@ -2,14 +2,17 @@ import { mount } from '@vue/test-utils'
import { navigate } from 'vike/client/router' import { navigate } from 'vike/client/router'
import { describe, it, expect, beforeEach, vi } from 'vitest' import { describe, it, expect, beforeEach, vi } from 'vitest'
import { vikePageContext } from '#context/usePageContext'
import VikeBtn from './VikeBtn.vue' import VikeBtn from './VikeBtn.vue'
vi.mock('vike/client/router') vi.mock('vike/client/router')
vi.mocked(navigate).mockResolvedValue() vi.mocked(navigate).mockResolvedValue()
describe.skip('VikeBtn', () => {
describe('VikeBtn', () => {
const Wrapper = () => { const Wrapper = () => {
return mount(VikeBtn, { return mount(VikeBtn, {
global: { provide: { [Symbol('pageContext')]: { urlPathname: 'some-url' } } }, global: { provide: { [vikePageContext]: { urlPathname: '/some-url' } } },
attrs: { href: '/some-path' }, attrs: { href: '/some-path' },
}) })
} }
@ -27,6 +30,26 @@ describe.skip('VikeBtn', () => {
expect(wrapper.find('.v-icon').exists()).toBe(false) expect(wrapper.find('.v-icon').exists()).toBe(false)
}) })
describe('with href attribute app', () => {
beforeEach(async () => {
await wrapper.setProps({ href: '/app' })
})
it('has flat variant', () => {
expect(wrapper.classes()).toContain('v-btn--variant-flat')
})
})
describe('with same href attribute', () => {
beforeEach(async () => {
await wrapper.setProps({ href: '/some-url' })
})
it('has tonal variant', () => {
expect(wrapper.classes()).toContain('v-btn--variant-tonal')
})
})
describe('click on button', () => { describe('click on button', () => {
it('calls navigate method with given href', async () => { it('calls navigate method with given href', async () => {
await wrapper.find('.v-btn').trigger('click') await wrapper.find('.v-btn').trigger('click')