| 1 | import type { App, ObjectDirective, Ref } from 'vue' |
| 2 | import { annotate } from '@slidev/rough-notation' |
| 3 | import { describe, expect, it, vi } from 'vitest' |
| 4 | import { ref } from 'vue' |
| 5 | import { useNav } from '../composables/useNav' |
| 6 | import { injectionCurrentPage } from '../constants' |
| 7 | import { resolveClick } from './v-click' |
| 8 | import { createVMarkDirective } from './v-mark' |
| 9 | |
| 10 | vi.mock('@slidev/rough-notation', () => ({ |
| 11 | annotate: vi.fn(() => { |
| 12 | let showing = false |
| 13 | return { |
| 14 | show: vi.fn(() => showing = true), |
| 15 | hide: vi.fn(() => showing = false), |
| 16 | isShowing: () => showing, |
| 17 | } |
| 18 | }), |
| 19 | })) |
| 20 | |
| 21 | vi.mock('../composables/useNav', async () => { |
| 22 | const { ref } = await import('vue') |
| 23 | const nav = { |
| 24 | currentPage: ref(1), |
| 25 | isPrintMode: ref(false), |
| 26 | } |
| 27 | return { |
| 28 | useNav: () => nav, |
| 29 | } |
| 30 | }) |
| 31 | |
| 32 | vi.mock('./v-click', () => ({ |
| 33 | resolveClick: vi.fn(), |
| 34 | })) |
| 35 | |
| 36 | vi.mock('../env', () => ({ |
| 37 | configs: {}, |
| 38 | })) |
| 39 | |
| 40 | const currentPage = useNav().currentPage as unknown as Ref<number> |
| 41 | |
| 42 | let vMark!: ObjectDirective<HTMLElement> |
| 43 | createVMarkDirective().install({ |
| 44 | directive: (_name: string, directive: ObjectDirective<HTMLElement>) => vMark = directive, |
| 45 | } as unknown as App) |
| 46 | |
| 47 | function mountMark(page: number, isActive: boolean) { |
| 48 | vi.mocked(resolveClick).mockReturnValueOnce({ |
| 49 | isActive: ref(isActive), |
| 50 | } as any) |
| 51 | const el = {} as HTMLElement |
| 52 | const binding = { |
| 53 | value: '+0', |
| 54 | modifiers: {}, |
| 55 | instance: { |
| 56 | $: { |
| 57 | provides: { |
| 58 | [injectionCurrentPage as any]: ref(page), |
| 59 | }, |
| 60 | }, |
| 61 | }, |
| 62 | } as any |
| 63 | vMark.mounted!(el, binding, null as any, null as any) |
| 64 | const annotation = vi.mocked(annotate).mock.results.at(-1)!.value |
| 65 | return { el, binding, annotation } |
| 66 | } |
| 67 | |
| 68 | function flush() { |
| 69 | return new Promise(resolve => setTimeout(resolve)) |
| 70 | } |
| 71 | |
| 72 | describe('v-mark', () => { |
| 73 | it('redraws shown marks once their preloaded slide becomes current', async () => { |
| 74 | currentPage.value = 1 |
| 75 | const { annotation } = mountMark(2, true) |
| 76 | expect(annotation.show).toHaveBeenCalledTimes(1) |
| 77 | |
| 78 | currentPage.value = 2 |
| 79 | await flush() |
| 80 | expect(annotation.show).toHaveBeenCalledTimes(2) |
| 81 | }) |
| 82 | |
| 83 | it('does not redraw hidden marks, marks on other slides or unmounted marks', async () => { |
| 84 | currentPage.value = 1 |
| 85 | const hidden = mountMark(2, false) |
| 86 | const other = mountMark(3, true) |
| 87 | const unmounted = mountMark(2, true) |
| 88 | vMark.unmounted!(unmounted.el, unmounted.binding, null as any, null as any) |
| 89 | |
| 90 | currentPage.value = 2 |
| 91 | await flush() |
| 92 | currentPage.value = 1 |
| 93 | await flush() |
| 94 | |
| 95 | expect(hidden.annotation.show).not.toHaveBeenCalled() |
| 96 | expect(other.annotation.show).toHaveBeenCalledTimes(1) |
| 97 | expect(unmounted.annotation.show).toHaveBeenCalledTimes(1) |
| 98 | }) |
| 99 | }) |
| 100 |