Merge pull request #44 from IT4Change/vike-navigate

feat(frontend): utilize vike-navigate
This commit is contained in:
Ulf Gebhardt 2024-01-09 07:02:52 +01:00 committed by GitHub
commit 3fad56a538
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 2 deletions

View File

@ -7,7 +7,7 @@ import { PageContext, VikePageContext } from '#types/PageContext'
import type { App, InjectionKey } from 'vue' import type { App, InjectionKey } from 'vue'
const key: InjectionKey<VikePageContext & PageContext> = Symbol(undefined) const key: InjectionKey<VikePageContext & PageContext> = Symbol('pageContext')
function usePageContext() { function usePageContext() {
const pageContext = inject(key) const pageContext = inject(key)

View File

@ -0,0 +1,36 @@
import { mount } from '@vue/test-utils'
import { navigate } from 'vike/client/router'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import VikeBtn from './VikeBtn.vue'
vi.mock('vike/client/router')
vi.mocked(navigate).mockResolvedValue()
describe.skip('VikeBtn', () => {
const Wrapper = () => {
return mount(VikeBtn, {
global: { provide: { [Symbol('pageContext')]: { urlPathname: 'some-url' } } },
attrs: { href: '/some-path' },
})
}
let wrapper: ReturnType<typeof Wrapper>
beforeEach(() => {
wrapper = Wrapper()
})
it('renders', () => {
expect(wrapper.find('.v-btn').exists()).toBeTruthy()
})
it('icon is hidden', () => {
expect(wrapper.find('.v-icon').exists()).toBe(false)
})
describe('click on button', () => {
it('calls navigate method with given href', async () => {
await wrapper.find('.v-btn').trigger('click')
expect(navigate).toHaveBeenCalledWith('/some-path')
})
})
})

View File

@ -1,13 +1,22 @@
<template> <template>
<v-btn :variant="isRouteSelected($attrs.href as string) ? 'tonal' : 'flat'"> <v-btn
:variant="isRouteSelected($attrs.href as string) ? 'tonal' : 'flat'"
@click.prevent="onClick($attrs.href as string)"
>
<slot /> <slot />
</v-btn> </v-btn>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { navigate } from 'vike/client/router'
import { usePageContext } from '#context/usePageContext' import { usePageContext } from '#context/usePageContext'
const pageContext = usePageContext() const pageContext = usePageContext()
function onClick(href: string) {
return navigate(href)
}
const isRouteSelected = (href: string) => { const isRouteSelected = (href: string) => {
if (href === '/app') { if (href === '/app') {
return pageContext.urlPathname.indexOf(href) === 0 return pageContext.urlPathname.indexOf(href) === 0