mirror of
https://github.com/IT4Change/boilerplate-frontend.git
synced 2025-12-13 07:35:53 +00:00
Merge pull request #44 from IT4Change/vike-navigate
feat(frontend): utilize vike-navigate
This commit is contained in:
commit
3fad56a538
@ -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)
|
||||||
|
|||||||
36
src/components/VikeBtn.test.ts
Normal file
36
src/components/VikeBtn.test.ts
Normal 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')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user