|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +const createAppMock = vi.fn(); |
| 4 | +const createPiniaMock = vi.fn(); |
| 5 | +const useSuperdocStoreMock = vi.fn(); |
| 6 | +const useCommentsStoreMock = vi.fn(); |
| 7 | +const useHighContrastModeMock = vi.fn(); |
| 8 | +const clickOutsideDirectiveMock = vi.fn(); |
| 9 | + |
| 10 | +vi.mock('vue', () => ({ createApp: createAppMock })); |
| 11 | +vi.mock('pinia', () => ({ createPinia: createPiniaMock })); |
| 12 | +vi.mock('@superdoc/common', () => ({ vClickOutside: clickOutsideDirectiveMock })); |
| 13 | +vi.mock('../stores/superdoc-store', () => ({ useSuperdocStore: useSuperdocStoreMock })); |
| 14 | +vi.mock('../stores/comments-store', () => ({ useCommentsStore: useCommentsStoreMock })); |
| 15 | +vi.mock('../composables/use-high-contrast-mode', () => ({ |
| 16 | + useHighContrastMode: useHighContrastModeMock, |
| 17 | +})); |
| 18 | +vi.mock('../SuperDoc.vue', () => ({ default: { name: 'SuperDocMock' } })); |
| 19 | + |
| 20 | +const setupAppMocks = () => { |
| 21 | + const originalUnmount = vi.fn(); |
| 22 | + const app = { |
| 23 | + use: vi.fn(), |
| 24 | + directive: vi.fn(), |
| 25 | + unmount: originalUnmount, |
| 26 | + }; |
| 27 | + createAppMock.mockReturnValue(app); |
| 28 | + createPiniaMock.mockReturnValue({}); |
| 29 | + useSuperdocStoreMock.mockReturnValue({}); |
| 30 | + useCommentsStoreMock.mockReturnValue({}); |
| 31 | + useHighContrastModeMock.mockReturnValue({}); |
| 32 | + return { app, originalUnmount }; |
| 33 | +}; |
| 34 | + |
| 35 | +const safeDelete = (key) => { |
| 36 | + const desc = Object.getOwnPropertyDescriptor(globalThis, key); |
| 37 | + if (!desc || desc.configurable !== false) { |
| 38 | + delete globalThis[key]; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +describe('createSuperdocVueApp edge cases', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.resetModules(); |
| 45 | + vi.clearAllMocks(); |
| 46 | + safeDelete('__VUE_DEVTOOLS_GLOBAL_HOOK__'); |
| 47 | + safeDelete('__VUE_DEVTOOLS_PLUGINS__'); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + safeDelete('__VUE_DEVTOOLS_GLOBAL_HOOK__'); |
| 52 | + safeDelete('__VUE_DEVTOOLS_PLUGINS__'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('handles queue replacement via property setter', async () => { |
| 56 | + const { app } = setupAppMocks(); |
| 57 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 58 | + createSuperdocVueApp({ disablePiniaDevtools: true }); |
| 59 | + |
| 60 | + const newQueue = []; |
| 61 | + globalThis.__VUE_DEVTOOLS_PLUGINS__ = newQueue; |
| 62 | + newQueue.push([{ id: 'dev.esm.pinia', app }, vi.fn()]); |
| 63 | + expect(newQueue).toHaveLength(0); |
| 64 | + |
| 65 | + const otherApp = {}; |
| 66 | + newQueue.push([{ id: 'dev.esm.pinia', app: otherApp }, vi.fn()]); |
| 67 | + expect(newQueue).toHaveLength(1); |
| 68 | + }); |
| 69 | + |
| 70 | + it('handles multiple unmount calls safely', async () => { |
| 71 | + const { app } = setupAppMocks(); |
| 72 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 73 | + createSuperdocVueApp({ disablePiniaDevtools: true }); |
| 74 | + app.unmount(); |
| 75 | + app.unmount(); // second call — no-op via ref count guard |
| 76 | + expect(true).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it('handles non-array pre-existing queue', async () => { |
| 80 | + globalThis.__VUE_DEVTOOLS_PLUGINS__ = { notAnArray: true }; |
| 81 | + const { app } = setupAppMocks(); |
| 82 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 83 | + expect(() => createSuperdocVueApp({ disablePiniaDevtools: true })).not.toThrow(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('emits unrelated events without suppression', async () => { |
| 87 | + const emitSpy = vi.fn(() => 'emitted'); |
| 88 | + globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__ = { emit: emitSpy }; |
| 89 | + const { app } = setupAppMocks(); |
| 90 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 91 | + createSuperdocVueApp({ disablePiniaDevtools: true }); |
| 92 | + const hook = globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 93 | + expect(hook.emit('other-event', { id: 'other', app: {} })).toBe('emitted'); |
| 94 | + expect(emitSpy).toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('returns app + stores from factory', async () => { |
| 98 | + const { app } = setupAppMocks(); |
| 99 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 100 | + const result = createSuperdocVueApp({ disablePiniaDevtools: false }); |
| 101 | + expect(result.app).toBe(app); |
| 102 | + expect(result.pinia).toBeDefined(); |
| 103 | + expect(result.superdocStore).toBeDefined(); |
| 104 | + expect(result.commentsStore).toBeDefined(); |
| 105 | + expect(result.highContrastModeStore).toBeDefined(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('replacing hook at runtime picks up new hook instance', async () => { |
| 109 | + const { app } = setupAppMocks(); |
| 110 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 111 | + createSuperdocVueApp({ disablePiniaDevtools: true }); |
| 112 | + |
| 113 | + const firstEmit = vi.fn(() => 'a'); |
| 114 | + globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__ = { emit: firstEmit }; |
| 115 | + let hook = globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 116 | + expect(hook.emit('devtools-plugin:setup', { id: 'dev.esm.pinia', app }, vi.fn())).toBeUndefined(); |
| 117 | + |
| 118 | + const secondEmit = vi.fn(() => 'b'); |
| 119 | + globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__ = { emit: secondEmit }; |
| 120 | + hook = globalThis.__VUE_DEVTOOLS_GLOBAL_HOOK__; |
| 121 | + expect(hook.emit('devtools-plugin:setup', { id: 'dev.esm.pinia', app }, vi.fn())).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('replacement queue also intercepts pinia setup for suppressed app', async () => { |
| 125 | + const { app } = setupAppMocks(); |
| 126 | + const { createSuperdocVueApp } = await import('./create-app.js'); |
| 127 | + createSuperdocVueApp({ disablePiniaDevtools: true }); |
| 128 | + |
| 129 | + const replacement1 = []; |
| 130 | + globalThis.__VUE_DEVTOOLS_PLUGINS__ = replacement1; |
| 131 | + replacement1.push([{ id: 'dev.esm.pinia', app }, vi.fn()]); |
| 132 | + expect(replacement1).toHaveLength(0); |
| 133 | + |
| 134 | + // Replace again — new queue should also get patched |
| 135 | + const replacement2 = []; |
| 136 | + globalThis.__VUE_DEVTOOLS_PLUGINS__ = replacement2; |
| 137 | + replacement2.push([{ id: 'dev.esm.pinia', app }, vi.fn()]); |
| 138 | + expect(replacement2).toHaveLength(0); |
| 139 | + }); |
| 140 | +}); |
0 commit comments