import type { ClicksContext } from '@slidev/types' import type { RendererElement, RendererNode } from 'vue' import { compileTemplate } from '@vue/compiler-sfc' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { compile, createRenderer, defineComponent, h, nextTick, onMounted, onUnmounted, ref, shallowRef } from 'vue' import VAfter from '../packages/client/builtin/VAfter' import VClick from '../packages/client/builtin/VClick' import { createClicksContextBase } from '../packages/client/composables/useClicks' import { injectionClicksContext, injectionFrontmatter } from '../packages/client/constants' import { createVClickDirectives } from '../packages/client/modules/v-click' import { transformVClickMount } from '../packages/slidev/node/vite/vClickMount' vi.stubGlobal('__DEV__', true) // `` of the `.mount` content reads the computed transition of the element, // so this is what `.slidev-vclick-target { transition: all 0.1s }` would give in a browser. let transitionDuration = '0s' beforeEach(() => { transitionDuration = '0s' vi.useFakeTimers() vi.stubGlobal('requestAnimationFrame', (cb: () => void) => setTimeout(cb, 16)) vi.stubGlobal('window', { getComputedStyle: (el: TestNode) => ({ transitionDuration: el.classList.contains('slidev-vclick-target') || String(el.props.class).includes('slidev-vclick-target') ? transitionDuration : '0s', transitionDelay: '0s', transitionProperty: 'all', animationDuration: '0s', animationDelay: '0s', }), }) }) afterEach(() => { vi.useRealTimers() vi.unstubAllGlobals() vi.stubGlobal('__DEV__', true) }) vi.mock('../packages/client/env', () => ({ configs: {}, })) // `.vue` files aren't compiled in this test. With a single `` the gap is always 0. vi.mock('../packages/client/builtin/VClickGap.vue', async () => { const { defineComponent } = await import('vue') return { default: defineComponent({ render: () => null, }), } }) function compileToCode(source: string) { const { code, errors } = compileTemplate({ source, filename: 'slides.md', id: 'test', compilerOptions: { nodeTransforms: [transformVClickMount], }, }) expect(errors).toEqual([]) return code } describe('v-click.mount template transform', () => { it('wraps the element in ', () => { const code = compileToCode(`
A
`) expect(code).toContain('_resolveComponent("v-click")') expect(code).toContain('mount: ""') expect(code).toContain('at: 3') expect(code).not.toContain('_resolveDirective("click")') expect(code).toMatchInlineSnapshot(` "import { createElementVNode as _createElementVNode, resolveComponent as _resolveComponent, withCtx as _withCtx, openBlock as _openBlock, createBlock as _createBlock } from "vue" export function render(_ctx, _cache) { const _component_v_click = _resolveComponent("v-click") return (_openBlock(), _createBlock(_component_v_click, { mount: "", at: 3 }, { default: _withCtx(() => [...(_cache[0] || (_cache[0] = [ _createElementVNode("div", { class: "a" }, "A", -1 /* CACHED */) ]))]), _: 1 /* STABLE */ })) }" `) }) it('keeps the default value, arrays and relative values', () => { expect(compileToCode(`
A
`)).not.toContain('at:') expect(compileToCode(`
A
`)).toContain('at: [3, 5]') expect(compileToCode(`
A
`)).toContain(`at: '+2'`) }) it('supports .hide, v-after and v-click-hide', () => { const hide = compileToCode(`
A
`) expect(hide).toContain('hide: ""') expect(hide).toContain('at: 3') const after = compileToCode(`
A
`) expect(after).toContain('at: "+0"') expect(after).not.toContain('hide:') expect(compileToCode(`
A
`)).toContain('hide: ""') expect(compileToCode(`
A
`)).toContain('hide: ""') }) it('passes animation modifiers as the animation prop', () => { const code = compileToCode(`
A
`) expect(code).toContain('animation: "fade up"') expect(code).toContain('at: 3') expect(compileToCode(`
A
`)).toMatch(/hide: "",\s+at: "\+0",\s+animation: "scale"/) expect(compileToCode(`
A
`)).not.toContain('animation:') }) it('wraps the children of