From 4dd2805eaaa0663891012b272d06551f272aad2e Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 9 Jan 2024 11:02:24 +0100 Subject: [PATCH] feat: test vike button --- renderer/context/usePageContext.ts | 6 +++--- src/components/VikeBtn.test.ts | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/renderer/context/usePageContext.ts b/renderer/context/usePageContext.ts index 1ed32b4..103e2ba 100644 --- a/renderer/context/usePageContext.ts +++ b/renderer/context/usePageContext.ts @@ -7,16 +7,16 @@ import { PageContext, VikePageContext } from '#types/PageContext' import type { App, InjectionKey } from 'vue' -const key: InjectionKey = Symbol('pageContext') +export const vikePageContext: InjectionKey = Symbol('pageContext') function usePageContext() { - const pageContext = inject(key) + const pageContext = inject(vikePageContext) if (!pageContext) throw new Error('setPageContext() not called in parent') return pageContext } function setPageContext(app: App, pageContext: VikePageContext & PageContext) { - app.provide(key, pageContext) + app.provide(vikePageContext, pageContext) } export { usePageContext } diff --git a/src/components/VikeBtn.test.ts b/src/components/VikeBtn.test.ts index 9054fa7..7111bd6 100644 --- a/src/components/VikeBtn.test.ts +++ b/src/components/VikeBtn.test.ts @@ -2,14 +2,17 @@ import { mount } from '@vue/test-utils' import { navigate } from 'vike/client/router' import { describe, it, expect, beforeEach, vi } from 'vitest' +import { vikePageContext } from '#context/usePageContext' + import VikeBtn from './VikeBtn.vue' vi.mock('vike/client/router') vi.mocked(navigate).mockResolvedValue() -describe.skip('VikeBtn', () => { + +describe('VikeBtn', () => { const Wrapper = () => { return mount(VikeBtn, { - global: { provide: { [Symbol('pageContext')]: { urlPathname: 'some-url' } } }, + global: { provide: { [vikePageContext]: { urlPathname: '/some-url' } } }, attrs: { href: '/some-path' }, }) } @@ -27,6 +30,26 @@ describe.skip('VikeBtn', () => { 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', () => { it('calls navigate method with given href', async () => { await wrapper.find('.v-btn').trigger('click')